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

Object Oriented Programming Concepts

Object oriented programming concepts include objects, classes, encapsulation, abstraction, polymorphism, and inheritance. Objects are real world entities modeled as classes which act as blueprints. Encapsulation hides implementation details while abstraction only shows essential details. Polymorphism allows classes to have many forms and inheritance establishes relationships between classes. These concepts provide advantages like easy reusability and extensibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Object Oriented Programming Concepts

Object oriented programming concepts include objects, classes, encapsulation, abstraction, polymorphism, and inheritance. Objects are real world entities modeled as classes which act as blueprints. Encapsulation hides implementation details while abstraction only shows essential details. Polymorphism allows classes to have many forms and inheritance establishes relationships between classes. These concepts provide advantages like easy reusability and extensibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

OBJECT ORIENTED PROGRAMMING

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

• It models real world well.


• Programs are easy to understand.
• It offers class reusability. Already created classes can be reused
without having to write them again.
• It facilitates quick development as parallel development of classes is
possible.
• Programs are easier to test, manage & maintain.
Objects

• Real world entities.


• An object is created from a class.
• OBJECT is an identifiable entity with some characteristics
and behavior.
• In OOP programming object represents a entity that can store data
and has its interface through functions.

Eg: person, car, etc…


Classes
• CLASS is a template/blue-print representing a group of objects that share
common properties and relationships.
• A user defined type
• Consists of both data and methods
• Makes possible encapsulation, data hiding and inheritance.
Example of class and object

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:

void area(int s);


void area(int l,int b);
float area(float b,float h,float x);
float area(float r);
float area(float r,float h);
Operator overloading example
complex operator+ complex operator-
(complex c2) (complex c2)
{ {
complex temp; complex temp;
temp.real=real+c2.real; temp.real=real-c2.real;
temp.img=img+c2.img; temp.img=img-c2.img;
return temp; return temp;
} }
Inheritance
• Reusability
• one class inherits the features(fields and methods) of another class
• “Is a” relationship

Super Class: The class whose features are inherited is known as superclass(or


a base class or a parent class).

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.

• A message for an object is a request for execution of a procedure


and therefore will invoke a function in the receiving object that
generates the desired results.

• Message Passing involves specifying the name of objects, the


name of the function, and the information to be sent.

You might also like