Chap -7- Classes & Objects
Chap -7- Classes & Objects
7
Class & Objects
Defining a class
Body of the class is enclosed in a pair of curled braces. Class body contains the declaration of
class members (data members and member functions). There are generally three types of
members namely private, public and protected. These are called as access specifiers.
Example class account
{
private: //implicit by default
int accno;
char name[20[;
char acctype[4];
int bal_amt;
public: //member functions
void get_data(); void
displaydata();
};
Program to find the sum of two numbers
#include<iostream.h>
#include<conio.h>
class Test
{
private:
int a, b;
public:
void getnum()
{
cout<<"Enter Numbers for Addition: "; cin>>a>>b;
}
void showdata()
{
cout<<"Sum of "<<a<<" and "<<b<<" = "<<a+b;
};
❖ If data and member functions are declared without access specifier, then, members
are private, by default.
For example: private: int x; Float y;
Public
❖ Data members and data functions can be accessed from outside the class.
❖ Generally, the data within class is private and the functions are public.
❖ The data is hidden to prevent accidental manipulation from outside the class.
❖ Data can be made public so that it can be accessed outside the class.
Class
Private
Data
or Not accessible
Function from
outside class
Public
Data Accessible
or from
Function outside class
2 Savitha Parthasarathy
Chapter - 7 Class and Objects
Protected
❖ This access specifier is used in inheritance.
❖ The members which are declared protected can be accessed only by the member
functions, friends of the class and also by the member functions derived from this
class.
Creating objects
Once the class is defined, objects (or variable) of that class can be created.
Syntax : class-name objects;
Example: employee e1, e2;
❖ Private and protected members of a class can be accessed only through the member
functions of that class.
❖ The public data members and member functions of objects of a class can be accessed
using direct member access operator (.)
Syntax: class-object. member-data
Example: e1.basic = 5000;
❖ When the member function is declared outside the class, the class contains the
prototype of the function.
❖ When the member function is declared outside the class, the member functions
contain the name of the class it belongs to.
Savitha Parthasarathy 3
Chapter - 7 Class and Objects
The syntax is
{
Statement 1;
statement 2;
-----
}
Here, class-name Name of the class that informs the compiler that the
Function belongs to a particular class.
:: Scope resolution operator shows that the scope of the
Function is restricted to the class_name
4 Savitha Parthasarathy
Chapter - 7 Class and Objects
❖ The same function name can be used in several different classes.
❖ Member functions are allowed to access private data members of that class.
❖ A function can call another function directly without using dot operator.
#include<iostream.h>
#include<conio.h>
class interest
{
private:
double p, t, r, si;
public:
void getdata();
void compute();
void putdata();
};
void interest::getdata()
{
cout<<"Enter principle amount, time and rate"<<endl;
cin>>p>>t>>r;
}
void interest::putdata()
{
cout<<"Principle: " <<p ;
cout<<"Time: "<<t;
cout<<"Rate: "<<r;
cout<<"Simple interest: "<<si;
}
Enter principle amount, time and rate: 1000 1.0 1
void interest::compute() Principle: 1000
{ Time: 1
si = (p*t*r)/100; Rate: 1
} Simple interest: 10.000
void main()
{
interest 0;
clrscr();
cout<<setprecision(8);
o.getdata();
o.compute();
o.putdata();
getch();
}
❖ In OOP, it is good practice to define the member functions outside the class
definition.
❖ When member functions are defined inside the class definition, the member
functions are called as inline functions.
❖ Only small and simple functions are defined inside the class.
Savitha Parthasarathy 5
Chapter - 7 Class and Objects
Program to find simple interest . Refer class work ..
Or
#include<iostream.h>
#include<ioman
ip.h> class
temparature
{
private:
float fahr;
float centigrade;
public:
void getdata()
{
cout<<"Enter the temperature in Fahrenheit: ";
cin>>fahr;
}
float convert()
{
centigrade = 1.8*fahr+32;
}
void display()
{
cout<<setprecision(5);
cout<<"Temperature in Centigrade: "<<centigrade;
}
};
in t main()
{
temparature T;
T.getdata();
Enter the temperature in Fahrenheit: 32
T.convert();
T.display(); Temperature in Centigrade: 89.6
return 0;
}
Thus, when a member function calls another member function of same class, it is called
as nested functions.
6 Savitha Parthasarathy
Chapter - 7 Class and Objects
};
void largest::getdata()
{
cout<<"Enter two numbers:
"; cin>>a>>b;
}
int largest::findlarge()
{
if(a>b)
return a;
else
return b;
}
void largest::display()
{
cout<<"Largest = "<<findlarge();
};
int main()
{
largest L; Enter two numbers: 20 10
L.getdata(); Largest = 20
L.display();
return 0;
}
Savitha Parthasarathy 7
Chapter - 7 Class and Objects
cout<<"Enter the four subjects marks: ";
f o r (in t i= 0 ; i< 4 ; i+ + )
cin>>marks[i];
}
void student::showdata()
{
int total = 0;
void main()
{
student
S;
S.getdat
a();
S.showd
ata(); }
Array of objects
✓ An array is collection of elements where all the elements are same data type.
✓ The array elements can be of type class also. Such arrays are called as array of
objects.
✓ Any possible operation performed on array can also be performed on array of objects
also.
8 Savitha Parthasarathy
Chapter - 7 Class and Objects
Savitha Parthasarathy 9
Chapter - 7 Class and Objects
Pass by value:
a. Copy of the object is passed to the function
b. The function creates another copy of the object and uses it.
c. Changes made to the object inside the function do not affect the original
object.
Pass by reference:
b. This means changes made to the object inside the function will reflect in the
original object.
Note:
You can use Lab program #6(Simple interest) as an example for Class , Object , Access
Specifiers and also for inside the class member function …
10 Savitha Parthasarathy