Object Oriented Programming Concepts
Object Oriented Programming Concepts
CONCEPTS
Object Oriented Programing Concepts
• Objects -real world entities
• Classes –blueprint of class
• Encapsulation–hides details of
implementation
• Data Abstraction –provides only
essentials
• Polymorphism-contains many
type of form
• Inheritance-relationship
• Data Hiding –hide internal obj
details
• MessagePassing-communication
ADVANTAGES OF OOP
Rectangle r1;
class Rectangle Rectangle r2;
{ Rectangle r3;
private:
int width;
int length;
public:
r1 r2
void set(int w, int l); width width
int area(); length length
}; width
r3 length
6
Encapsulation
• The wrapping up of data and functions into a single unit (called class).
• Encapsulation is a way to implement data abstraction.
• It hides the details of the implementation of an object.
• It is a protective shield that prevents the data from being accessed by the code
outside this shield.
Abstraction
• The act of representing essential features without including the background details
or explanations.
• The property by virtue of which only the essential details are displayed to the user.
Polymorphism
• Polymorphism means having many forms.
• An object behaves differently in different situations.
• Types of polymorphism
o Compile time polymorphism
• Function overloading:
• Many functions with same name
• Differs with data types and no of arguments
• Operator overloading:
- Operators are made to perform different action other than their
original operation.
o Runtime polymorphism:
• Virtual functions
• Inheritance and dynamic binding
Function overloading example:
Sub Class: The class that inherits the other class is known as subclass(or a
derived class, extended class, or child class). The subclass can add its own
fields and methods in addition to the superclass fields and methods.
Types of inheritance
•Single inheritance
class emp{….};
class dept:public emp{….}; •Multipath Inheritance
•Multiple inheritance class A
class person{….}; class B:public A
class company{….}; class C:public A
class owner:public person,public class D:public A,public B,public C
company{…};
•Multi-level inheritance •Hybrid Inheritance
class person{…}; class item{….};
class employee:public person{…}; class sell:public item{….};
class faculty:public employee{…}; class discount{….};
•Hierarchical Inheritance class bill:public sell,public discount{….};
class student{….};
class artgraduate:public student{….};
class engggraduate:public student{….};
Dynamic binding
• Late binding or run-time binding
• A block of code to be executed with reference to a
procedure(method) call is determined at run time.
• Inheritance
• Method overriding
• Virtual functions
V-table and v ptr
• Abstract classes
class polygon public:
{
public: void calcarea()
float area; {
virtual void calcarea()=0;
area=3.14*r*r;
}; cout<<area;
class rectangle:public polygon }
{
private: };
float l,b; void main()
public: {
void calcarea() polygon *p;
{ rectangle r;
area=l*b; p=&r;
cout<<area; p->calcarea();
} circle c;
p=&c;
}; p->calcarea();
class circle:public polygon
{ }
private:
float r;
Data Hiding
• Hide internal object details (data members)
• Information hiding
• Ensures exclusive data access to class members and protects
object integrity by preventing unintended or intended changes.
• Data encapsulation and abstraction
• Access specifiers:
o Private
o Protected
o public
Public:
-Public class members are available
everywhere.
-The data members and member functions
declared as public can be accessed by other
classes and functions.
-Public members of a class are accessed from
anywhere in the program using the direct
member access operator (.) with the object of
that class.
Private:
-Private members can be accessed only by the
member 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 the class.
Protected:
The class members declared as Protected
can be accessed by any derived class of that
class as well.
Message Passing
• Objects communicate with one another by sending and receiving
information to each other.