05.Chapter 1 Class and Object
05.Chapter 1 Class and Object
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
Employee
Educator:
Ms. Twinkle Panchal
● Member Functions: These members are normal C++ functions. Along with
variables, we can also include functions inside a structure declaration.
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
● obj.RollNo
● To Assign Value:
Educator:
● obj.RollNo=35; Ms. Twinkle Panchal
● obj.printName() .
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
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
● By Default all the data members and member functions of the class are
Private
Educator:
Ms. Twinkle Panchal
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.
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
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
Educator:
Ms. Twinkle Panchal
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
Child obj1;
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.
Educator:
Ms. Twinkle Panchal
● Member function is called by other member function just called by its name.
Educator:
Ms. Twinkle Panchal
Here, we are just calling the input and display function as other
Educator:
Functions are called inside disp_num() function. Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
● 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
● Example:
● Employee obj_emp[5];
Educator:
Ms. Twinkle Panchal
Create two member function 1) Getdata and 2) Putdata to get data from user and
display it for 5 employees.
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal
● Example:
Educator:
Ms. Twinkle Panchal
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
● 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
Educator:
Ms. Twinkle Panchal
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
Educator:
Ms. Twinkle Panchal
Program Here
Educator:
Ms. Twinkle Panchal
Hint:
Use friend function and return object..
Educator:
Ms. Twinkle Panchal
Educator:
Ms. Twinkle Panchal