C++ Object-Oriented Programming Concepts
C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features:
- Object
- Class
- Data abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Message passing
Object
An object is the basic unit of object-oriented programming. It is a real-time entity. It can represent any item that the program has to handle.
Declaration Syntax:
Example
Here Fruit is class and mango is an object.
Class
Class is a user-defined data type, which holds data members and member functions. Data members and member functions can be accessed by creating an instance (object) of the class. A class is created by using keyword
class.
Declaration Syntax:
class class_Name{
// data member;
// member function(){}
};
Example
Data Abstraction
Abstraction refers to hiding the internal background details and reflecting only essential features.
Abstraction represents the needed information in the program without representing details.
For example, a base class data member and member function used in another class, but the other class does not know the implementation detail of data member and member function of the base class.
Data Encapsulation
Data encapsulation refers to the binding of data members and member functions into a single unit that is called class. The
encapsulation prevents direct access of data. Data is only accessible through the member functions of the class.
Polymorphism
Polymorphism means many forms. When a single task is performed in different ways is called polymorphism. Polymorphism provides an implementation of the same function name in multiple ways.
Inheritance
Inheritance is a feature in which one class inherits the property of another class. A class which inherits the property is called a
derived class or subclass or child class and from which derived class inherits property is called as
a base class or parent class.
Message Passing
When a set of objects communicate with each other is called
message passing. Message passing is done to sending and receiving information by objects. Following steps are followed in message passing:
- Creating classes having objects and their behavior,
- Creating objects from the class definition,
- Establishing communication between objects.