Education logo

Object-oriented programming (OOP) in C++

Lets understand the full concepts

By LejoyPublished 3 years ago 8 min read

Object-oriented programming (OOP) is a programming paradigm that is widely used in modern software development. One of the most popular languages for implementing OOP is C++, which was designed with OOP in mind. In this blog post, we will explore the fundamental OOP concepts in C++ and how they can be used to develop robust and efficient software.

CLASSES & OBJECTS:

The primary OOP concept in C++ is the class, which is a user-defined data type that encapsulates data and functions. The data is represented as member variables or data members, while the functions are represented as member functions or methods. Classes are used to create objects, which are instances of the class.

For example, let's say we want to create a class to represent a car. We can define a class called Car with member variables like make, model, year, and color, as well as member functions like start, stop, and drive. We can then create objects of the Car class, each with its own values for the member variables. Here's an example of how to define and use a Car object in C++:

class Car {

public:

string make;

string model;

int year;

string color;

void start() {

cout << "Starting the car..." << endl;

}

void stop() {

cout << "Stopping the car..." << endl;

}

void drive() {

cout << "Driving the car..." << endl;

}

};

int main() {

Car myCar;

myCar.make = "Honda";

myCar.model = "Civic";

myCar.year = 2021;

myCar.color = "Red";

myCar.start();

myCar.drive();

myCar.stop();

return 0;

}

In this example, we define a Car class with member variables make, model, year, and color, as well as member functions start, stop, and drive. We then create a Car object called myCar and set its member variables. Finally, we call the member functions to start, drive, and stop the car.

FOUR PILLARS OF OOP:

➡INHERITANCE

➡POLYMORPHISM

➡ABSTRACTION

➡ENCAPSULATION

INHERITANCE:

A key OOP concept in C++ is inheritance, which allows a class to inherit properties and behavior from a parent class.

In C++, we can define a derived class that inherits from a base class using the syntax:

class DerivedClass : public BaseClass {

// member variables and functions

};

The derived class inherits all the public and protected member variables and functions of the base class, and can add its own member variables and functions as well. Here's an example of inheritance in C++:

class Vehicle {

public:

string make;

string model;

int year;

string color;

void start() {

cout << "Starting the vehicle..." << endl;

}

void stop() {

cout << "Stopping the vehicle..." << endl;

}

void drive() {

cout << "Driving the vehicle..." << endl;

}

};

class Car : public Vehicle {

public:

void honk() {

cout << "Honk honk!" << endl;

}

};

int main() {

Car myCar;

myCar.make = "Honda";

myCar.model = "Civic";

myCar.year = 2021;

myCar.color = "Red";

myCar.start();

myCar.drive();

myCar.honk();

myCar.stop();

return 0;

}

In this example, we define a Vehicle class with member variables and functions for a generic vehicle. We then define a Car class that inherits from the Vehicle class and adds a member function for honking the car horn. When we create a Car object, we can call the member functions of both the Car class and the Vehicle class.

POLYMORPHISM:

Polymorphism is another fundamental OOP concept in C++. It allows different classes to implement the same function or method in different ways. Polymorphism can be achieved through function overloading and virtual functions.

Function overloading allows a class to define multiple functions with the same name but different parameters. The appropriate function to call is determined at compile-time based on the number and types of the arguments passed to the function. Here's an example of function overloading in C++:

class Calculator {

public:

int add(int x, int y) {

return x + y;

}

float add(float x, float y) {

return x + y;

}

};

int main() {

Calculator calc;

int a = 3, b = 5;

float c = 3.2, d = 5.7;

cout << "Sum of " << a << " and " << b << " is " << calc.add(a, b) << endl;

cout << "Sum of " << c << " and " << d << " is " << calc.add(c, d) << endl;

return 0;

}

In this example, we define a Calculator class with two add functions that have different parameter types (int and float). We can call either function with the appropriate arguments, and the correct function will be called at compile-time.

Virtual functions, on the other hand, are functions that can be overridden by derived classes. They allow us to define a function in a base class that can be implemented differently by each derived class. Here's an example of virtual functions in C++:

class Shape {

public:

virtual float area() = 0;

};

class Circle : public Shape {

public:

float radius;

Circle(float r) : radius(r) {}

float area() {

return 3.14 * radius * radius;

}

};

class Rectangle : public Shape {

public:

float length;

float width;

Rectangle(float l, float w) : length(l), width(w) {}

float area() {

return length * width;

}

};

int main() {

Circle myCircle(5);

Rectangle myRectangle(3, 4);

cout << "Area of circle with radius " << myCircle.radius << " is " << myCircle.area() << endl;

cout << "Area of rectangle with length " << myRectangle.length << " and width " << myRectangle.width << " is " << myRectangle.area() << endl;

return 0;

}

ABSTRACTION:

Abstraction is an important pillar of Object-Oriented Programming, which is used to represent complex real-world systems in a simplified and organized manner. In C++, abstraction is achieved through the use of classes and abstract classes.

An ATM (Automated Teller Machine) is a great example of abstraction in programming. An ATM is a complex system that performs various operations, such as account balance inquiries, cash withdrawals, and transfers, among others. However, the end-user (i.e., the person using the ATM) does not need to know the underlying technical details of how the system works. Instead, they only need to interact with the interface provided by the ATM.

Abstraction is the process of identifying essential features of an object and ignoring the non-essential ones. Abstraction allows us to focus on the important details and ignore the irrelevant ones. In C++, abstraction is achieved by defining abstract classes that provide a blueprint for other classes to follow. An abstract class cannot be instantiated, but can be used as a base class for other classes.

Here is an example of an abstract class in C++:

class Shape {

public:

virtual void draw() = 0;

virtual double area() = 0;

};

In this example, Shape is an abstract class that defines two pure virtual functions: draw() and area(). A pure virtual function is a function that has no implementation and is defined as = 0. Classes that inherit from an abstract class must provide their own implementation of the pure virtual functions.

Here is an example of a class that inherits from Shape and provides its own implementation of the pure virtual functions:

class Circle : public Shape {

private:

double radius;

public:

Circle(double r) {

radius = r;

}

void draw() {

cout << "Drawing a circle" << endl;

}

double area() {

return 3.14159 * radius * radius;

}

};

In this example, Circle is a class that inherits from Shape and provides its own implementation of the draw() and area() functions. The draw() function simply prints a message to the console, while the area() function calculates and returns the area of the circle.

By using abstraction, we can create a hierarchy of classes that share common properties and behaviors, while allowing for variations in implementation. This makes our code more modular, maintainable, and extensible.

ENCAPSULATION:

Encapsulation is one of the fundamental concepts of Object-Oriented Programming (OOP). It is the practice of combining data and the functions that operate on that data into a single unit, called a class. In C++, encapsulation is achieved by using access specifiers to control the visibility of the class members.

Encapsulation - Simplified Learning

Here is an example of a simple class that demonstrates encapsulation:

class Person {

private:

string name;

int age;

public:

void setName(string n) {

name = n;

}

string getName() {

return name;

}

void setAge(int a) {

age = a;

}

int getAge() {

return age;

}

};

In this example, Person is a class that contains two private member variables: name and age. The private access specifier means that these members can only be accessed from within the class. This provides data hiding and prevents the outside world from modifying the internal state of the class directly.

The public access specifier contains four public member functions: setName(), getName(), setAge(), and getAge(). These functions are used to access and modify the private member variables of the class. The public access specifier means that these members can be accessed from outside the class.

Here is an example of how to use the Person class:

int main() {

Person p;

p.setName("John");

p.setAge(30);

cout << "Name: " << p.getName() << endl;

cout << "Age: " << p.getAge() << endl;

return 0;

}

In this example, we create an instance of the Person class and set its name and age using the setName() and setAge() functions. We then use the getName() and getAge() functions to retrieve the values and print them to the console.

By encapsulating the data and functions within the class, we can ensure that the data is accessed and modified in a controlled manner. This helps to prevent bugs and other issues that can arise from directly modifying the internal state of the class.

CONCLUSION:

In this blog post, we have covered the fundamental OOP concepts in C++, including classes and objects, inheritance, polymorphism, and more. These concepts are essential for developing complex and scalable software, and understanding them is crucial for any C++ programmer. With these tools at your disposal, you can write efficient and maintainable code that is easily extensible and adaptable to changing requirements.

*********

collegehigh schoolinterviewstudentcourses

About the Creator

Lejoy

Read my poems and sonnets they are too blissful 😇😇

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.