4 Pillars of OOPs
OOPs
OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. Some features of OOPs are:
- OOPS is a Paradigm (Methodology / Way of programming).
- C++, Java, Python, and C# support OOPS.
- OOPS allows users to create a class and use them throughout the program by creating an object of the class.
- OOPS provide protection or security for the program.
- Every unity game programming supports OOPS.
Pillars of OOPS
There are mainly 4 pillars or concepts of OOPS:
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism

1. Abstraction
Data Abstraction is a purposeful process of hiding confidential information from the outside world.
Data abstraction is a way of hiding internal details /information in a program from the user and the outside world. It provides data that the public class can access.
Abstraction made it easy for users to interact with the programming language by hiding irrelevant information.
Benefits of Data Abstraction
Data abstraction provides important advantages -
- Class data are protected from unwanted user-level errors.
- It becomes easier to operate with the help of data abstraction.
- Helps to improve the security level of an application or a program, as
- only basic information is visible to the user.
- Reduction in low-level code.
- It reduces the complexity of a program for the user. It becomes
- easy to read and understand by the user.
- It avoids duplication of code and thus increases the reusability.
- In big projects, Abstraction is very useful as portioning across the program provides structure, and error finding becomes easy.
Ways to implement Abstraction in C++
There are two ways to achieve Abstraction in C++:
1. Abstraction using classes
2. Abstraction using header files.
1. Abstraction using classes
An abstraction can be done through classes. A class has some specifiers that can control which data or function is to be visible to the user and which is not.
Example-
#include <iostream>
using namespace std;
class sample {
public:
int x, y, z;
public:
void put() {
cout<< "enter three values:" << endl;
cin>>x>>y >> z;
}
private:
void display() {
cout<< "sum of x y z is: "<<endl;
cout<< x +y+ z;
} };
int main() {
sample s1;
s1.put();
s1.display();
return 0;
}
Explanation-If we run this program, then private member functions will not be accessible by the compiler so it will generate an error.
OUTPUT

Another example of declaring all the member functions in public modifiers:
#include<iostream >
using namespace std;
class sample {
private:
int x, y, z;
public:
void put() {
cout<< "enter three values" <<endl;
cin>> x>> y>> z;
}
public:
void display() {
cout<<"sum of x y z is"<< endl;
cout<< x+ y+ z;
}
};
int main() {
sample s1;
s1.put();
s1.display();
return 0;
}
Explanation --If you execute this program, all the class member functions and data members will be accessible, so this will generate the following output:
OUTPUT

2. Abstraction in Header files
Abstraction can be achieved through header files in C++. For example, in <math.h> header file in C++, sqrt() function is available to calculate the square root of any number without knowing the actual algorithm behind it. So, the header file hides the implementation details from the user.
Example-
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
cout<< "Square root of 144 is ";
//header file used to find the square root
cout<< sqrt(144) <<endl;
return 0;
}
OUTPUT

Inheritance
Inheritance is a concept of OOPS in which one class inherits the property of another class. When one class accesses the property or characteristics of another class, it is known as inheritance.

Derived class- also called the child class. It is the class that inherits from another class.
Base class- also called parent or superclass. It is a class that is inherited.
Syntax:
Class <derived class name> : access specifier < base class name>
{
//statements..;
}
Access specifier/ modes in inheritance
There are 3 access specifiers in inheritance:
- Public mode
If we derive a base class from a public modifier base class, then the public member of the base class will become public in the derived class, and protected members of the base class will become protected in the derived class. - Protected mode
If we derive a subclass from a protected base class, then both the public member and protected member will become protected in the derived class. - Private mode
If we derive a subclass from the private base class, then both public and protected members will become private in the derived class.
Advantages of inheritance
1. Reduce code size.
2. Provides better code reusability.
3. Improves code readability as code is divided into classes.
4. Different clients can use member functions of their want and add more by inheritance.
5. No duplication of code.
Types of inheritance
There are 5 types of inheritance, and they are:
- Single inheritance
- Multi-level inheritance
- Multiple inheritance
- Hybrid inheritance
- Hierarchical inheritance
i. Single inheritance
The inheritance in which only one base class and one parent class is called single inheritance.
Code example-
#include<iostream>
using namespace std;
class A {
public:
void func() {
cout<< "Inherited" << endl;
}
};
class B:public A {
};
int main() {
B ob;
ob.func();
return 0;
}
OUTPUT

ii. Multi-level inheritance
The inheritance in which another class can again derive a derived class is called multi-level inheritance.
Code example-
#include<iostream>
using namespace std;
class A {
public:
void Afunc() {
cout<<"Function A"<< endl;
}
};
class B : public A {
public:
void Bfunc() {
cout<<"\nFunction B";
} };
class C : public B {
};
int main() {
C obj;
obj.Afunc();
obj.Bfunc();
return 0;
}
OUTPUT

iii. Multiple Inheritance
The inheritance in which a parent has more than one base or child class is called multiple inheritance.
Code example-
#include<iostream>
using namespace std;
class A {
public:
void Afunc() {
cout<< " Function A" << endl;
}
};
class B {
public:
void Bfunc() {
cout<<" \n Function B ";
} };
class C : public A , public B {
};
int main() {
C obj;
obj.Afunc();
obj.Bfunc();
return 0;
}
OUTPUT

iv. Hierarchal inheritance
This type of inheritance has a tree-like structure as every class act as a parent class for another class.
Code example-
#include <iostream>
using namespace std;
//hierarchical inheritance example
class Shape // shape class -> base class
{
public:
int x,y;
void get_data(int n,int m) {
x= n;
y = m;
}
};
class Rectangle : public Shape { // inherit Shape class
public:
int area_rect() {
int area = x*y;
return area;
}
};
class Triangle : public Shape // inherit Shape class
{
public:
int triangle_area() {
float area = 0.5*x*y;
return area;
}
};
class Square : public Shape // inherit Shape class
{
public:
int square_area() {
float area = 4*x;
return area;
}
};
int main()
{
Rectangle r;
Triangle t;
Square s;
int length,breadth,base,height,side;
//area of a Rectanglest
cout << "Enter the length and breadth of a rectangle: ";
cin>>length>>breadth;
r.get_data(length,breadth);
int rect_area = r.area_rect();
cout << "Area of the rectangle = " <<rect_area<< endl;
//area of a triangle
cout << "Enter the base and height of the triangle: ";
cin>>base>>height;
t.get_data(base,height);
float tri_area = t.triangle_area();
cout <<"Area of the triangle = " << tri_area<<endl;
//area of a Square
cout << "Enter the length of one side of the square: ";
cin>>side;
s.get_data(side,side);
int sq_area = s.square_area();
cout <<"Area of the square = " << sq_area<<endl;
return 0;
}
OUTPUT

V. Hybrid inheritance
This is a combination of more than one inheritance. Hybrid inheritance can be a combination of two or more types of inheritance.
Code example-
#include <iostream>
#include <string>
using namespace std;
//Hybrid inheritance = multilevel + multilp
class student //First base Class
{
int id;
string name;
public:
void getstudent(){
cout << "Enter student Id and student name:\n";
cin >> id >> name;
}
};
class marks: public student{
public: //derived from studentprotected:
int marks_math,marks_phy,marks_chem;
void getmarks(){
cout << "Enter 3 subject marks:";
cin >>marks_math>>marks_phy>>marks_chem;
}
};
class sports{
protected:
int spmarks;
public:
void getsports(){
cout << "Enter sports marks:";cin >> spmarks;
}
};
class result : public marks, public sports //Derived class by multiple inheritance
{
int total_marks;
float avg_marks;
public :
void display(){
total_marks=marks_math+marks_phy+marks_chem;
avg_marks=total_marks/3.0;
cout << "Total marks =" << total_marks << endl;
cout << "Average marks =" << avg_marks << endl;
cout << "Average + Sports marks =" <<avg_marks + spmarks;
}
};
int main(){
result res;//object//
res.getstudent();
res.getmarks();
res.getsports();
res.display();
return 0;
}
OUTPUT

Explanation--Here we have four classes i.e., Student, Marks, Sports, and Result. Marks are derived from the student class. The class Result derives from Marks and Sports as we calculate the result from the subject marks as well as sports marks. The output is generated by creating an object of class Result that has acquired the properties of all the three classes.
Encapsulation
Encapsulation can be defined as wrapping up data and functions together into a single unit.
Wrapping them up into a single unit means combining data member and member functions into a single class. It helps with data hiding.
Consider a real-life-based example-
Different departments in a company have different roles and contributions to the company. Suppose a company has production, sales, and IT departments. The production department handles all records of how much production is required to meet. Similarly, the sales department records all the sales. For more reason, the production department head wants the sales record of the month. In this case, he cannot directly access the sales department records. He must first request some sales department official to get the data.
This is what encapsulation does. To access a member function from the class, we need an object to call it. The object is made in the main function.
For example-
#include<iostream>
using namespace std;
class ball {
private:
float area;
public:
void setarea (int radius ) {
area= 3.14 * radius * radius;
}
float getarea() {
return area;
}
};
int main() {
ball b1;
b1.setarea(7);
cout<<"Area of ball is ";
cout<< b1.getarea() ;
return 0;
}
OUTPUT

NOTE: Only class member functions can access private data members (area) in this code. The member functions and data members are combined together so that they can manipulate each other. This is called encapsulation.
Access specifier in encapsulation
- – in public scope, other classes and functions can access the data members and member functions.
- - in private scope, only the member functions inside the class have access to class members. Functions outside the class cannot access them.
- - similar to private scope, we cannot access member function from the outside class, but they can be accessed from derived class.
Difference between Abstraction and Encapsulation
Encapsulation | Abstraction |
Encapsulation combines data and functions together into a single unit. | In Abstraction, the unwanted information is kept hidden. |
It helps in hiding data. | It helps in hiding the implementation of code or program. |
It can be implemented using access modifiers. | It can be implemented using abstract class as well as access modifiers. |
In this, problems are solved at the implementation level | In this, problems are solved at the design level. |
Polymorphism
Polymorphism is made up of two words:
- Poly- many
- Morphism-forms
The meaning of polymorphism is the same object has different behavior. It means having different functions have the same name.
Types of polymorphism

There are two types of polymorphism:
- Compile time polymorphism
- Run time polymorphism.
Compile time polymorphism
The function is called at the time of compiling the program. It is called compile-time polymorphism.
Also called static binding.
Compile time polymorphism has two types- function overloading and operator overloading.
i. Function overloading
In functionoverloading, more than one function has the same name with different arguments passed inside them. The same function can perform many tasks with different arguments. The function is called at the time of compilation, so it is called compile time polymorphism.
Example-
#include<iostream>
using namespace std;
class demo {
public:
void printresult( int i) {
cout<< "print integer result: "<< i<<endl;
}
void printresult(double f) {
cout<< "print float result: "<< f<<endl;
}
void printresult (string c) {
cout<< "print string result: "<< c<<endl;
}
};
int main()
{
demo show;
show.printresult(5);
show.printresult(400.363);
show.printresult("Welcome to programming");
return 0;
}
OUTPUT

--In this program printresult() function called integer, float, and string data type with the same name but different arguments.
ii. Operator Overloading
An operator symbol (+, -,*,/) used differently is called operator overloading.
Operator overloading means assigning additional operand tasks without changing their actual functionality.
Example-
#include<iostream>
using namespace std;
class complex {
private: int a, b;
public:
void getdata(int x, int y) {
a= x;
b= y;
}
void showdata() {
cout<< "value of a= "<< a;
cout<< "\nvalue of b= "<< b;
}
complex operator +(complex obj)
{
complex temp;
temp.a = a + obj.a;
temp.b = b + obj.b;
return temp;
}
};
int main()
{
complex t1, t2, t3;
t1.getdata(1, 2);
t2.getdata(2, 3);
t3=t1 + t2;
// t3 = t1. Operator +(t2)
t3.showdata();
return 0;
}
OUTPUT

Run Time Polymorphism
In Runtime polymorphism, functions are called at the time of the program execution.
Run time polymorphism has two types- function overriding and virtual function.
i. Function Overriding
In function overriding, the main function is rewritten in the derived class.
Thus, the main or base function is overridden. Overriding can only be possible in a derived class. The same function has two definitions- one in the base class and another in the derived class. The compiler decides which function should call at run time, so this is called run time polymorphism.
Example-
#include<iostream>
using namespace std;
class bank {
public:
float a, intr, tot_balance;
void calc_interest( float balance)
{
a= balance;
intr= a * 4/100;
tot_balance = a + intr;
cout<< "your interest is: "<< intr;
cout<< "\nyour total balance is: "<< tot_balance;
}
};
class sbi : public bank {
float a, intr, tot_balance;
public:
void calc_interest(float balance) {
a= balance;
intr= a * 5/100;
tot_balance = a + intr;
cout<< "your interest is: "<< intr;
cout<< "\nyour total balance is: "<< tot_balance;
}
};
int main() {
sbi obj;
obj.calc_interest(500);
return 0;
}
OUTPUT

ii. Virtual Function
A virtual function is a function in which the programmer determines which version of the function to use.
The function is declared in the base class with a virtual keyword, then redefined in derived classes.
Example-
#include<iostream>
using namespace std;
class demo {
public:
virtual void f1() {
cout<< "base class"<< endl;
} };
class abc: public demo {
public:
void f1() {
cout<< "Derived class"<< endl;
}
};
int main()
{
abc a;
demo *d= &a;
d->f1();
return 0;
}
OUTPUT
