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 : "<
cout<<"Second Complex Number : "<
C3.AddComplex(C1, C2);
cout<<"Sun of Two Complex Numbers : "<
}
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 : "<
cout<<"Second Complex Number : "<
C3=C1.AddComplex(C2);
cout<<"Sun of Two Complex Numbers : "<
}
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;i
cout<<"IdNo"<
}
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 "<
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 "<
}
Output
Value using object 340
Value using pointer 340