Classes & Objects
nnOnOOPs Concept
* Object-oriented programming — As the name suggests uses
objects in programming.
* Object-oriented programming aims to implement real-
world entities like inheritance, hiding, polymorphism, etc.
in programming.
* The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the
code can access this data except that function.
nneoClasses
Class in C++ is the building block that leads to object oriented
programming
* Class is a user defined data type, which holds its own data
members and member functions, which can be accessed
and used by creating an instance of that class,
* Data members are the data variables and member functions
are the functions used to manipulate these variables
together, these data members and member functions define
the properties and behavior of the objects in a Class
nneoExploring CLASS
* A C++ class is like a blueprint for an object. Let us consider an
example class MOBILE.
Tira | (Cle)
q
Ceti co
q% yy 1 "he!
nneoFeatures
=a
lume |
| set |
waa
-- -~
(mm) pan ) Ce
W oscar: TF susore _ :f en
——— Oy
nneoClass & Objects
is defined,
* An Object is an instance of a Class. When a clas:
no memory is allocated but when it is instantiated (i.e. an
object is created) memory is allocated
* Here, consider Object as the “key” for accessing anything inside
a classroom like chair, projector (data members) and accessing
functions inside the classroom like watching lectures, taking a
seminar ete.
VS
neoDefining class and Declaring objects
* Acclass is defined in C+ using the keyword class followed by
the name of the class.
* The body of the class is defined inside the curly brackets and
terminated by a semicolon at the end.
keyword userdetined name
lean be private public or prot
[I] Naribles tobe used
(0 [1ethods to access data members
1) Class name ends with 2 semicolon
nnecoDeclaring objects
* When a class is defined, only the specification 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.
Torre rw strato
Classes & Objects
* Syntax
|=Class
Box bi| [pox b2) [Box b3|<~ Objects
neovos
(class)Accessing Data Members and
Member Function
* Accessing data members and member functions: The data
members and member functions of the class can be accessed
using the dot(*.’) operator with the object.
+ For example, if the name of the object is obj and you want to
access the member function with the name printName() then
you will have to write obj.printName()
neoAccessing Data Members
The public data members are also accessed in the same way
given however the private data members are not allowed to
be accessed directly by the object.
Accessing a data member depends solely on the access
control of that data member. This access control is given by
Access modifiers in C++.
There are three access modifiers: public, private, and
protected
OnExample Program using Public
Output
Pee)
iG
Coenen
eet
amneoExample Programs using Private
Pereenntt
Output
aoe} Perel tees
cout << "Area
>
main)
ceanen
ese arcenExample Program using Protected
ara
mneoRecall
OOPs Concept
Class & Objects
Accessing Data members
Accessing member function
Access specifiers
neo