OOP (Object Oriented Programming)
Object Oriented programming is a programming paradigm based on the concepts of objects which can contain data and methods. Many of programming language supports the concepts of object oriented Programming. C++, Java, Python are the example programming language of OOP.
Features of Object Oriented Programming are:
- Class
- Object
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
1. Class: Class is a user-defined data type. Class is a blueprint for creating an objects. Class contains data members and member functions.
Syntax:
class className
{
access specifier:
data_type data_members;
data_type member_functions;
};
Class Example:
class student
{
public:
int rollno;
string name;
void show();
};
2.Objects: Object is an instance of a class.
Syntax:
className objectName;
Example:
student st;
3.Polymorphism: Polymorphism means “many forms.” It allows methods to perform differently based on the object that calls them. Example: Function Overloading, Function Overriding
Types of Polymorphism:
- Compile-time Polymorphism
- Runtime Polymorphism
4.Inheritance: The process of creating new class derived class by acquiring the properties of base class is known as inheritance. Inheritance allows a new class (derived class / child class) to acquire the properties and methods of an existing class (base class / parent class / super class).
Types of Inheritance:
- Single Inheritance
- Multi-level Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Single Inheritance: A Derived class inherits the properties and methods of one base class.
Syntax:
class A
{
access specifier:
data_type data_members of A;
data_type member_functions of A;
};
class B: public A
{
access specifier:
data_type data_members of B;
data_type member_functions of B;
};
Multilevel Inheritance: A process of deriving a class from another derived class.
Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: access specifier A
{
Data_members of class B;
member_functions of class B;
};
Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B: public A
{
public:
int B_id;
string B_name;
void display();
};
Multiple Inheritance: A derived class inherits from more than one base classes. This allows the derived class to have access to members of multiple base classes.
Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: {
Data_members of class B;
member_functions of class B;
};
class C: accessSpecifier A, accessSpecifier B{
Data_members of class B;
member_functions of class B;
};
Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B
{
public:
int B_id;
string B_name;
void display();
};
class C: public A, public B
{
public:
int B_id;
string B_name;
void display();
};
Hierarchical Inheritance: Hierarchical Inheritance is a type of inheritance where multiple derived classes inherit from a single base class. It has tree like structure.
Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: accessSpecifier A
{
Data_members of class B;
member_functions of class B;
};
class C: accessSpecifier A
{
Data_members of class C;
member_functions of class C;
};
Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B: public A
{
public:
int B_id;
string B_name;
void display();
};
class C: public A
{
public:
int B_id;
string B_name;
void display();
};
5.Encapsulation: Encapsulation in C++ means binding the data (variables) and the functions (methods) that work on the data together in a single unit. Class is the best example of encapsulation.
6.Abstraction: Abstraction in C++ is a fundamental concept in Object-Oriented Programming (OOP) that focuses on hiding implementation details and exposing only the essential features of an object.