×

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

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Print Table Using For-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...

3 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.