Chapter 7 Classes and Objects
Chapter 7 Classes and Objects
Chapter-7
Classes:
A class is a collection of objects that have identical properties, common behavior and shared
relationship.
A class binds the data and its related functions together.
Key word class is used to declare a class. User_Defined_Name is the name of the class.
Class body is enclosed in a pair of flower brackets. Class body contains the declaration of its
members (data and functions).
There are generally three types of members namely private, public and protected.
Example: Let us declare a class for representation of bank account.
class account
{
private:
1|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
int accno;
char name[20];
char acctype[4];
int bal_amt;
public:
void get_data( );
void display_data( );
};
Access Specifiers:
Every data member of a class is specified by three levels of access protection for hiding data and
function members internal to the class.
They help in controlling the access of the data members.
Different access specifiers such as private, public, and protected.
private:
private access means a member data can only be accessed by the class member function or friend
function.
The data members or member functions declared private cannot be accessed from outside the
class.
The objects of the class can access the private members only through the public member functions
of the class. This property is also called .
By default data members in a class are private.
Example:
private:
int x;
float y;
protected:
The members which are declared using protected can be accessed only by the member functions,
friend of the class and also the member functions derived from this class.
The members cannot be accessed from outside the class.
The protected access specifier is similar to private access specifiers.
public:
public access means that member can be accessed any function inside or outside the class.
Some of the public functions of a class provide interface for accessing the private and protected
members of the class.
2|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
Member Function:
Member functions are functions that are included within a class (Member functions are also called
Methods).
Member functions can be defined in two places.
Inside class definition
Important
Outside class definition 5 Marks
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
area;
}
};
3|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
cout<<
}
};
4|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
5|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
Example 2:
class num
{
private :
int x, y;
public :
int sum(int p, int q)
int diff(int p, int q)
};
void main( )
{
num s1, s2;
s1.sum ( 200,300);
s2.diff (600, 500);
}
6|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
Example:
class rectangle
{
int length, breadth, area;
public:
void get_data( )
{
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
}
};
void main( )
{
rectangle r1;
OUTPUT:
clrscr( );
r1.get_data( ); Enter the length and breadth
r1.compute( ); 30 10
r1.display( );
The area of rectangle is 300
getch( );
}
void readarray( );
void displayarray( );
};
void array : : readarray( )
{
8|Page
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
cin>>regno;
cout<<
cin>>maths;
cin>>computer;
dipalay( );
}
void data : : average( );
{ OUTPUT:
int avg;
avg = (maths+computer)/2; Enter Register No: 20
} Enter Maths marks: 56
void data : : display( ) Enter Computer marks: 78
{ Average = 67
Enter Register No: 22
}
void main( ) Enter Maths marks: 56
{ Enter Computer marks: 77
data stude[3]; Average = 66
clrscr( ); Enter Register No: 10
for(i=0; i<3; i++) Enter Maths marks: 44
stud[i]. readdata( ); Enter Computer marks: 89
getch( );
Average = 66
}
private:
float phy, che, mat ;
public:
void readdata( )
{
cin>>phy>>che>>mat;
}
void total(exam PU , exam CT)
{
phy = PU.phy + CT.phy;
che = PU.che + CT.che;
mat = PU.mat + CT.mat;
}
void display( )
{
<phy<<endl;
}
};
void main( ); OUTPUT:
{
Enter PUC Marks
Exam PUC, CET, Puc_plus_Cet;
Input Physics, Chemistry, Maths marks :
clrscr( );
67 89 80
Enter CET Marks
PUC.readdata( );
Input Physics, Chemistry, Maths marks :
60 76 91
CET.readdata( );
Total marks of PUC and CET is:
Puc_plus_Cet.total(PUC, CET);
Physics: 127
Total marks of
Chemistry: 165
Puc_plus_Cet.display( );
} Maths: 171
In pass by reference, when an address of an object is passed to the function, the function directly
works on the original object used in function call.
This means changes made to the object inside the function will reflect in the original object,
because the function is making changes in the original object itself.
Pass by reference is more efficient, since it requires only passing the address of the object and not
the entire object.
11 | P a g e
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
IMPORTANT QUESTIONS:
1 Mark questions:
1. What is a Class, Objects, Data Member, Member Functions, Scope Resolution Operator, and
Array of objects?
2. Mention the access specifiers used with a class?
5 Mark questions:
1. Explain class definitions and class declaration with syntax and example.
2. Explain Member function.
a. Inside class definition
b. Outside class definition
3. Explain the array of objects. Important
5 Marks
Exercise programs
1. Write a C++ program to find the simple interest using class and objects.
#include<iostream.h>
#include<conio.h>
class SI
{
private:
float p, t, r, si;
public:
void readdata( )
{
12 | P a g e
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
cin>>p>>t>>r;
}
void compute( )
{
si = (p * t * r)/100;
}
void display( )
{
;
}
};
void main( )
{
SI s;
clrscr( );
s.readdata( );
s.compute( );
s.display( );
getch( );
}
2. Let product list be a linear array of size N where each element of the array contains
following field Itemcode, Price and Quantity. Declare a class Product list with three data
members and member functions to perform the following
a. Add values to the product list
b. Printing that total stock values
#include<iostream.h>
#include<conio.h>
#include<iomainp.h>
class product
{
private:
char itemcode[6];
float price, quantity;
public:
void Addproduct( )
{
Item Code
cin>>itemcode;
Price
cin>>price;
Quantity
cin>>quantity;
}
13 | P a g e
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
void display( )
{
cout<< \ \
}
};
void main( )
{
int N=0;
char ans;
product list[100];
clrscr( );
while(1)
{
List[N].Addproduct( );
cin>>ans;
if(toupper(ans)
break;
N++;
}
\t Price \t Quantity
for(i=0; i<N; i++)
List[i].display( );
getch( );
}
3. A class cock has following member hours and minutes. Create member function
a. To initialize the data members
b. Display the time
c. To convert hours and minutes to minutes.
#include<iostream.h>
#include<conio.h>
class clock
{
private:
int hh, mm;
public:
void initialize( int h, int m)
{
hh = h;
mm = m;
}
void display( )
{
14 | P a g e
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
<mm;
}
void convert( )
{
mm = hh * 60 + mm;
}
};
void main( )
{
int h, m;
clock c;
clrscr( );
cin>>h>>m;
c.intialize( );
c.display( );
c.convert( )
getch( );
}
4. Write a C++ program that receives arrival time and departure time and speed of an
automobile in kilometers/hours as input to a class. Compute the distance travelled in
meters/second and display the result using member functions.
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int Ahh, Amm, Dhh, Dmm;
float speed;
public:
void inputtime( )
{
cin>>Ahh>>Amm;
<endl;
cin>>Dhh>>Dmm;
cin>>speed;
}
void computedistance( )
15 | P a g e
Chapter 7- Classes and Objects II PUC, MDRPUC, Hassan
{
float dist;
dist = ( (Ahh * 60 + Amm) (Dhh * 60 + Dmm) ) * speed/60;
dist = (dist * 1000 / (60 * 60);
}
};
void main( )
{
Distance d;
clrscr( );
d.inputtime( ):
d.computedistance( );
getch( );
}
**************
16 | P a g e