(305)Object Oriented Programming
Classes and Objects
Educator:
Ms. Twinkle S. Panchal
Assistant Professor
Sutex Bank College of Computer
Applications and Science, Amroli.
Contents
● Introduction to Structures
● Introduction Class and Object
● Access Specifiers
● Constructor and Destructor
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
What is Structure ?
A structure is a user-defined data type in C/C++. A structure creates a data type
that can be used to group items of possibly different types into a single type.
Employee
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Structure Contains 2 Types of Members:
● Data Member: These members are normal C++ variables. We can create a
structure with variables of different data types in C++.
● Member Functions: These members are normal C++ functions. Along with
variables, we can also include functions inside a structure declaration.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example:
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Structure
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Limitations of Structure
1) We cannot use operators like +,- etc. on Structure variables. For example,
consider the following code:
Not Allowed in Structure
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Limitations of Structure
2) No Data Hiding: C Structures do not permit data hiding. Structure members can be
accessed by any function, anywhere in the scope of the Structure.
In other words, the structure members are public members
3) Access Modifiers: C Programming language do not support access modifiers. (public,
private and protected)
4) Construction creation in Structure: Structures in C cannot have constructor inside
Structures.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
To Overcome the Limitation of Structure the
Concept of Class came into existence..
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Introduction to Class
● A class in C++ is the building block for Object-Oriented programming.
● It is a user-defined data type, which holds its own
○ data members and
○ member functions,
● which can be accessed and used by creating an object of that class.
● A C++ class is like a blueprint for an object.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Object
● An Object is an instance of a Class.
● When a class is defined, no memory is allocated but when an object is
created memory is allocated.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
● A Class is defined in C++ using keyword class followed by the name of class.
● The body of class is defined inside the curly brackets and terminated by a
semicolon at the end.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Declaring Objects
● When a class is defined, only the specification(formate) for the object is
defined; no memory or storage is allocated.
● To use the data and access functions defined in the class, you need to create
objects.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
How to Access Data Members(Variable) of Class?
● The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object.
● For example if the name of object is obj and you want to access the data
member with the name RollNo then you will have to write
● obj.RollNo
● To Assign Value:
Educator:
● obj.RollNo=35; Ms. Twinkle Panchal
(305)Object Oriented Programming
How to Access Methods of Class?
● The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object.
● For example if the name of object is obj and you want to access the member
function with the name printName() then you will have to write
● obj.printName() .
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Inside Class
● Variables inside class are known as Data Members.
● Functions inside class are known as Member Functions or Methods.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example for Class Creation
#include <iostream.h>
Class student // Creating Object: (write code in main
{ block)
//Declaring Access
Specifier Student objStud;
Public:
//Declaring variable // Accessing Variables
Int Rollno, Marks;
//Declaring methods objStud.Rollno=101;
void display()
{ objStud.Marks=90;
cout<<”Roll No ”<<Rollno;
//Accessing Methods
cout<<”Marks ”<<Marks; Educator:
} objStud.display(); Ms. Twinkle Panchal
};
(305)Object Oriented Programming
Declaring Member Functions for Class
There are 2 ways to define a member function:
● Inside class definition
● Outside class definition
To define a member function outside the class definition we have to use the
scope resolution :: operator along with class name and function name.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Declaring Member Function inside and outside
Class
#include <iostream.h> //Definition of displayMarks() using
Class student
scope resolution operator ::
{
Public:
Int Rollno,Marks;
void student:: displayMarks()
//Declaring methods
inside class {
void displayRollNo()
{ cout<<”Marks ”<<Marks;
cout<<”Roll No ”<<Rollno; }
}
//Declaring methods outside class Educator:
(only declaration) Ms. Twinkle Panchal
void displayMarks();
}; (305)Object Oriented Programming
Note:
● If we do not specify any access modifiers for the members inside the class
then by default the access modifier for the members will be Private.
● By Default all the data members and member functions of the class are
Private
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Access Modifiers / Access Specifiers
Access modifiers are used to implement an important feature of Object-Oriented
Programming known as Data Hiding.
Access Modifiers or Access Specifiers in a class are used to set the accessibility of the
class members.
It sets some restrictions on the class members not to get directly accessed by the outside
functions.
There are 3 types of access modifiers available in C++:
1. Public Educator:
2. Private Ms. Twinkle Panchal
3. Protected
(305)Object Oriented Programming
Public
All the class members declared under public will be available to everyone.
The data members and member functions declared public can be accessed by
other classes too.
The public members of a class can be accessed from anywhere in the program
using the direct member access operator (.) with the object of that class.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Private
The class members declared as private can be accessed only by the functions
inside the class.
They are not allowed to be accessed directly by any object or function outside
the class.
Only the member functions or the friend functions are allowed to access the
private data members of a class.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example: int main()
{
// creating object of the class
class Circle
{ Circle obj;
// private data member
private: // trying to access private data member
double radius; // directly outside the class
obj.radius = 1.5;
// public member function
public: // member function is public so it is
double compute_area() accessible
{ cout << "Area is:" << obj.compute_area();
// member function can access //private- return 0;
data member radius }
return 3.14*radius*radius; Educator:
} Ms. Twinkle Panchal
};
(305)Object Oriented Programming
Output
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Protected – Introduction
Protected access modifier is similar to that of private access modifiers.
The difference is that the class member declared as Protected are inaccessible
outside the class but they can be accessed by any subclass(derived class) of that
class.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Protected Access Specifier
class Parent public:
void setId(int id)
{
{
// protected data members // Child class is able to access the
protected: inherited
// protected data members of base
int id_protected;
class
};
id_protected = id;
// sub class or derived class }
class Child : public Parent void displayId()
{ {
Educator:
cout <<id_protected;
Ms. Twinkle Panchal
}
};
(305)Object Oriented Programming
Continue..
// main function
int main() {
Child obj1;
// member function of the derived class can
// access the protected data members of the base class
obj1.setId(81);
obj1.displayId(); Educator:
return 0; Ms. Twinkle Panchal
}
(305)Object Oriented Programming
Nesting of Member Functions
● One member function can call another member function of the same class
directly by just using its name.
● This concept is known as Nesting of Member Functions.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Note:
● A member function can call another member function of the same class for
that you do not need an object.
● Member function is called by other member function just called by its name.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Nesting of Member Function
class nesting public:
void disp_num()
{
{
int a;
int sq=square_num(); //nesting of member function
public:
void input_num( ) int cu=cube_num(); //nesting of member function
{
cout<<”\nEnter a number ”; cout<<”\nThe square of “<<a<<” is ” <<sq;
cin>>a;
cout<<”\nThe cube of “<<a<<” is ” <<cu;
}
}
private: }; //end of class
int square_num( )
{ return a* a; }
Educator:
int cube_num( ) Ms. Twinkle Panchal
{ return a* a*a; }
(305)Object Oriented Programming
Example – Continue..
int main()
{
nesting n1;
n1.input_num();
n1.disp_num();
return 0;
}
Here, we are just calling the input and display function as other
Educator:
Functions are called inside disp_num() function. Ms. Twinkle Panchal
(305)Object Oriented Programming
Let’s do it practically..
Question – 1 : Create class data with two data members and take input from user and display it.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Array Within a Class
● Arrays can be declared as the members of a class.
● The arrays can be declared as private, public or protected members of the
class.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Array within a Class
class arraydemo void show()
{
{ cout<<”\nArray elements are”<<endl;
int a[6];
public: for(int i=0;i<6;i++)
void getdata() {
{ cout<<a[i]<<endl;
cout<<”Enter 6 numbers in the array:”<<endl; }
}
};
for(int i=0;i<6;i++) void main()
{ {
cin>>a[i]; arraydemo obj;
} obj.getdata(); Educator:
} obj.show();
Ms. Twinkle Panchal
}
(305)Object Oriented Programming
Example of Array within class
Create class ArrayDemo with six data members of same type, take input from
user , calculate sum of all six members and display the sum.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example for Array Within a Class
const int size=5; void tot_marks() //calculating total marks
{
class student
int total=0;
{
for(int i=0; i<size; i++)
int roll_no; total+ = marks[i];
int marks[size]; cout<<"\n\nTotal marks "<<total;
public: }
void getdata ()
{ void main() {
cout<<"\nEnter roll no: ";
student stu;
Cin>>roll_no; stu.getdata() ;
for(int i=0; i<size; i++) stu.tot_marks() ;
{ getch(); Educator:
cout<<"Enter marks in Ms. Twinkle Panchal
subject"<<(i+1)<<": "; cin>>marks[i] ; }
}
(305)Object Oriented Programming
Arrays of Object
● Like array of other user-defined data types, an array of class type can also be
created.
● The array of class type contains the objects of the class as its individual
elements.
● Thus, an array of a class type is also known as an array of objects.
● An array of objects is declared in the same way as an array of any built-in data
type.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Syntax for Array of Objects:
● Class_name Object_name [size] ;
● Example:
● Employee obj_emp[5];
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Array of Objects
class Student int main()
{
{ Student obj_st[5];
char name[30]; for( int i=0; i<5; i++ )
int marks; {
cout << "Student " << i + 1 << endl;
public: cout << "Enter name and marks" << endl;
void getdata() { st[i].getdata();
cin >> name; }
cin >> marks; } for( int i=0; i<5; i++ )
{
void displayInfo() cout << "Student " << i + 1 << endl;
st[i].displayInfo(); Educator:
{ cout << "Name : " << name << endl;
} return 0; } Ms. Twinkle Panchal
cout << "Marks : " << marks << endl; }
};
(305)Object Oriented Programming
Let’s do it Practically
Write a program to create employee class with following data members
1) emp_Id 2) emp_Name 3) Age 4) Salary
Create two member function 1) Getdata and 2) Putdata to get data from user and
display it for 5 employees.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Objects as Function Arguments
● In C++ we can pass class’s objects as arguments and also return them from a
function the same way we pass and return other variables.
● No special keyword or header file is required to do so.
● To pass an object as an argument we write the object name as the argument
while calling the function the same way we do it for other variables.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Syntax:
● function_name(object_name);
● Example:
● Sum ( obj1 , obj2 )
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example for Object as Function Argument
class Example { int main()
{
public:
Example E1, E2, E3;
int a;
// Values are initialized
// This function will take // for both objects
// object as arguments and E1.a = 50;
// return object E2.a = 100;
void add(Example Ea, Example Eb) E3.a = 0;
{
E3.add(E1,E2)
Example Ec;
Ec.a = Ec.a + Ea.a + Eb.a; Cout<<“Sum is of two object ”;
Cout<<E3.a; Educator:
// returning the object } Ms. Twinkle Panchal
return Ec;
}
}; (305)Object Oriented Programming
Example for Object as Function Arguments
int main()
class Example { {
public: // Create objects
int a; Example E1, E2;
// Values are initialized for both objects
// This function will take E1.a = 50;
E2.a = 100;
// an object as an
argument E2.add(E1);
void add(Example E)
{ return 0;
} Educator:
a = a + E.a; Ms. Twinkle Panchal
}
}; (305)Object Oriented Programming
Practical Question for Practice..
● Write a program for multiplydemo class with one data member num to get
multiplication from 3 objects.
● Hint:
● m4.multiply ( m1, m2, m3)
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Weight Calculation Program..(Object as Function Argument)
void weight :: putdata ()
class weight
{
{ cout<<kilogram<<" Kgs. and"<<gram<<" grams.\n";
int kilogram; }
int gram; void weight :: sum_weight(weight wl,weight w2)
public: {
void getdata (); gram = wl.gram + w2.gram;
void putdata ();
Kilogram = gram/1000;
void sum_weight (weight,weight) ;
}; Gram = gram%1000;
void weight :: getdata()
{ Kilogram += wl.kilogram+w2.kilogram;
cout<<"/nKilograms:";
cin>>kilogram; }
cout<<"Grams:"; Educator:
cin>>gram; Ms. Twinkle Panchal
}
(305)Object Oriented Programming
Time Calculation Program (Object as Function Argument)
class time Void sum (time t1,time t2)
{ {
int hours; Minutes = t1.minutes + t2.minutes;
int minutes;
Public: Hours = minutes/60;
void getdata()
Minutes = minutes%60;
{ cin>>hours; cin>>minutes;}
Hours + = t1.hours + t2.hours;
void putdata() }
{
cout<<“Hours ”<<hours << “ Minutes
”<<minutes ;
} Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Friend Function
● If a function is defined as a friend function then, the private and protected
data of a class can be accessed using the function.
● The complier knows a given function is a friend function by the use of the
keyword friend.
● For accessing the data, the declaration of a friend function should be made
inside the body of the class (can be anywhere inside class either in private or
public section) starting with keyword friend.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Declaration of friend function in C++
class class_name
{
... ..
...
friend return_type function_name(argument/s);
... ..
...
}
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Continue.. With declaration
class className
{
... .. ...
friend return_type functionName(argument/s);
... .. ... }
return_type functionName(argument/s)
{ ... .. ... // Private and protected data of className can be accessed from
// this function because it is a friend function of className.
... .. ... } Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Example of Friend Function
Class sample Int mean (sample s)
{
{
return (s.a + s.b) / 2;
int a,b; }
Public:
void getdata() Int main()
{ cin>>a; cin>>b; } {
sample obj;
Friend int mean (sample S); obj.getdata();
};
int ans = mean(obj);
Educator:
return 0;
Ms. Twinkle Panchal
}
(305)Object Oriented Programming
A Function Friendly for Two class
class class2
class class2; // forward declaration {
int y;
class class1 public:
{ void setY(int j)
int x; {
public: y=j;
void setX(int i) }
{ friend void maximum (class1 c1,class2 c2)
x=i; };
}
friend void maximum (class1 c1,class2 Educator:
Ms. Twinkle Panchal
c2)
};
(305)Object Oriented Programming
void maximum (class1 c1,class2 c2) int main() {
{
class1 obj1;
if (c1.x >= c2.y)
obj1.setX(71);
{
cout<<"\n Maximum is class2 obj2;
"<<c1.x<<endl; obj2.setY(8);
}
else maximum(obj1, obj2);
{
return 0;
cout<<"\n Maximum is
}
"<<c2.y<<endl; Educator:
Ms. Twinkle Panchal
}
}
(305)Object Oriented Programming
Practice Question
Write a program to swap private data members of two classes.
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Returning Objects using friend function
Write a program to transpose of 2 objects of matrix class.
Program Here
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Practice Question
Write a program for matrix class of 3 by 3 matrix and calculate sum all the
elements of matrix.
Hint:
Use friend function and return object..
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming
Thank You..
Educator:
Ms. Twinkle Panchal
(305)Object Oriented Programming