OOPs Concepts in C++

OOPs Concepts in C++

6 mins read9.2K Views Comment
clickHere
Updated on Oct 3, 2023 12:48 IST

OOP concepts in C++ involve creating objects from classes, bundling data and methods. Inheritance forms new classes from existing ones, while polymorphism enables interchangeable usage of objects, enhancing code modularity and flexibility. Let’s understand!

2022_05_Add-a-heading-1.jpg

In C language, our program was structured around functions and logic, but in C++ we have an Object-oriented programming approach program is structured around objects rather than functions and logic. This approach makes the code flexible and modular by partitioning data into two memory areas, i.e., data and functions. Object-oriented programming mainly focuses on classes and objects. In OOPs, data is represented as objects that have attributes and functions. In this blog, we will learn about the different OOPs concepts in C++ which were not there in the C language.

Table of Content

Why Do You Need Object-Oriented Programming?

Earlier, we used procedural-oriented programming in a programming language like C language. This approach was not good as it had some limitations like code cannot be reused in the program, and there was the problem of global data access, and this procedural approach did not quickly solve the real-world issues.

Problems solved by object-oriented approach:

Code reusability: No need to write the same code again and again, which increases the program’s simplicity.As the same code can be executed many times in the program.

Data hiding: Data can be hidden from the outside world.

OOPs Concepts in C++

Some basic concepts act as the building blocks of OOPs.

So now, we will cover these concepts with examples and real-world analogy

1. Class

A class is a user-defined data type holding variables and member functions together, which can only be accessed by using objects.

Let’s understand by a real-life analogy.

Let’s take Human Being as a class. 

Human Beings are broadly categorized into two types, Male and Female. Right? It’s true. So, in this case, we can take Male and Female as subclasses(derived class). Every Human being(Male or Female) has some characteristics two hands, two legs, one nose, two eyes, one heart, etc.; take these body parts as data variables. All human beings perform tasks like walking, eating, seeing, talking, hearing, etc. Take this task as member functions.

Here we have Class Rectangle having age as a data member and setLength as a member function.

Interesting, isn’t it? Now we will see how all this is related to C++ and OOPS.

Classes and Objects in C++
Classes and Objects in C++
If you are a student then you must have got this question in the exam or practical viva and if you are a job seeker then you must have faced...read more
Working with Strings in C++
Working with Strings in C++
Strings in C++ is a very simple yet very important topic from exams as well as interview point of view.
Constructors in C++ and its Types
Constructors in C++ and its Types
Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly three types of constructors in C++, Default, Parameterized and...read more

Also read: OOPs concepts in JAVA

Also explore: Class Vs. Object: What are the differences?

2. Object

Objects are the blueprint or instances of the class. Objects are created from classes.

Like in this Human Being is a class having three objects: human 1, human 2, and human 3.

In this, we created an object of the Rectangle class. Now using this object we assigned the value to the age variable and execute it. setLength()

3. Abstraction

Suppose a person is performing the task of uploading and downloading a file. He knows which options to click for that. He will click on upload or download the file and start uploading and downloading, respectively. The phone shows you that the file is uploaded or downloaded. What happens in the background when you click send is hidden from you as it is not relevant to you. What happens in the background when he clicks upload/download is hidden from him as it is not relevant and shows only that specific part that the user wants to see.

4. Encapsulation

Let’s understand with a real-life analogy:

In a company on a particular project, we have different teams working like Functional teams, Development teams, testing teams, content teams, and project teams, handling other tasks. Suppose the content team needs all data about the development team. In this case, data is not directly accessible. The content team has to contact someone from the development team and request to provide data. This is what encapsulation is. Data(project data+ employees data) is wrapped under a single name  ‘development team.’

You can also see the concept of data hiding here. No other team can access data without permission. In the same way, no class can access the private data of the other class.

In the above example, we can see that we have data member(ID) and member functions(set_value() and get_value()) encapsulated in the container called class.

5. Inheritance

Inheritance is the process in which there is a relationship between two base class and a derived class. The derived class acquires the properties and features of the base class. The class which inherits/takes the features is known as the derived class, and the class whose features it inherited is called the base class. For example, the Class HumanBeing is the base class, and classes Male and Female are derived classes.

Real-life analogy:

In the above example, we have HumanBeing class. There were two classes, Male and Female. Human Being class has functions like talk walk, see, hear. Both Male and Females can inherit these functions. Both males and females.

In the same way, all human beings eat, walk, see, talk, hear, etc. Male and Female perform some common functions. But some functions are specific to the class. Like a female can give birth, but a male cannot.

2022_05_image-45.jpg

This employee class is a base class having salary as the data member and developer is a derived class having ‘e’ as a data member and salary as a member function. This means the developer class can use the salary attribute.

Must explore: Multilevel inheritance in C++ with real life analogy

Must check: Hierarchical inheritance in C++ with real-life analogy

6. Polymorphism

Polymorphism works by redefining the way something works either by changing the method of performing it or changing the parts using which it is done. A person at the same time can play different roles. A female at the same time is a mother, sister, wife, and employee. This is called polymorphism.

Polymorphism has two important concepts

  1. Operator Overloading: When an operator shows different behaviors in different instances is known as operator overloading.
  2. Function Overloading: When a function shows different behaviors in different instances is known as function overloading. That means the function having the same name in different classes will give different outputs. For example:

Example:

 
#include <iostream>
using namespace std;
 
// Function with 2 int parameters
int sum(int num1, int num2) {
    return num1 + num2;
}
 
// Function with 2 double parameters
double sum(double num1, double num2) {
    return num1 + num2;
}
 
// Function with 3 int parameters
int sum(int num1, int num2, int num3) {
    return num1 + num2 + num3;
}
 
int main() {
    // Call function with 2 int parameters
    cout << sum(3, 4) << endl;
 
    // Call function with 2 double parameters
    cout << sum(3.4, 6.6) << endl;
 
    // Call function with 3 int parameters
    cout << sum(9, 3, 8) << endl;
 
    return 0;
}
Copy code

Output

7 10 20

Here you can see there is one function sum() which is used in the same program but with a different number of arguments and different argument types. First sum() has two integers, second sum() has two double numbers and third sum() has three integer values.

Endnotes

I hope you must have got an idea of the OOPs concepts in C++ and why we use it. This effort was made to make you understand it with a real-life analogy. If you liked the blog, please share it with other people looking out for this topic.

FAQs

What is class in C++?

A class is like a container containing functions and data. Class is used for ensuring security as everything in Class is by default private, which means members of a class canu2019t be accessed from outside. If you want to access data or functions of a class you need to provide an access specifier (public ) for that.

What is an object in C++?

An Object is an instance of the class. It is a real-life entity like a car, bus, book, etc. These objects are used to access data and function of class.

What is the need for OOPs?

Users can understand the software easily, without knowing the actual implementation. With OOPs, the readability, debugging and modification of the code becomes easy. u00a0OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. Code Reusability becomes easy.

What is a constructor?

Constructors are member functions which are called when an object is created and whose name is the same as the class name. Syntax: class CLASS_NAME { u2026u2026u2026 public : CLASS_NAME() //Default constructor { . . . . . . } //other member functions };

Download this article as PDF to read offline

Download as PDF
clickHere
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments

We use cookies to improve your experience. By continuing to browse the site, you agree to our Privacy Policy and Cookie Policy.