Unit: 2
Objects and Classes
Trim II, MBA Tech
Div A
Subject: CP
Subject Teacher: Mrs. Preeti Patil
Data Hiding and Encapsulation
• The Wrapping up of data and functions into a
single unit is known as Encapsulation.
• Data encapsulation is the feature of a class.
• The data is not accessible to the outside
world, and only those functions which are
wrapped in the class can access it.
• These functions provide the interface
between objects data and the program.
• This insulation of the data from direct access
by the program is called data hiding or
information hiding.
12/5/2009 MBA TECH, Trim_II Subject: C.P 2
Specifying a Class
• Class is a way to bind the data and its
associated functions together.
• It allows the data and functions to be hidden
from external use.
• A Class specification has two parts
– Class Declarations
– Class Function definitions
12/5/2009 MBA TECH, Trim_II Subject: C.P 3
Class declarations
General form of class declaration is:
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 4
• The class body contains the declaration of variables
and functions.
• These functions and variables are collectively called
class members.
• Class members are grouped under two sections,
namely, private and public to denote the visibility of
members
• The variables defined inside the class are called as
data members.
• The functions defined inside the class are called as
member functions.
12/5/2009 MBA TECH, Trim_II Subject: C.P 5
Class Example
class item
{
int number; // by default the variables are private
float cost;
public:
void getnumber(int a, float cost);
void putdata(void);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 6
Creating Objects
item x;
• Creates variable x of type item.
• In C++, the class variables are called as
Objects.
• The necessary memory space is allocated to
an object at this stage.
12/5/2009 MBA TECH, Trim_II Subject: C.P 7
• Objects can be created when a class is defined
as shown
class item
{
----
----
} x,y,z;
• Creates the objects x,y,z of type item.
12/5/2009 MBA TECH, Trim_II Subject: C.P 8
Accessing class members
• The private data of a class can be accessed
only through the member functions of that
class.
Object_name.function_name(actual_arguments);
Eg:
x.getdata(100,75.5);
12/5/2009 MBA TECH, Trim_II Subject: C.P 9
Defining member functions
• Can be defined in two ways
– Outside the class definition.
– Inside the class definition.
• Outside the class definition:
return_type class_name :: function_name(argument_declaration)
{
function_body
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 10
Eg: for function declaration outside the
class
void item :: getdata(int a, float b)
{
number=a;
cost= b;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 11
Inside the class definition
• Replace the function declaration with the actual
function definition.
• It is treated as inline function.
• Eg:
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{ cout<<number<<“\n”<<cost<<“\n;}
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 12
WAP to illustrate the uses of
classes and objects
Class definition
#include<iostream.h>
Class item
{
int number;
float cost;
public:
void getdata(int a, float b);
Void putdata(void)
{
cout<<“number:”<<number<<“\n”;
cout<<“cost :”<<cost<<“\n”;
}
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 14
Define member function getdata()
void item::getdata(int a, float b)
{
number=a;
cost=b;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 15
Main( ) function
int main()
{
item x;
cout<<“ operation on Object x”<<“\n”;
x.getdata(100,288.34);
x.putdata();
item y;
cout<<“ operation on Object y”<<“\n”;
y.getdata(40,300.34);
y.putdata();
return(0);
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 16
Output of the program
operation on Object x
number: 100
cost :288.34
operation on Object y
number: 40
cost :300.34
12/5/2009 MBA TECH, Trim_II Subject: C.P 17
Memory allocation for objects
• Member functions are created and placed in
memory space only once when they are defined as a
part of a class definition.
• All the objects of the class use the same member
functions, no separate space is allocated for member
functions when the objects are created
• Space for member variables are allocated separately
for each object.
• Member variables will hole different values for
different objects.
12/5/2009 MBA TECH, Trim_II Subject: C.P 18
Static Data Members
• It is initialized to zero when the first object of
its class is created. No other initialization is
permitted.
• Only one copy of the member is created for
the entire class and is shared by all the objects
of that class, no matter how many objects are
created.
12/5/2009 MBA TECH, Trim_II Subject: C.P 19
Static Data Members
• It is visible only in the class, but its lifetime is
the entire program.
• Static variables are normally used to maintain
values common to the entire class.
12/5/2009 MBA TECH, Trim_II Subject: C.P 20
WAP to illustrate the use of
Static Data Member.
Defining the class definition
#include<iostream.h>
class item
{
static int count;
int number;
public:
void getdata(int a) { number=a; count++;}
void getcount(void){ cout<<“count”<<count<<“\n”;}
};
int item:: count;
12/5/2009 MBA TECH, Trim_II Subject: C.P 22
Main() function
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100); // getting data into object a
b.getdata(200); // getting data into object b
c.getdata(300); // getting data into object c
cout<<“ count value after reading data”<<“\n”;
a.getcount();
b.getcount();
c.getcount();
return 0;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 23
Output of the program
count=0;
count=0;
count=0;
count value after reading data
count=3;
count=3;
count=3;
12/5/2009 MBA TECH, Trim_II Subject: C.P 24
Static member functions
• A static function can have access to only other
static members(functions or variables)declared
in the same class.
• A static member function can be called using
the class name(instead of its objects) as
follows
class_name :: function_name;
12/5/2009 MBA TECH, Trim_II Subject: C.P 25
WAP to illustrate Static member
functions
Class definition
#include<iostream.h>
Class test
{
int code;
static int count;
public:
void setcode(void){code=++count;}
void showcode(void)
{cout<<“Object number”<<code<<“\n”;}
static void showcount(void)
{ cout<<“count: “<<count<<“\n”;}
};
int test::count;
12/5/2009 MBA TECH, Trim_II Subject: C.P 27
Main() function
int main()
{ test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 28
Output of the program
count: 2
count: 3
Object number: 1
Object number: 2
Object number: 3
12/5/2009 MBA TECH, Trim_II Subject: C.P 29
Arrays of Object
• Arrays of variables that are of type class are called arrays of
objects.
• Eg:
class student
{ int rollno;
int marks;
public:
void getdata();
void putdata();
};
• The objects created as
Student BTECH[50];
Student MBATECH[20];
here BTECH[50] is an array of object of type Student
here MBATECH[20] is an array of object of type Student
12/5/2009 MBA TECH, Trim_II Subject: C.P 30
WAP to create Array of Objects
Class definition
#include<iostream.h>
Class Student
{
int rollno;
int marks;
public:
void getdata(void);
void putdata(void);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 32
Define getdata() function
void student :: getdata(void)
{ cout<<“Enter Rollno”<<“\n”;
cin>> rollno;
cout<<“Enter marks”<<“\n”;
cin>>marks;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 33
Define putdata() function
void Student:: putdata(void)
{
cout<<“ Rollnumber”<<rollno;
cout<<“Marks”<<marks;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 34
Define main() function
int main()
{
Student Btech[3];
for(int i=0;i<3;i++)
{ cout<<“ Read Btech Students Detail”;
Btech[i].getdata();
}
for(int j=0j<3;j++)
{ cout<<“\n Btech Details are as follows”;
Btech[i].putdata();
}
return 0;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 35
Output of the program
Read Btech Students Detail
Enter Rollno: 1
Enter Marks:25
Enter Rollno: 3
Enter Marks:23
Enter Rollno: 5
Enter Marks:24
12/5/2009 MBA TECH, Trim_II Subject: C.P 36
Btech Details are as follows:
Rollno:1
Marks:25
Rollno:3
Marks:23
Rollno:5
Marks:24
12/5/2009 MBA TECH, Trim_II Subject: C.P 37
Objects as function arguments
• An Object can be used as function argument
in two ways:
– A copy of entire Object is passed to the
function.(Pass_by_Value)
– Only the address of the object is transferred to the
function.(Pass_by_Reference)
12/5/2009 MBA TECH, Trim_II Subject: C.P 38
Eg. For passing objects as arguments
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m){hours=h;minutes=m;}
Void puttime(void)
{cout<<hours<<“hrs &”<<minutes<<“mins”<<“\n”;}
Void sum(time,time);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 39
Defining sum function
Void time::sum(time t1,time t2) //outside class
{
minutes=t1.minutes+t2.minutes;
hours=t1.hours+t2.hours;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 40
Main( ) function
int main()
{
time t1,t2,t3;
t1.gettime(2,45);
t2.gettime(3,30);
t3.sum(t1,t2);
cout<<“t1= “; t1.puttime();
cout<<“t2= “; t2.puttime();
cout<<“t3= “; t3.puttime();
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 41
Output for the program
T1= 2 Hrs & 45 min
T2= 3 Hrs & 30 min
T3= 6 Hrs & 15 min
12/5/2009 MBA TECH, Trim_II Subject: C.P 42
Friend Function
• The functions that are declared as friend are
known as friend functions.
• A friend function, although not a member
function, has all access rights to the private
members of the class.
12/5/2009 MBA TECH, Trim_II Subject: C.P 43
WAP to show how friend function
works as a bridge to two classes
Declare class ABC
#include<iostream.h>
Class ABC
Class XYZ
{
int x;
public:
void setvalue(int i){x=I;}
friend void max(XYZ,ABC);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 45
Declare class ABC
Class ABC
{
int a;
public:
void setvalue(int i){a=I;}
friend void max(XYZ,ABC);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 46
Definition of friend function
void max(XYZ m,ABC n)
{
if(m.x >=n.a)
cout<<m.x;
else
cout<<n.a;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 47
Main () function
int main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
return 0;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 48
Returning Objects
• A function cannot only receive objects as
arguments but can also return them.
• Illustrated with example.
12/5/2009 MBA TECH, Trim_II Subject: C.P 49
Class definition
Class complex
{
float x;
float y;
public:
void input(float real,float imag)
{ x=real; y=imag;}
friend complex sum(complex,complex);
void show(complex);
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 50
Defining sum function
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 51
Show function defined outside class
void complex:: show(complex c)
{
cout<<c.x<<“+j”<<c.y<<“/n”;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 52
Main() function
int main()
{
complex A,B,C;
A.input(3.1,5.65);
B.input(2.75,1.2);
C=sum(A,B);
cout<<“A=“;A.show(A);
cout<<“B=“;B.show(B);
cout<<“C=“;C.show(C);
Return 0;
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 53
output
A=3.1+ j5.65
B=2.75+j1.2
C=5.85+J6.85
12/5/2009 MBA TECH, Trim_II Subject: C.P 54
Friend class
• when a class is declared as a friend, the friend
class can have access to the private data of the
class which made this a friend.
12/5/2009 MBA TECH, Trim_II Subject: C.P 55
Friend class
#include < iostream.h >
class A
{
int var1;
friend class friendclass;
public:
A()
{
var1 = 5;
}
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 56
Defining friend class
class friendclass
{
public:
int subtractfrom(int x)
{
A obj;
return obj.var1 - x;
}
};
12/5/2009 MBA TECH, Trim_II Subject: C.P 57
Main function
int main()
{
friendclass var3;
cout << “Result of the subtraction is: "<<
var3.subtractfrom(2)<
}
12/5/2009 MBA TECH, Trim_II Subject: C.P 58
Output of the program
Result of the subtraction is: 3
12/5/2009 MBA TECH, Trim_II Subject: C.P 59
End of UNIT 2.