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