Swordrock's Blog

Just another WordPress.com site

C++ 2

Hi ,

Last lesson i share some basic knowledge and code analysing , designing and writing process . Every code brings a solution to problem/problems , so i tell how to attend to solution . I also tell why object using and object mesaggings are important in C++ .

Today i tell you  why C++ is better than C , declaration and deefinition basics , syntax usage in C++ , funcion variable definiton,including headers  . As lesson ends , we get the knowledge of writing our first code : radix converter

Why C++ is better ?

#improved productivity

#closed lots of holes in C

#the need for the preprocessor has been eliminated for value substitution and macros . (it was difficult to find lots of bugs before.)

#a feature called “namespace”  improves control of names .

#%10 faster program speed compared to C

Difference between declaration and definitions

A declaration introduces a name-an identifier-to the compiler . It tells the compiler “This function or this variable exists somewhere,and there how it is .”

A definition says “make this variable here or make this function here.”

It allocates storage for the name .

You can declare a variable or a fuınction wherever you want but only one definiton (this is called OOD rule) is allowed in C and C++ .

Function declaration syntax

İnt func1 (int,int)  // takes two arguments as integer

The first keyword is the return value : int

The arguments are enclosed by parenthesis

The semicolon tells the end of the statement , it tells the compiler “this is the end of this function”

We can declare func1() in a different way but with the same meaning :

İnt func1 (int length,int height)

Function definiton

İnt func1 (int length,int height)  {}

There is no semicolon but the  braces .

Variable declaration syntax

Tells the compiler what a variable looks like . it says “i know you havent seen it before but i will use it in somewhere. ”

İnt a ;

Including Headers

There is lots of functions and variables in the libraries .

To save work , C and C++ use files called “header file”

A header file contains external declarations .

It has a filename called headerfile.h

To include header file we use  :

#include <header>

We can use file name in double quotes like :

#include “iostream.h”

They are old tradidional versions , but now for C++ , you can use them like :

#include <iostream>

So  ;

#include <stdio.h>

#include <stdlib.h>


#include <cstdio>

#include <cstdlib>

// The iostream package automatically defines a variable (an object)  called cout that accepts all data bound for standart output .

// to send the data to standart output , we use the operator  << (bitwise left shift), it means “send to.” Cout  is hort of “console output” .

Cout << “hey , what’s up man ? ”<<

//namespace  : each set of C++ definitions in a libray or program is wrapped in a namespace . This keyword tells the compiler that i want to use definitons and/or declarations in this namespace .

Using namespace std ; // this means that you want to expose all the elements from the namespace called “std” .

Now i will tell an important note , when we use C programming language,we add header file to the program with “.h”  . But in C++ , you dont need to use it like that . There is a relationship between them .

#include <iostream.h>

Means now

#include <iostream>

using namespace std ;

Program Structure

When you begin to write code , you write primary code in main () .

As i tell earlier , you declare a function like that :

İnt func1 () {

//function code here


Main is always a function and it returns a value “int” .

Inside a character array , you can insert special characters for example

\n means newline

\t means tab


\b backsapce

Now let s write our first program :

Eg :


// C02 hello world program

e.g :

#include <iostream> 

using namespace std ;

int main ()


cout << “hello,world i am ”

<< 8 << ” Today!” << endl ;      system (“pause”);


As you see , we say hello to world , we only need iostream header and we get an output from it  .


Now we will see another stream features of iostream  :

Eg2 :


// CO2 : Stream.cpp

// more stream features on iostream

//iostream manipulators for converting numbers

#include <iostream>

using namespace std ;

int main () {

// specifying formats with manipulators

cout << “a number in decimal :”

<< dec << 15 << endl ;

cout << “in octal :”<<oct <<15<<endl ;

cout <<“in hex : “<< hex<< 15 << endl ;

cout <<“a floating point number : ” <<3.415698<< endl ;

cout << “non-printing char (escape): ” << char(27)<< endl ;   //any character can be sent to cout object using char (ascii code)

system (“pause”);



These code will work like that figure when you compile and run  :

I want everybody first learn “cout” object usage good enough , so let’s do another example on that



Character array concatenation

E.g  :

#include <iostream>

using namespace std;

int main() {

cout << “This is far too long to put on a ”

“single line but it can be broken up with ”

“no ill effects\nas long as there is no ”

“punctuation separating adjacent character ”


System (“pause”) ;



These code will also work like that :


Here is a video about OOP :

So useful , must SEE !


Thats all for today , thanks for reading , See you .


Ekim 30, 2010 - Posted by | C/C++ programming | , , ,

Henüz yorum yapılmamış.

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Connecting to %s

%d blogcu bunu beğendi: