×

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.


Related Topics

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

4 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.