0% found this document useful (0 votes)
5 views

Chap -7- Classes & Objects

Uploaded by

purvikablore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chap -7- Classes & Objects

Uploaded by

purvikablore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter

7
Class & Objects

Class is a user-defined data type.


Class is collection of similar objects.
A class binds data members and member functions together.

Defining a class

Syntax: class class_name


{
private:
Data members;
Member functions;
protected:
Data members;
Member functions;
public:
Data members;
Member functions;
};

Here, class - Keyword used to declare a class


class_name - Name of the 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;
};

Savitha Parthasarathy , HOD , SSMRV PU College 1


Chapter - 7 Class and Objects
void main()
{
Test o; //T is the object
clrscr();
o.getnum();
o.showdata(); Enter Numbers for Addition: 10 20
getch(); Sum of 10 and 20 = 30
}
Access specifiers

❖ An object is defined to be an instance of a class.

❖ Every data member of a class is specified by three levels of access protection


for hiding data.

❖ The access specifiers define the scope of data.

❖ There are three access specifiers – private, public and protected


Private
❖ The class members (data and functions) that are declared as private can be
accessed only within the function.

❖ 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

Example : Program to find Simple Interest, Refer Class work….

2 Savitha Parthasarathy
Chapter - 7 Class and Objects
Protected
❖ This access specifier is used in inheritance.

❖ We know that the private members of a class cannot be inherited.

❖ It can be inherited by making public which is against OOP.

❖ This problem can be solved by declaring it as protected.

❖ 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.

❖ The members cannot be accessed from outside.

❖ The protected access specifier is therefore similar to private specifier.

Creating objects
Once the class is defined, objects (or variable) of that class can be created.
Syntax : class-name objects;
Example: employee e1, e2;

Accessing class members

❖ Private and protected members of a class can be accessed only through the member
functions of that class.

❖ It can be accessed as follows:

Syntax : Object_name.function_name(actual arguments);


Example: student.getdata();

❖ 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;

Defining member functions:


Member functions of a class can be defined in two ways:
1. Outside the class definition
2. Inside the class definition

Defining member function outside the class definition

❖ 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

return_type_specifier class_name::function_name(argument list)

{
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

For example: void employee :: getdata()


{
cout<<”Enter the IDNO: “;
cin>>idno;
cout<<”Enter the Name: “;
cin>>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.

PROGRAM TO FIND SIMPLE INTEREST [LAB PROGRAM #6]

#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();
}

Defining function inside the class

❖ 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

Program to convert temperature in Fahrenheit to centigrade

#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;
}

Nesting of member functions

A member function of a class can be called in two ways.


1. Using an object of that class with dot operator
2. Using a member function of that class

Thus, when a member function calls another member function of same class, it is called
as nested functions.

To find the greatest of two numbers using nested functions


#include <iostream.h>
#include<conio.h>
class largest
{
private:
int a,b;
public:
void getdata();
in t findlarge();
void display();

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;
}

Arrays within a class


➢ The data members of a class can be arrays.
➢ Arrays can be manipulated as other data members.
Example: class student
{
private:
int regno; char name[10];
i nt marks[6];
public:
void getdata();
void showdata();
};
To find the total marks of four subjects of a student
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
class student
{
private:
in t regno;
char name[15];
in t marks[4];
public: Enter the name: Santhosh B K
void getdata(); Enter the regno: 1234
void showdata(); Enter the four subjects marks: 90 90 90 90
};
void student::getdata() The student data:
{ Reg. No: 1234
cout<<"Enter the name: "; Name: Santhosh B K
cin.getline(name, 15); Marks: 90 90 90 90
cout<<"Enter the regno: "; Total marks: 360
cin>>regno;

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;

cout<<"\n\nThe student data:


"<<endl; cout<<"Reg. No:
"<<regno<<endl; cout<<"Name: ";
cout.write(name,strlen(name));
cout<<"\nMarks: ";
f o r (in t i= 0 ; i< 4 ; i+ + )
cout<<setw(4)<<marks
[i];
f o r (in t i= 0 ; i< 4 ; i+ + )
total = total + marks[i];
cout<<endl<<"Total marks: "<<total;
}

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.

Example: class student


{
private:
i nt regno;
char name[10];
i nt marks[6];
public:
void getdata();
void showdata();
}s[4];

8 Savitha Parthasarathy
Chapter - 7 Class and Objects

To find the total marks of a student


#include<iostream.h>
#include<string.h>
#include<iomanip.h>
class student
{ Enter the regno: 123
Enter the name: Harshini
private: Enter the four subjects marks: 60 60 60 60
in t regno;
char name[10]; Enter the regno: 124
in t marks[4]; Enter the name: Nandini
Enter the four subjects marks: 70 70 70 70
public:
void getdata(); Enter the regno: 125
void showdata(); Enter the name: Sushmitha
}; Enter the four subjects marks: 80 80 80 80
void student::getdata() Name: Harshini
{ ---- -- --- --- -- --- -- - --- -- ---
cout<<"\nEnter the regno: "; Reg. No: 123
cin>>regno; Marks: 60 60 60 60
Total marks: 240
cout<<"Enter the name: ";
cin.getline(name, 10);
co ut<<" E nter the f o ur su bje cts m a r ks: ";
Name: Nandini
---- -- --- --- -- --- -- - --- -- ---
f o r (in t i= 0 ; i< 4 ; i+ + ) Reg. No: 124
Marks: 70 70 70 70
cin>>marks[i]; Total marks: 280
}
void student::showdata() Name: Sushmitha
{ ---- -- --- --- -- --- -- - --- -- ---
Reg. No: 125
int total = 0; Marks: 80 80 80 80
cout<<"\n\nName: "; Total marks: 320
cout.write(name,strlen(name));
cout<<"\n----------------------------"<<endl;
cout<<"Reg. No: "<<regno<<endl;
cout<<"\nMarks: ";
f o r (in t i= 0 ; i< 4 ; i+ + )
cout<<setw(4)<<marks[i];
f o r (in t i= 0 ; i< 4 ; i+ + )
total = total + marks[i];
cout<<endl<<"Total marks: "<<total;
}
void main()
{
student s[3];
f o r (in t i= 0 ; i< 3 ; i+ + )
s[i].getdata();
for(int i=0; i<3; i++)
s[i].showdata();
}
Passing objects to functions

➢ A function can receive an object as an argument.


➢ This is similar to any other data type passed to a function.
➢ An object can be passed to a function in two ways:
a. Copy of entire object is passed to function (pass-by-value).
b. Only address of the object is transferred to the function (pass-by-reference).

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:

a. When an address of an object is passed to the function, the function directly


works on the original object.

b. This means changes made to the object inside the function will reflect in the
original object.

c. Pass-by-reference is more efficient, since it requires only passing the address


of the object and not the entire 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

You might also like