Derived Types: Classes

A class is a user-defined type.

Syntax:

class class_name
{
private:
// Private Data and Functions
public:
// Public Data and Functions
} object_name;

Defined Member Functions

Member functions can be defined in two types:

1. Inside the class definition: example,

#include
class number
{
private:
int a;
public:
void SetData(int n)
{ a=n;}
void DisplayData()
{ cout<<"Number is "<};

void main()
{
number num1;
num1.SetData(145);
num1.DisplayData();
}

Output:
Number is 145

2. Outside the class definition:

Syntax:
return-type class-name::function-name(parameter-list)
{
// body of function
}

Example:

#include
class number
{
private:
int a;
public:
void SetData(int n);
void DisplayData();
};

void number::SetData(int n)
{
a=n;
}
void number::DisplayData()
{
cout<<"Number is "<}

Assigning Objects

One object can be assigned to another provided that both objects are of the same type. By default, when one object is assigned to another, a bitwise copy of all the data member are made.

Example:
#include
class complex
{
private:
float real, imag;
public:
void ReadComplex()
{
cout<<"Input real part";
cin>>real;
cout<<"Input imaginary part ";
cin>>imag;
}
void DisplayComplex();
{
cout<<"Complex number is ";
cout< }
};

void main()
{
complex C1,C2;
C1.ReadComplex();
C2=C1;
C1.DisplayComplex();
C2.DisplayComplex();
}

Output:
Input real part 3
Input imaginary part 2
Complex number is 3+i2
Complex number is 3+i2

Passing Objects to Functions

Objects can be passed to functions as arguments in just the same way as other types of data are passed. Simply declare the function's parameter as a class type and the use an object of the class as an argument when calling the function.

Example:
#include
class complex
{
private:
float real, imag;
public:
void ReadComplex()
{
cout<<"Input real part";
cin>>real;
cout<<"Input imaginary part ";
cin>>imag;
}
void DisplayComplex();
{
cout<<"Complex number is ";
cout< }
void AddComplex( complex C1, complex C2)
{
real=C1.real+C2.real;
imag=C1.imag+C2.imag;
}
};

void main()
{
complex C1,C2,C3;
cout<<"First Complex Number : "< C1.ReadComplex();
cout<<"Second Complex Number : "< C2.ReadComplex();
C3.AddComplex(C1, C2);
cout<<"Sun of Two Complex Numbers : "< C3.DisplayComplex();
}

Output:
First Complex Number :
Input real part 2
Input imaginary part 5
Second Complex Number :
Input real part 4
Input imaginary part 2
Sun of Two Complex Numbers :
Complex number is 6+i7

Return Objects From Functions

Example:
#include
class complex
{
private:
float real, imag;
public:
void ReadComplex()
{
cout<<"Input real part";
cin>>real;
cout<<"Input imaginary part ";
cin>>imag;
}
void DisplayComplex();
{
cout<<"Complex number is ";
cout< }
complex AddComplex( complex C2)
{
complex temp; //temporary variable to hold
temp.real=real+C2.real; //sum of two complex number
temp.imag=imag+C2.imag;
return temp;
}
};

void main()
{
complex C1,C2,C3;
cout<<"First Complex Number : "< C1.ReadComplex();
cout<<"Second Complex Number : "< C2.ReadComplex();
C3=C1.AddComplex(C2);
cout<<"Sun of Two Complex Numbers : "< C3.DisplayComplex();
}

Output:
First Complex Number :
Input real part 4
Input imaginary part 3
Second Complex Number :
Input real part 1
Input imaginary part 2
Sun of Two Complex Numbers :
Complex number is 5+i5

Friend Function

#include
class Wife;
class Husband
{
private:
char name[20];
int salary;
public:
void ReadData()
{
cout<<"Input Husband's name: ";
cin>>name;
cout<<"Input salary";
cin>>salary;
}
friend int TotalSalary(Husband, Wife);
};
class Wife
{
private:
char name[20];
int salary;
public:
void ReadData()
{
cout<<"Input Wife's name: ";
cin>>name;
cout<<"Input salary";
cin>>salary;
}
friend int TotalSalary(Husband, Wife);
};
int TotalSalary(Husband H, Wife W)
{
return (H.salary+W.salary);
}

void main()
{
Husband H;
Wife W;
H.ReadData();
W.ReadData();
cout<<"Total salary of the family is "<}

Arrays of Objects

#include
#include
class Student
{
private:
int IdNo;
char Name[20];
int Marks;
public:
void ReadData()
{
cout<<"Input Students' Information (Id, Name, Marks): ";
cin>>IdNo>>Name>>Marks;
}
void DisplayData()
{
cout<}
};
void main()
{
Student COLLEGE[100]; // Array of Student
int N,i;
cout<<"Input Number of Students.";
cin>>N; // Number of Student
for(i=0;iCOLLEGE[i].ReadData();
cout<<"IdNo"<for(i=0;iCOLLEGE[i].DisplayData();
}

Output
Input Number of Students.3
Input Students' Information (Id, Name, Marks): 1 Sumon 78
Input Students' Information (Id, Name, Marks): 2 roy 45
Input Students' Information (Id, Name, Marks): 3 kumar 89
IdNo Name Marks
1 Sumon 78
2 roy 45
3 kumar 89

Static Data Member

#include
class number
{
private:
static int count; //only one data item for all objects
int n;
public:
void AssignData(int x)
{
n=x;
count++;
}
void DisplayCount()
{
cout<<"Count Value = "<}
};
//Definition of number::count is still private to number
//int number::count=4;
int number::count;
void main()
{
// Create three objects and assign zero to count
number ob1, ob2, ob3;
ob1.DisplayCount();
ob2.DisplayCount();
ob3.DisplayCount();
ob1.AssignData(509);
ob2.AssignData(460);
ob3.AssignData(700);
cout<<"After assigning data "<ob1.DisplayCount();
ob2.DisplayCount();
ob3.DisplayCount();
}

Output
Count Value = 0
Count Value = 0
Count Value = 0
After assigning data
Count Value = 3
Count Value = 3
Count Value = 3

Static Member Function

#include
class number
{
private:
static int count;
int IdNo;
public:
void AssignIdNo()
{
IdNo=++count;
}
void DisplayIdNo()
{
cout<<"Object Number : "<}
static void DisplayCount()
{
cout<<"Number of objects created so far = "<}
};
int number::count;
void main()
{
number ob1;
ob1.AssignIdNo();
number::DisplayCount(); // Call static member
number ob2, ob3;
ob2.AssignIdNo();
ob3.AssignIdNo();
number::DisplayCount(); // Call static member
ob1.DisplayIdNo();
ob2.DisplayIdNo();
ob3.DisplayIdNo();
}

Output
Number of objects created so far = 1
Number of objects created so far = 3
Object Number : 1
Object Number : 2
Object Number : 3

Pointer within a Class

#include
class Number
{
private:
int a; //Member variable
public:
Number(int x){a=x;} //Constructor
int GetNumber() {return a;}
};

void main()
{
Number N(340); // Create Object
Number *p; // Create a pointer to object
p=&N;
cout<<"Value using object "<cout<<"Value using pointer "<GetNumber();
}

Output
Value using object 340
Value using pointer 340

Derived Types: Enumerations

Continuing ...

Derived Types: Union

A union is similar to a struct, except it allows you to define variables that share storage space.

Syntax:
union [] {
;
...
} [] ;

Example:
union int_or_long {
int i;
long l;
} a_number;

Turbo C++ will allocate enough storage in a_number to accommodate the largest element in the union. Unlike a struct, the variables a_number.i and a_number.l occupy the same location in memory. Thus, writing into one will overwrite the other. Elements of a union are accessed in the same manner as a struct.

Derived Types: Struct

Continuing ...

Derived Types: Strings

Continuing ...

Derived Types: Arrays

Initializing Arrays

int array1[4] = {0,1,2,3};

int array2[4] = {0,1,2};

int array3[1000] = {0};

int array4[] = {0,1,2,3};

char word[] = "Hello";

int scores[3][5] = {
{92,73,57,98,89},
{88,34,65,32,56},
{94,5,67,0,78,45}
};

int scores[][] = {
{92,73,57,98,89},
{88,34,65,32,56},
{94,5,67,0,78,45}
};

Using Arrays

Example 01:
#include
int main()
{
int test_index, student_index, scores[3][5] = {
{ 92 , 73 , 57 , 98 , 89 },
{ 88 , 76 , 23 , 95 , 72 },
{ 94 , 82 , 63 , 99 , 94 }
};
float sum;
for (test_index = 0; test_index < 3; test_index++){
for (student_index = 0, sum = 0; student_index < 5; student_index++)
sum = sum + scores[test_index][student_index];
cout << "Average for test" << test_index + 1 << " is " << sum / 5 << endl;
}
return 0;
}

Example 02:
#include
int main()
{
int scores[5];
scores[0] = 91;
scores[1] = 21;
scores[2] = 41;
scores[3] = 81;
scores[4] = 91;
cout<<"The score of the third person is "< return 0;
}

Determining The Size of An Array

We can use the sizeof operator to find the size of a one-dimensional array, because sizeof will tell us both the total size of the array in bytes and the size of each element in the array and all we have to do is to divide one by the other.

Example:
#include
int main()
{
int scores[5] = {92 , 73 , 57 , 98 , 89 };
cout << "The array is " << sizeof(scores) << " bytes long." << endl;
cout << "The array has " << sizeof(scores) / sizeof(int) << " elements.";
return 0;
}

Output:
The array is 10 bytes long.
The array has 5 elements.

Pointers

Address Operator
int ptr, num=45;
ptr = #

This places the address where num is stored into the variable ptr. If num is stored in memory 21260 address, then the variable ptr has the value 21260.

Pointer Expressions and Pointer Arithmetic
Pointer are variables. They are not integer, but they can usually be displayed as unsigned integer. The conversion specified for a pointer a pointer is added and subtracted. For example, ptr++ causes the pointer to be incremented, but not by 1. Suppose n is an integer and the system stores integer and the system stores integers in two bytes. Further suppose the address of n would occupy bytes 65524 and 65523.

Example:
int n, *p;
n=16;
p=&n;
p++;
cout<

Example: if ptr1 and ptr2 are declared as pointer variables, then the following are valid use of pointers in arithmetic operators.
1. *ptr1 = *ptr + *ptr; Same as (*ptr1)+(*ptr2)
2. sum = *ptr +10; Same as (*ptr) + 10
3. sum += *ptr2; Same as sum + (*ptr2)
4. result = 10 * - *ptr1/*ptr; Same as ((10 * (-(*ptr1))) / (*ptr2))
5 ptr1 > ptr2;
6. ptr2 != ptr2;


Pointers and Functions
The usage of the pointers in a function may be classified into two group:
1. Call by Value.
2. Call by Reference.

Call by Value - We have seen that when a function is invoked, there is established a correspondence between the formal and actual parameters. A temporary storage is created where the value of the actual parameter is stored. The formal parameter picks up its value from this storage area.
Example:
#include
int function(int, int);
void main()
{
int a=20, b=30;

cout<<"\nValue of a and b before calling the function:\na = "<<<"\nb = "<<<"\nValue of a and b after the function called:\na = "<<<"\nb = "<

Call by Reference - The process of calling a function using pointers to pass the address of variable is known as call by reference. The function which is called by 'reference' can change the value of the variable used in the call.
Example:
#include

void modify(int*);

void main()

{
int a=100;
cout<<"Value of a before calling the function = "<<<"Value
of a after the function called = "<

Pointers and Arrays

Example:
#include
void main()

{
int i=3, *x;
float j=1.5, *y;
char k='c', *z;

cout<<"\nValue of i = "<<<"\nValue of j = "<<<"\nValue of k = "<<<"\nOriginal Address in x = "<<<"\nOriginal Address in y = "<<<"\nOriginal Address in z = "<<<"\nNew Address in x = "<<<"\nNew Address in y = "<<<"\nNew Address in z = "<

Output
Value of i = 3
Value of j = 1.5
Value of k = c
Original Address in x = 0x8f5efff4
Original Address in y = 0x8f5efff0
Original Address in z = c
New Address in x = 0x8f5efff6
New Address in y = 0x8f5efff4
New Address in z =

Example:
#include
void main()

{
int arr[] = {10,20,30,40,50,60,70};
int *i, *j;

i=&arr[1];
j=&arr[5];
cout<<"\nThe Address of arr[1] = "<<&arr[1

]; cout<<"\nThe Address of arr[5] = "<<&arr[5]; cout<<"\nThe Address of i = "<<&i; cout<<"\nThe Address of j = "<<&j; cout<<"\nj - i = "<<<" - "<<<" = "<<<"\n*j - *i = "<<*j<

<" - "<<*i<<" = "<<*j-*i; }

Output
The Address of arr[1] = 0x8fc9ffe6
The Address of arr[5] = 0x8fc9ffee

The Address of i = 0x8fc9fff4
The Address of j = 0x8fc9fff2
j - i = 0x8fc9ffee - 0x8fc9ffe6 = 4
*j - *i = 60 - 20 = 40

Example: Here is a program that prints out the memory locations in which the elements of this array are stored.

void main()

{
int num[]={24,34,12,44,56,17};


for(int i=0; i<=5; i++){ cout<<"\nElement No. "<<<" Address = "<<&num[i]; } }

Output:
Element No. 0 Address = 0x8fbfffea
Element No. 1 Address = 0x8fbfffec
Element No. 2 Address = 0x8fbfffee

Element No. 3 Address = 0x8fbffff0
Element No. 4 Address = 0x8fbffff2
Element No. 5 Address = 0x8fbffff4

Example:

#include
void main()

{
int s[5][2]={
{1234, 56},
{1212, 33},
{1434, 80},

{1312, 78}
};
for(int i=0; i<=3; i++){ cout<<'\n'; for(int j=0; j<=1; j++) cout<<*(*(s+i)+j); } }

Output:
1234 56

1212 33
1434 80
1312 78

Example:

#include
void main()
{
int *arr[4]; //array of integer pointers
int i=31, j=5, k=19, l=71;
arr[0]=&i;
arr[1]=&j;
arr[2]=&k;

arr[3]=&l;
for(int m=0; m<=3; m++){ cout<<"\nThe Value of "<<(char) (73+m)<<" is "<<*arr[m]; cout<<"\nThe Carrying Value of arr["<<<"] is "<<<"\nThe Address of arr["<<<"] is "<<&arr[m]; cout<<"\nThe Value of arr["<<<"] is "<<*arr[m]; } }

Output:
The Value of I is 31
The Carrying Value of arr[0] is 0x8fb0ffec

The Address of arr[0] is 0x8fb0ffee
The Value of arr[0] is 31
The Value of J is 5
The Carrying Value of arr[1] is 0x8fb0ffea
The Address of arr[1] is 0x8fb0fff0
The Value of arr[1] is 5
The Value of K is 19

The Carrying Value of arr[2] is 0x8fb0ffe8
The Address of arr[2] is 0x8fb0fff2
The Value of arr[2] is 19
The Value of L is 71
The Carrying Value of arr[3] is 0x8fb0ffe6
The Address of arr[3] is 0x8fb0fff4
The Value of arr[3] is 71

Example: The following program would justify an array of pointers can have the address of other arrays.
#include
void main()
{

int a[]={1,2,3,4,5};
int *ptr[]={a, a+1, a+2, a+3, a+4};
for(int i=0; i<=4; i++) cout<<'\n'<<*ptr[i]<<" Stored at Address "<

Output:
1 Stored at Address 0x8fcaffec
2 Stored at Address 0x8fcaffee
3 Stored at Address 0x8fcafff0
4 Stored at Address 0x8fcafff2

5 Stored at Address 0x8fcafff4

Pointers to Pointers
The concept of pointer are further extended. Pointer we know is a variable which contains the address of a variable.

Example:

#include
void main()
{
int a=45;
int *ptr;
int **ptr_ptr;

ptr=&a;
ptr_ptr=&ptr;

cout<<"\nAddress of a = "<<&a; cout<<"\nAddress of a = "<<<"\nAddress of a = "<<*ptr_ptr; cout<<"\nAddress of ptr = "<<&ptr; cout<<"\nAddress of ptr = "<<<"\nAddress of ptr_ptr = "<<&ptr_ptr; cout<<"\nValue of a = "<<<"\nValue of a = "<<*(&a); cout<<"\nValue of a = "<<*ptr; cout<<"\nValue of a = "<<**ptr_ptr; cout<<"\nValue of ptr = "<<<"\nValue of ptr_ptr = "<

Output:
Address of a = 0x8faafff4
Address of a = 0x8faafff4
Address of a = 0x8faafff4
Address of ptr = 0x8faafff2
Address of ptr = 0x8faafff2
Address of ptr_ptr = 0x8faafff0
Value of a = 45
Value of a = 45
Value of a = 45
Value of a = 45
Value of ptr = 0x8faafff4
Value of ptr_ptr = 0x8faafff2

Variables

Integer with long, short and signed, unsigned
An integer constant is any number in the range -32768 to +32767. This is because an integer constant always occupies two bytes in memory.
Example:
int n,a,b;
int integer_number;

long integers cause the program to run a bit slower. The value of a long integer can vary from -2147483648 and +2147483647.
Example:
long i;
long abc;

short as well integers which need less space in memory and thus help speed up program execution. In fact, a short int is nothing but our ordinary int.
Example:
short int j;
short int height;

Sometimes, we know in advance that the value stored in a given integer variable will always be positive. In such a case we can declare the variable to be unsigned, Example:
unsigned int num_student;
ð
With such a declaration, the range of permissible integer value will shift from the range -32768 to +32767 to the range 0 to 65535. Note that an unsigned integer still occupied two bytes. In fact an unsigned int is nothing but a short unsigned int. Thus, all the following declarations are same:
short unsigned int i;
unsigned int i;
unsigned i;

There also exists a long unsigned int which has a range of 0 to 4294967295 and occupies four bytes of memory.

Chars with signed and unsigned
There are signed and unsigned chars, both occupying one byte each, but having different ranges. A signed char is same as our ordinary char and has a range from -128 to +127; whereas an unsigned char has a range from 0 to 225.
Example:
void main()
{
unsigned char ch;
for(ch = 0 ; ch = 254 ; ch++ )
cout<<'\n'<<(int)ch<<'\t'<<(char)ch;
cout<<'\n'<<(int)ch<<'\t'<<(char)ch;
}

Floats and Doubles
A float occupies four bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient then C++ offers a double data type which occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308.
Example:
float n, decimal_num;
double a, population;

If the situation demands usage of real numbers which lie even beyond the range offered by double data type then there exists a long double which can range from -1.7e4932 to +1.7e4932. A long double occupies 10 bytes in memory.
Example:
long double n, world_population;

Briefly Explanation of all data types

Data Type Range Bytes
signed char -128 to +127 1
unsigned char 0 to 255 1
short signed int -32768 to +32767 2
short unsigned int 0 to 65535 2
long signed int -2147483648 to +2147483647 4
long unsigned int 0 to 4294967295 4
float -3.4e38 to +3.4e38 4
double -1.7e308 to +1.7e308 8
long double -1.7e4932 to +1.7e4932 10

Example:
void main()
{
char c;
unsigned char d;
int i;
unsigned int j;
long int k;
unsigned long int m;
float x;
double y;
long double z;
}

Automatic Variables

Register Variables

Static Variables

External Variables

Statement Of C++

If

Syntax:
if (expression)
statement

Example:
#include
int main()
{
int value=10;
if (value > 0)
cout<<"
Abc("<endl;
return 0;
}

Example: Multiple statement within If
#include
int main()
{
int value=10;
if (value > 0) {
cout<<"The number was positive."<<
endl;
cout<<"
Abc("<endl;
}

return 0;
}

If-Else

Syntax:
if (expression)
statement
else
statement

Example
#include
int main()
{
int value=-10;
if (value > 0) // When if statement has single statement
cout<<"Abc("< else
// When else statement has single statement
cout<<"Abc("<
return 0;
}

Example: Multiple statement within If-Else
#include
int main()
{
int value=-10;
if (value > 0){ // When if statement has multiple statements
cout<<"Abc("<endl;
cout<<"The number was positive."<<
endl;
}
else { // When else statement has multiple statements
cout<<"Abc("<endl;
cout<<"The number was Negative."<<
endl;
}
return 0;
}

Nested If or If-Else

Syntax:
if (expression)
if (expression)
statement
else
statement
else
statement

Example:
#include
int main()
{
int i;
cout<<"Enter either 1 or 2 : ";
cin>>i;
if (i==1)
cout<<"You would go to heaven!";
else {
if (i==2)
cout<<"Hell was created with you in mind";
else
cout<<"How about mother earth!";
}
return 0;
}

Output:
Enter either 1 or 2 : 2
Hell was created with you in mind

If-Else Ladders

Syntax:
if (expression)
statement
else if (expression)
statement

Example:
#include
int main()
{
int day=3;
if (day==1)
cout<<"It\'s Monday";
else if(day==2)
cout<<"It\'s Tuesday";
else if(day==3)
cout<<"It\'s Wednesday";
else if(day==4)
cout<<"It\'s Thursday";
else if(day==5)
cout<<"It\'s Friday";
else if(day==6)
cout<<"It\'s Saturday";
else if(day==7)
cout<<"It\'s Sunday";
return 0;
}

Output:
It\'s Wednesday

For

Syntax:
for ( initialization_statement ; test_expression ; post_expression)
statement

Example:
#include
int main()
{
int number;
cout<<"How many times do you want to see the greeting? ";
cin>>number;
for (int n=0; n cout<<"Hello from C++."<<endl;
return 0;
}

Output:
How many times do you want to see the greeting? 4
Hello from C++.
Hello from C++.
Hello from C++.
Hello from C++.

Example: The above example is written in different way,
#include
int main()
{
int number,n=0;
cout<<"How many times do you want to see the greeting? ";
cin>>number;
for (; n cout<<"Hello from C++."<<endl;
n++;
}
return 0;
}

Nested For

Syntax:
for ( initialization_statement_1 ; test_expression_1 ; post_expression_1)
for ( initialization_statement_2 ; test_expression_2 ; post_expression_2)
statement

Example:
#include
int main()
{
int sum=0;
for (int a=1; a <=3; r++) // outer loop
{
for (int b=1; b<=2; b++) // inner loop
{
sum=a+b;
cout<<"a = "<
While

Syntax:
while (expression)
statement

Example:
#include
int main()
{
int number;
cout<<"Enter a non-zero integer to be checked: ";
cin>>number;
while (number!=0){
if (number > 0)
cout<<"\nThat integer is greater than zero."< else
cout<<"\nThat integer is less than zero."< cout<<"Enter a non-zero integer to be checked: ";
cin>>number;
}
cout<<"\nYou entered zero";
return 0;
}

Do-While

Syntax:
do
statement
while (expression)

Example:
#include
int main()
{
int values[]={1,2,3,4,5}, test, index=0;
do {
test = 5* values[index++];
}while (test < 15);
cout<<"Test = "< return 0;
}

Output:
Test = 15

Case

Syntax:
switch ( expression ) {
case value1:
statement1;
[break;]
case value2:
statement2;
[break;]
case value3:
statement3;
[break;]
................
................
default:
default_statement;
}

Example:
#include
int main()
{
int today=3;
switch (today) {
case 0:
cout<<"It\'s Monday";
break;
case 1:
cout<<"It\'s Tuesday";
break;
case 2:
cout<<"It\'s Wednesday";
break;
case 3:
cout<<"It\'s Thursday";
break;
case 4:
cout<<"It\'s Friday";
break;
case 5:
cout<<"It\'s Saturday";
break;
default:
cout<<"It\'s Sunday";
}
return 0;
}

Output:
It's Thursday

The conditional Operators

Syntax:
expression1 ? expression2 : expression3

Example:
int x, y;
cout<<"Enter two numbers: ";
cin>>x;
y = (x>5 ? 3 : 4); // if x is more than 5, y = 3
cout <<"y = "<

Output:
Enter two numbers: 6
y = 3

Break Statement

When the keyword break is encountered inside any C++ loop, control automatically passes to the first statement after the loop.

Example:
void main()
{
int num, i;
cout<<"Enter a number";
cin>>num;
i =2;
while (i<= num-1)
{
if (num % i == 0)
{
cout<<"Not a prime number";
break;
}
i++;
}
if (i == num)
cout<<"Prime number";
}

Output:
Enter a number : 3
Prime number

Continue Statement

When the keyword continue is encountered inside any C++ loop, control automatically passes to the beginning of the loop.

Example:
void main()
{
for (int i=1; i<=2; i++ )
for (int j=1; j<=2; j++)
{
if (i==j)
continue;
cout<<'\n'< }
}

Using the Exit and Abort Statement

Both the exit and abort statements terminate C++ program.

Example:
#include
#include
int main()
{
for (int n = 0; n > -5; n--) {
if (n == 0)
goto label_1;
cout<<"1 / "< }
exit(0);
label_1 : cerr<<"Ending loop to avoid dividing by 0."< return 0;
}

Goto Statement

We can use the goto statement to jump from location to location in our code.

Syntax:
goto label
label : statement

Example:
#include
#include
int main()
{
for (int n = 0; n > -5; n--) {
if (n == 0)
goto label_1;
cout<<"1 / "< }
exit(0);
label_1 : cerr<<"Ending loop to avoid dividing by 0."<<endl;
return 0;
}

Essential C++

Finding a C++ Compiler

Compilers in UNIX

The C++ compiler on UNIX systems is often named CC. If our UNIX installation doesn't have CC built in, try looking for g++, the Free Software Foundation's GNU C++ compiler.

Compiler in DOS/WINDOWS

There are several high-powered C++ compilers available in Windows, such as Borland C++, Microsoft Visual C++, Turbo C++ etc.

Macintosh Compilers

Among the best known C++ compiler for the Macintosh are the Symantec C++ compiler and the Metrowerks Code Warrior compiler.

Using Header Files

We can use the #include preprocessor directive to include header files. Header files usually contain declarations, definitions and preprocessor directives like #include to include other header files and storing such items in header files helps make us code clearer.

01 Example: To include a library's header file of C++:

#include
int main()
{
cout<<"Hello from C++."<

02 Example: To include our own created header file which is stored in same directory of source files:

#include "useheader"
int main()
{
cout<<"Hello from C++."<

namespace:

The ANSI/ISO committee decided to use namespaces for the names it made a standard part of C++ and all items declared in the iostream header file are part of the std namespace. To indicate that we want to make that namespace the default namespace, you can add the statement using namespace std like this:

Example,

#include
using namespace std;
// Display a welcome message
int main()
{
cout<<"Hello from C++."<

Comment

We can use C++ comment in our code, which is an annotation added to C++ code to make it more readable:

01 Example,

#include
using namespace std;
// Display a welcome message
int main()
{
cout<<"Hello from C++."<

02 Example,

#include
using namespace std;
/* Display a welcome message */
int main()
{
cout<<"Hello from C++."<

03 Example,

#include
using namespace std;
/* **********************
Display a welcome message
Welcome to the Matrix of C++
************************/
int main()
{
cout<<"Hello from C++."<

main() function

The main function is the point where control is passed to your program from the operating system. That is to say, the main function is where you place the code you want executed first and it's essential to have a main function in all C++ programs.

Example,

#include
int main()
{
........ ..... ......
........ ..... ......
}

cout<<" "<

cout is a special stream object in C++ that handles many of the details for us. We can pass text or numbers to cout and it'll be able to handle both by itself.

Example

#include
int main()
{
cout<<"Hello from C++."<<endl;
return 0;
}

The endl manipulator makes the output skip to the next line, and it is not necessary in the case if you're running this program in DOS, because when the program terminates the output skips to the next line when the command prompt reappears-although that's not the case in UNIX.

Return

We use the return statement to send back or return the values from functions:

Example,

#include
using namespace std;
int main()
{
cout<<"Hello from C++."< return 0;
}

The Reserved C++ keywords

As we create our own C++ programs, we'll be creating our own names and those names should not conflict with terms that C++ has reserved for its own use.

Reserved C++ Keywords.

Preprocessor Directives

Preprocessor Directives are handled before the code is passed to the C++ compiler converting the C++ code to C code, which was then passed on to the C compiler. Here are the allowed Preprocessor Directives in standard C++: Click here.

Example: We can use these preprocessor directives with predefined tokens like _cplusplus, which is defined if the current program is C++ and undefined otherwise.

#ifdef _cplusplus
cout <<"This is a C++ program."<#endif

Here are some predefined tokens we can use in our code: Click here

Using Character Escape Sequences

Placing a backslash "\" in front of a character makes C++ look for a character escape sequence and treat it in a special way.

C++ character escape sequences

\a Bell (alert)
\b Backspace
\f Form feed
\n New Line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\" Double quotation mark
\\ Backslash
\? Literal question mark
\000 Character code in octal notation
\xhhh Character code in hexadecimal notation

Example

#include
#include
//read and display data
int main()
{
string input;
cout<<"Type a word: ____\b\b\b\b"; cin>>input;
cout<<"You typed: ";