Chapter 7 Classes and Objects (1) (4)
Chapter 7 Classes and Objects (1) (4)
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:
AVINASH C Page 1
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
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 information hiding.
• 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.
AVINASH C Page 2
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
➢ 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.
o Inside class definition Important
o Outside class definition 5 Marks
✓ Inside class definition:
• To define member function inside a class the function declaration within the class is replaced by
actual function definition inside the class.
• A function defined in a class is treated as inline function.
• Only small functions are defined inside class definition.
• Example:
class rectangle
{
int length, breadth, area;
public:
void get_data( )
{
cout<< ” Enter the values for Length and Breadth”;
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
cout<<” The area of rectangle is”<<area;
}
};
AVINASH C Page 3
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
AVINASH C Page 4
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
AVINASH C Page 5
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
• 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);
}
AVINASH C Page 6
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
• Example:
class rectangle
{
int length, breadth, area;
public:
void get_data( )
{
cout<<”Enter the length and breadth”<<end;
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
cout<<”The area of rectangle is “<<area;
}
};
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( )
{
cout<<”Enter “<<m<<”Array elements”<<endl;
for(int i=0; i<m; i++)
cin>> a[i];
}
void array : : displayarray( )
{
cout<<”Array elements are:”<<endl;
for(int i=0; i<m i++)
cout<< a[i]<<”\t”;
}
void main( )
{
int n;
array a;
clrscr( ); OUTPUT:
cout<<”Input number of elements:”<<endl; Input number of elements: 5
cin>>n;
Enter 5 Array elements
a.setnoofelements(n);
a.readarray( ); 10 20 30 40 50
a.dispalyarray( ); Array elements are:
getch( );
10 20 30 40 50
}
AVINASH C Page 8
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
AVINASH C Page 9
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
private:
AVINASH C Page 10
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
AVINASH C Page 11
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
private:
float phy, che, mat ;
public:
void readdata( )
{
cout<<”Input Physics, Chemistry, Maths marks : ” ;
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( )
{
cout<< “Physics :” <<phy<<endl;
cout<< “Chemistry :” <<che<<endl;
cout<< “Maths :” <<mat<<endl;
}
};
void main( ); OUTPUT:
{
Enter PUC Marks
Exam PUC, CET, Puc_plus_Cet;
Input Physics, Chemistry, Maths marks
clrscr( );
: 67 89 80
cout<<”Enter PUC Marks”<<endl;
Enter CET Marks
PUC.readdata( );
Input Physics, Chemistry, Maths marks
cout<<”Enter CET Marks”<<endl;
: 60 76 91
CET.readdata( );
Total marks of PUC and CET is:
Puc_plus_Cet.total(PUC, CET);
Physics: 127
cout<<”Total marks of PUC and CET is:” <<endl;
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.
AVINASH C Page 12
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
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( )
{
cout<<”Enter the Principal Amount, Time & Rate”<<endl;
AVINASH C Page 13
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
cin>>p>>t>>r;
}
void compute( )
{
si = (p * t * r)/100;
}
void display( )
{
cout<<”Simple Interest = “<<si;
}
};
void main( )
{
SI s;
clrscr( );
s.readdata( );
ompute( );
isplay( );
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( )
{
cout<<”Enter the Item Code”<<endl;
cin>>itemcode;
cout<<”Enter the Price”<<endl;
cin>>price;
cout<<”Enter the Quantity”<<endl;
cin>>quantity;
}
AVINASH C Page 14
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
void display( )
{
cout<<itemcode<<”\t”<<price<<”\t”<<quantity<<endl;
}
};
void main( )
{
int N=0;
char ans;
product list[100];
clrscr( );
while(1)
{
cout<<”Item Code, Price and Quantity”<<endl;
List[N].Addproduct( );
cout<<”Do you want to add next item (Y/N)?”<<endl;
cin>>ans;
if(toupper(ans) == ‘N’)
break;
N++;
}
cout<<”Item Code \t Price \t Quantity”<<endl;
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( )
AVINASH C Page 15
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
{
cout<<”Hours = “<<hh;
cout<<”Minutes = “<<mm;
}
void convert( )
{
mm = hh * 60 + mm;
cout<<”Total Minutes = “<<mm;
}
};
void main( )
{
int h, m;
clock c;
clrscr( );
cout<<”Enter the Hour and Minutes”<<endl;
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( )
{
AVINASH C Page 16
Chapter 7- Classes and Objects II PUC, SBMJC, V V PURAM BANGALORE
cout<<”Enter the Arrival Time:”<<endl;
cout<<”Enter the Hour and Minutes”<<endl;
cin>>Ahh>>Amm;
cout<<”Enter the Departure Time:”<<endl;
cout<<”Enter the Hour and Minutes”<<endl;
cin>>Dhh>>Dmm;
cout<”Enter the speed in Kmph”<<endl;
cin>>speed;
}
void computedistance( )
float dist;
dist = ( (Ahh * 60 + Amm) – (Dhh * 60 + Dmm) ) * speed/60;
dist = (dist * 1000 / (60 * 60);
cout<<”Distance Travelled = “<<dist<<”Meter/Second”;
};
void main( )
{
Distance d;
clrscr( );
d.inputtime( ):
d.computedistance( );
getch( );
AVINASH C Page 17