As you may have already heard that Object Oriented Programming is one of the most popular programming paradigm ( Programming paradigm is the way of writing efficient, scalable and readable code). Object Oriented Programming defines the set of principles, concepts and practices that guide the development of development of softwares. There are four main pillars of Object Oriented Programming :
- Encapsulation: Encapsulation refers as the bundling of data and methods (functions) that is operating on a single unit called as class. Encapsulation is binding data with data members with member variables. This will avoid the direct access of data members and data member will be secure. Encapsulation minimizes the revealedd code part to the user. Encapsulation works like a protective wrapper that conceals tha data and code with in the class.
#include <iostream>
using namespace std;
class temp{
int a;
int b;
public:
int solve(int input){
a=input;
b=a/2;
return b;
}
};
int main() {
int n;
cin>>n;
temp half;
int ans=half.solve(n);
cout<<ans<<endl;
}
- Inheritance: Inheritance is one of the most important concept in Object Oriented Programming. Inheritance allows to make code reusable by allowing the child class or subclass to inherit all the properties of parent class. By inheriting means we can user the members and member function of the parent class. Here, we use the Protected access specifier which allows data members andd member functions to only be access by the same class or inherited class.
Note - By default, Access specifier is private, if we did not define the access specifier for the base class. It'll by default Private derivation. In Private derivation, a derived class can not access the private data members.
#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
class Student : private Person {
char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Types of Inheritance:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Heiracrhial Inheritance
Hybrid Inheritance
Polymorphism: Polymorphism is made from two words "poly" + "morphism" which means many shape. Polymorphism is significant in OOPs. Polymorphism allow us to have various or multiple forms of object, variables andd methods. There can be varies implementation of same method as per class.
Types of Polymorphism.a. Compile time Polymorphism: In compile time polymorphism, compiler knows which function should called based on the number, type and order of arguments. It is also call static polymorphism.
Function Overloading: When multiple functions in a class with the same name but different parameters exist, these functions are said to be overloaded.
The functions can be overloaded by using a different number of arguments and by using different types of arguments.
If two same name and same argument functions just vary in their return type then such function isn’t overloaded.
Operator Overloading: We know that, “+” is used for addition or concatenation. Now, if I want to perform my customized operation i.e. upon calling +, I want to print “Hello Geek” or I want to perform subtraction instead of addition. In such cases, we use operator overloading.
b. Runtime Polymorphism: As the name suggest that polymorphism must be occured on runtime. In runtime polymorphism, the compiler resolves the object at run time and then it decides which function call should be associated with that object. It is also known as dynamic or late binding polymorphism.
It has two types
Funtion Overriding
Virtual Funtion
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
// Eating bread...
Abstraction: Abstraction in object oriented programming is one of the key feature to only displaying essential data and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Types of abstraction:Data abstraction – This type only shows the required information about the data and hides the unnecessary data.
Control Abstraction – This type only shows the required information about the implementation and hides unnecessary information.
// C++ Program to Demonstrate the
// working of Abstraction
#include <iostream>
using namespace std;
class implementAbstraction {
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
//Output a=10 b=20