Let us go through the Concept of OOPs in C++ programming in this blog. Object-oriented programming (OOP) in C++ is a powerful programming approach that focuses on organizing code into objects, making it more efficient, reusable, and easy to manage. By combining data and functions into a single unit, OOP helps in building real-world models in code.
This article introduces the core concepts of OOP in C++, including classes, objects, encapsulation, inheritance, polymorphism, and abstraction, explained in a simple and beginner-friendly way.
What is the Concept Of OOP in C++?
OOP stands for object-oriented programming. It is a programming paradigm that allows code to be written in class and object format. In OOP, we bundle these properties and actions together into something called a class, and then we make objects, which are the real things, from these classes.
Concept of OOP in C++ is beneficial in many ways, including
- Keeps our code organized
- Makes it easier to manage and reuse
- Helps break down big problems into smaller and meaningful parts
- Models real-world things in a way that is easy to understand
Major Concepts of OOP In C++ Language
There are a few major concepts in OOP that make up the pillar of the object-oriented programming paradigm. These include:
- Class: It is like a recipe to create real-world entities.
- Objects: Real instances made up of class
- Encapsulation: Keeping data and code together in one place
- Inheritance: A class can inherit properties from another class
- Polymorphism: One entity in different forms
- Abstraction: Hiding unnecessary and sensitive details
C++ Classes and Objects
Now, a class in C++ is like a blueprint, or call it a template for creating objects. Class itself is not a real entity, and hence, it does not occupy any memory until you create an object from it. It consists of different components, including properties and member functions. Properties are the data members (variables), and the member functions are the methods.
Now, an object in C++ is a real entity, or call it a real instance of a class. When we create an object, it actually holds up the data and can perform actions.
Read More: What is the Main Difference Between Python and C++ Programming Language
Basic Example of Class and Object Implementation in C++
Implementation of Classes and Objects in C++ |
---|
#include<iostream>
using namespace std; class Car { public: string brand; int speed; void start() { cout << “Car has started!” << endl; } }; int main() { Car myCar; // Object created myCar.brand = “BMW”; myCar.speed = 180; cout << “Brand: ” << myCar.brand << endl; cout << “Speed: ” << myCar.speed << ” km/h” << endl; myCar.start(); // Call member function return 0; } |
Encapsulation Concept of OOP in C++ Programming
Encapsulation is one of the basic pillars of concept of oop in C++. Encapsulation means wrapping up properties and code (methods) together inside a class. It is similar to putting your phone’s battery, camera, and screen inside a casing. Encapsulation is about protecting the data from outside interference and misuse.
We can control what can be accessed or modified from outside the class using the access specifiers, generally termed as access modifiers. Access specifiers are the special components that determine the scope of access of any property or method present inside a class. In C++, there are three access specifiers that are mentioned below:
- Public: No restrictions; the members can be accessed anywhere
- Private: only accessible within the class; cannot be accessed directly from outside
- Protected: Can be accessed within the class and the derived class (subclass)
There are several benefits of Encapsulation in C++, including
- Encapsulation keeps your data safe and secure
- Encapsulation controls how data is accessed or modified
- Encapsulation makes your code clean and organized
- Encapsulation helps in data hiding
Implementation of Encapsulation in C++
Implementation of Encapsulation in C++ |
---|
#include<iostream>
using namespace std; class Student { private: string name; int marks; public: void setData(string n, int m) { name = n; if (m >= 0 && m <= 100) { marks = m; } else { cout << “Invalid marks!” << endl; marks = 0; } } void getData() { cout << name << ” got ” << marks << ” marks.” << endl; } }; int main() { Student s1; s1.setData(“Ankit”, 90); s1.getData(); s1.setData(“Ankit”, -50); // Will show invalid marks s1.getData(); return 0; } |
Inheritance Concept of OOP in C++ Language
Inheritance is a feature of object-oriented programming (OOP) where one class can inherit properties and behaviors from another class. It is just like in real life; a child inherits traits from parents; in concept of oop in C++, a derived class inherits from a base class.
There are five types of inheritance in C++, that are:
- Single Inheritance: One base class and one derived class
- Multiple Inheritance: Multiple base classes and one derived class
- Multilevel Inheritance: Derived class, another derived class
- Hierarchical Inheritance: One base class, and multiple derived class
- Hybrid Inheritance: Combination of two or more types of inheritance
Suppose an Animal is a base class. Now, the Dog class is a subclass that inherits from the Animal class, and similarly, the Puppy class inherits from the Dog class. Just like family traits passing down, this is the inheritance.
Basic Implementation of Inheritance in C++
Implementation of Inheritance in C++ |
---|
#include<iostream>
using namespace std; // Base Class (Parent) class Animal { public: void eat() { cout << “This animal eats food.” << endl; } }; // Derived Class (Child) class Dog : public Animal { public: void bark() { cout << “The dog barks!” << endl; } }; int main() { Dog d; d.eat(); // Inherited function d.bark(); // Own function return 0; } |
Abstraction Concept of OOP in C++
Abstraction Concept of oop in C++ means hiding unnecessary details and showing only the essential features of an object. In other words, you interact with what you need without worrying about how it works inside. We can achieve it using abstract classes and pure virtual functions.
An abstract class contains at least one pure virtual function. A pure virtual function is a function with no body in the base class. It forces the derived class to override it. You cannot create objects of abstract classes; they are meant to be inherited and implemented by child classes.
Implementation of Abstraction in C++
Implementation of Abstraction in C++ |
---|
#include<iostream>
using namespace std; class Animal { public: virtual void sound() = 0; // Pure virtual function }; class Dog : public Animal { public: void sound() { cout << “Dog barks!” << endl; } }; class Cat : public Animal { public: void sound() { cout << “Cat meows!” << endl; } }; int main() { Animal* a1; Dog d; Cat c; a1 = &d; a1->sound(); // Dog barks! a1 = &c; a1->sound(); // Cat meows! return 0; } |
Polymorphism Concept of OOP in C++ Language
Polymorphism means “many forms”. In C++, it allows functions or objects to behave differently based on various situations, even though they share the same name. In simple words, same name, different behavior depending on which object or data it is working with.
Polymorphism increases the flexibility of the code and supports code reuse and extensibility. It makes our programs behave smartly based on object types.
Imagine a smart remote: when pointed at a TV, it changes channels; when pointed at an AC, it changes temperature; and when pointed at a music player, it adjusts volume. See a single remote having different function calls for different objects. The same function call has different behavior depending upon the object.
There are two major types of polymorphism in C++, which are
- Compile-time Polymorphism: Function overloading and operator overloading.
- Run-time Polymorphism: Function overriding using virtual functions.
Implementation of Compile-time Polymorphism
Compile-time Polymorphism |
---|
#include<iostream>
using namespace std; class Print { public: void show(int x) { cout << “Integer: ” << x << endl; } void show(string s) { cout << “String: ” << s << endl; } }; int main() { Print p; p.show(50); p.show(“Suraj”); return 0; } |
Implementation of Run-time Polymorphism
Run-Time Polymorphism |
---|
#include<iostream>
using namespace std; class Animal { public: virtual void sound() { cout << “This animal makes a sound!” << endl; } }; class Dog : public Animal { public: void sound() { cout << “The dog barks!” << endl; } }; int main() { Animal* a; Dog d; a = &d; a->sound(); // Calls Dog’s sound() because of virtual keyword return 0; } |
Learn C++ Programming & DSA With PW Skills
Become proficient in Data structures and C++ programming with PW Skills Decode DSA With C++ Course. Engage in an interactive learning environment with top industry experts guiding you throughout the course. This course is designed for people who want to build their career in the tech industry. Build your expertise in all major concept of OOP in C++ programming with real world projects and practice excercises.
Engage in interactive learning using recorded videos and practice and master the fundamentals of C++ with concepts of data structures and algorithms in C++. Prepare for job interviews and competitive programming with pwskills.com
Concept of OOP In C++ Programming FAQs
Q1. What is OOP in C++?
Ans. OOP stands for object-oriented programming. It is a programming paradigm that allows code to be written in class and object format.
Q2. What is encapsulation?
Ans. Encapsulation means wrapping up properties and code (methods) together inside a class. It is similar to putting your phone’s battery, camera, and screen inside a casing. Encapsulation is about protecting the data from outside interference and misuse.
Q3. What are classes and objects in C++?
Ans. A class in C++ is like a blueprint, or call it a template for creating objects. Class itself is not a real entity, and hence, it does not occupy any memory until you create an object from it. An object in C++ is a real entity, or call it a real instance of a class. When we create an object, it actually holds up the data and can perform actions.
Q4. What is Abstraction in C++?
Ans. Abstraction means hiding unnecessary details and showing only the essential features of an object. In other words, you interact with what you need without worrying about how it works inside. We can achieve it using abstract classes and pure virtual functions.