C++ Classes and Objects Notes
C++ Classes and Objects Notes
Classes and Objects:Class:A class is a user defined data that contained data as well as function together.
or
A class is a way to bind the data and function together in a single unit.
or
A class is a group of objects that share common properties and relationships
Note:1. The variables and functions enclosed in a class are called data member and member
functions.
2. The variable of class are called objects or instance of a class .
3. Classes are basic user defined data type of object oriented programming language
4. The difference between a class and structure is that ,by default ,all the members of a
class are private while by default ,all the members of structure are public
Note:- The data members and member functions of class are grouped under
three sections.
1. private
2. protected
3. public
Explanation of private ,protected & public access modifier:private :By private we mean that members can be accessed only from within the class i.e member
data can be accessed by the member function .
The private data members are not accessible to the outside worls (i.e outside the class)
By default members of a class are private.
Private members are not inheritable.
Protected:The members which are declared as protected ,can only be accessed by member functions
and friends function function of that class.
37
Protected members are similer to private members ,the only difference is that protected
members are inheritable .
Public:The members which are declared as public can be accessed from out side classes also.
Note:- By default ,the members of a class are private .if both the lables ,are missing,then by
difault ,all the members are private .Such a class is completely hidden from outsite worls.
Example:class Student
{
int rollno;
float marks;
public:
void getdata(int x,flot y);
void display(void);
};
Array of Objects:- An array of variables that are of the type Class .such variables are
called array of objects.
Example.
class employee
{
char name[50];
int age;
public:
void getdata(void);
void showdata(void);
};
The employee is a user defined data type and can be used to create objects that relate to
different categories of employees.
employee manager[4];
employee engineer[15];
employee workers[200];
Accessing Class Members:- After creating the objects there is a need to access the class
members .This can be done using ( . ) operator .
Syntax:
Objectname . Datamember
Objectname.memberfunction
As:
S1.getdata(129,100)
s1.display()
s1.rollno=129 // illegal statement
Note:- A variable declared public can be accessed by the objects directly.
//Example for public data members
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
class date
{
public:
int day;
int month;
int year;
};
date d1;
d1.day=26;
d1.month=6;
d1.year=2012
cout<< Date is=<<d1.day<</ <<d1.months<</ <<d1.year<<endl;
getch();
}
Example 2:-Write a program to make use of simple arithmetic operations such as
addition,subtraction,multiplication division using the concept class.
#include <iostream.h>
#include<conio.h>
class calculator
{
private :
int x;
int y;
public :
void getdata();
{
cout<< Enter the two numbers: << endl;
cin>> x >> y;
}
void display()
39
{
cout<<x=<<x<<endl;
cout<<y=<<y<<endl;
}
void sum()
{
Member function outside the class definition:Note:- 1. declare function prototype within the body of a class and then define it outside the
body of class.
2. By using scope resolution operator ( : : ) we bind the function to the class.
Syntax:Return type classname :: functionname(argument declaration)
{
function body
}
40
Example:
#include<iostream.h>
#include<conio.h>
class product
{
int qty;
float cost;
Public:
void getdata(int a, float b);
void putdata(void);
};
void product :: getdata(int a,float b)
{
qty= a;
cost=b;
}
void product :: putdata(void)
{
cout<< qty=<< qty<<endl;
cout<<cost= << cost<<endl;
}
void main()
{
clrscr();
product p;
cout<< The object ps is << endl;
p.getdata(75,255.60);
p.putdata();
product Q;
cout<< The object Q is = << endl;
Q.getdata(100,125.25);
Q.putdata();
getch();
}
Note:- label product:: informs the compiler that the functions getdata & putdata are the
2. Member function inside the class definition.
Note:- When a function is defined inside a class ,it is treated as inline function.normally
small functions are defined inside the class definition.
Example:#include<iostream.h>
#include< conio.h>
class date
{
private:
int day;
int month;
int year;
41
public:
void getdata(int x,int y,int z)
{
day=x;
month=y;
year=z;
}
void display()
{
cout<< day<<month<<year <<endl;
}
}
Void main()
{
clrscr();
date d1,d2,d3;
d1.getdata(30,12,2012);
d2.getdata(23,11,2011)
d3.getdata(24,05,2010);
cout<< you have entered following date;
d1.display();
d2.display();
d3.display();
getch();
}
Array of Objects:- An array of variables that are of the type Class .Such variables are called
array of objects.
Example.
class employee
{
char name[50];
int age;
public:
void getdata(void);
void showdata(void);
};
The employee is a user defined data type and can be used to create objects that relate to different
categories of employees.
employee manager[4];
employee engineer[15];
employee workers[200];
Example:#include<iostream.h>
#include<conio.h>
class employee
{
char name[50];
42
int age;
public:
void getdata(void);
void showdata(void);
};
void employee:: getdata(void)
{
cout<<enter name=;
cin>>name;
cout<<enter age=;
cin>>age;
}
void employee::showdata(void)
{
cout<<name<<endl<<age;
}
void main()
{
clrscr();
int size=4;
employee manager[size];
for(int i=0; i<size;i++)
{ cout<<Enter Details of managers;
manager[i].getdata();
}
cout<<endl;
for(i=0;i<size;i++)
{
cout<<Details of Managers is=;
manager[i].putdata();
}
getch();
}
Friend Function:- A friend function is not a member function ,has full access rights to the
private members of the class.
Syntax :class ABC
{
----------------------public:
--------------------------friend void xyz(void);
}
43
{
if (m.x>=n.a)
cout<<m.x;
else
cout<<n.x;
}
void main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
}
void indata(int a)
{
value2=a;
}
void display(void )
{
cout<<value2<<\n;
}
friend void exchange (class _1 t1, class_2 t2);
};
void exchange(class_1 &T1,class_2 &T2)
{ int temp;
temp=T1.value1;
T1.value1=T2.value2;
T2.value2=temp;
}
void main()
{
class_1 C1;
class_2 C2;
C1.indata(100);
C2.indata(200);
cout<< values before exchange <<\n;
C1.display();
C2.display();
exchange(C1,C2);
cout<< values after exchange<<\n;
C1.display(); C2.display(); }