C++ OOPs Concept
The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of the object oriented programming paradigm. True object-oriented programming is a programming paradigm in which everything is presented as an object.
Object Oriented Programming System
A real-world entity, such as a pen, chair, or table, is referred to as an object. Object-oriented programming (OOP) is a programming approach or pattern that uses classes and objects to create programs. It offers a variety of concepts that facilitate software development and maintenance:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Object
An object is any entity that has a state and behavior. For example: chair, pen, table, keyboard, bicycle etc. It can be both physical and intellectual in nature.
Student e1; //creating an object of Student
Explanation:
Student is the type in this example, and e1 is the reference variable that refers to the Student class instance.
Class
A class is a user-defined data type that may be used in our program as an object function Object() { [native code] } or "blueprint" for constructing objects.
Example of a program creating a class Student:
class Student { // The Student
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Explanation:
- A class of student names is created using the class keyword.
- A public keyword is an access specifier that indicates that class members (features and methods) are available from outside. Later, you'll learn more about access specifications.
- The class has an integer variable myNum and the string variable myString. The variables declared in the class are known as attributes.
Inheritance
Inheritance occurs when an object inherits all the characteristics and behavior of its parent object. It allows code reuse. It is used to achieve polymorphism at runtime. The class that inherits the members of another class is referred to as the class derived in C ++, while the class whose members inherit is known as the base class. Derived Class of Base Class is a customized version of Base Class.
Example of a program showing single level inheritance in C++:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Bank {
public:
float salary = 85000;
};
class Programmer: public Bank {
public:
float bonus_salary = 10000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus Salary: "<<p1.bonus_salary<<endl;
return 0;
}
OUTPUT:
Salary: 85000
Bonus salary: 10000
Explanation:
Bank is the base class in this example, while Programmer is the derived class.
Polymorphism
Polymorphism occurs when a job is completed in several ways. For example, to illustrate the buyer differently, draw something like a form or a rectangle. To implement polymorphism in C ++, we use function overloading and function overriding.
Example:
class x // declaration of base class x.
{
int i;
public:
void display()
{
cout<< "Class x ";
}
};
class y : public // derived class declaration.
{
int j;
public:
void display()
{
cout<<"Class y";
}
};
Explanation:
In the example above, the prototype of the show () function is the same in both base and derived class. As a result, the static binder cannot be used. It would be wonderful if the right task could be chosen at runtime.
Abstraction
Abstraction is the process of hiding internal features while demonstrating efficiency. For example, we don't know how to process a phone call. To complete abstraction in C ++, we use abstract classes and interfaces.
Example of a program showing abstraction in C++:
#include <iostream>
using namespace std;
class create_Abstraction
{
private:
int i, j;
public:
// method to set values of
// private members
void set(int a, int b)
{
i = a;
j = b;
}
void display()
{
cout<<"i = " <<i << endl;
cout<<"j = " << j << endl;
}
};
int main()
{
create_Abstraction obe;
obe.set(24, 30);
obe.display();
return 0;
}
OUTPUT:
a= 24
b= 30
Explanation:
As you can see, we are not permitted to access the variables i and j directly in the preceding program; however, we may use the functions set() and show() to set and display the values of a and b.
Encapsulation
Encapsulation is the process of combining (or encapsulating) code and data into a single entity. For instance, a capsule is wrapped with many drugs.
Example:
#include<iostream>
using namespace std;
class create_Еncapsulation
{
private:
// data hidden from outside world
int i;
public:
// function to set value of
// variable i
void set(int x)
{
i =x;
}
// function to return value of
// variable i
int get()
{
return i;
}
};
// main function
int main()
{
create_Еncapsulation obe;
obe.set(12);
cout<<obe.get();
return 0;
}
OUTPUT:
12
Explanation:
The variable i is declared private in the above program. Only the methods get() and set(), which are included in the class, can be used to access and alter this variable. As a result, we may conclude that the variable i, as well as the methods get() and set(), are bound together, resulting in encapsulation.