Principles of Object-Oriented Programming in C++
What is Object-Oriented Programming?
Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of object-oriented programming is to bind together the data and the functions that operate on them up to the point that no other part of the code can access the data except for the function that operates on the data.
Example for object-oriented programming in C++
//This program demonstrates how objects are used in the program
#include<iostream>
using namespace std;
//Creating a class person with the class keyword.
class person
{
//This is the private part of the class
char name[20];
int id;
//creating a public part of the code
//creating a function member
public:
void getdetails(){}
};
//Creating the main function.
int main()
{
//creating object for the class person.
person p1; // p1 is a object
}
Explanation:
The above demonstrates how to use an object in the c++ code. In the above code, we have created a class person; inside the class, we have created a few function members and variables. Then we created the main function and object for the class.
Basic Principles of OOPs
Below are the basic principles of object-oriented programming:
- Encapsulation
- Data Abstraction
- Polymorphism
- Inheritance
1. Encapsulation:
The encapsulation of data and its methods or functions is the process by which they are combined. In C++, we can do encapsulation by making the function members in the class private and making the public function access the private functions.
Example:
//This code is written to explain the encapsulation
#include<iostream>
using namespace std;
//creating a class using the class keyword
class Encapsulation
{
private:
// data hidden from the outside world
int x;
public:
// function to set the value of
// variable x
void set(int a)
{
x =a;
}
// function to return the value of
// variable x
int get()
{
return x;
}
};
// creating a main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
Output:

Explanation:
The above code is written to demonstrate how to use encapsulation in c++ code. In the code, we have created a class encapsulation. In the class, we have created a private variable, x then we have created function members get and set public. In the main function, we have created an object for the encapsulation, and then using the function member we have set the values of x and printed the values x. This process of binding together the private and public parts of the code is an example of encapsulation.
2. Abstraction:
A user's attention is diverted from irrelevant information by abstraction. This means that abstraction involves hiding irrelevant information from the user. For example, if we open an app on our phone, the information about how the app opens in the background is hidden from the user. This is known as abstraction. This is very useful as the user will not know the unnecessary information using the abstraction process. We can use private keywords in the code so that other users cannot access the code.
//This code is written to demonstrate how abstraction works in a c++ program
#include <iostream>
using namespace std;
//Creating a class Abstraction using the class keyword.
class Abstraction
{
private:
//private only this class can access the private variables
int a, b;
public:
//Creating a function member.
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
//Creating a main function
int main()
{
//object is created for the Abstraction class
Abstraction obj;
obj.set(50, 60);
obj.display();
return 0;
}
Output:

Explanation:
This code is written to demonstrate how abstraction works in a c++ program. In the above code, we have created a class in which we have written private parts of the code with some variables. The variables in the private code are only accessible to the code in that class. Inside the main function of the code, we set the value of the variables in the private part of the code using the function members.
3. Inheritance:
Inheritance is the process in which another class inherits one class's properties. When a class "A" has properties of another class "B", then we can access the members in the class "B" in the main function using the class A’s object.
Example:
Here is an inheritance example:
// C++ program to demonstrate inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create an object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
// Calling member of the derived class
dog1.bark();
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the inheritance. In the example, we can see that we have created a class, Aminal, with some function members. Again we have created a class with the name Dog and inherited the animal class properties into the Dog class. In the main function, we created an object using the Dog class. Using the Dog class object, we have called function members in the dog class and animal class.
4. Polymorphism:
Polymorphism means having many forms. Polymorphism helps the coder to create an overloading function. Polymorphism means we can create one functions with different definitions.
//This code is written to show how polymorphism works in c++
#include <iostream>
using namespace std;
//Creating a class Animal using the class keyword
class Animal {
public:
//Creating a function member of void return type
void eat(){
cout<<"Eating...";
}
};
//Creating a class dog and inherting the properties of animal class
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
//Creating a main function.
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
Output:

Explanation:
In the above code, we have created two classes, Animal and dog, and we have inherited the animal characteristics into the dog class. In the main class, when we created an object for the dog class, we called the function present in the dog class i.e., eat().