Is C++ an object-oriented programming language?

ericaeducative

ericaeducative

Posted on January 12, 2022

Is C++ an object-oriented programming language?

Object-oriented programming (OOP) is one of the most popular programming paradigms. The C++ programming language is one of many languages that supports object-oriented programming, alongside Java, C#, Python, and JavaScript. Some developers consider C++ to be an object-oriented language, while others argue that it’s not. Today, we’ll discuss object-oriented programming to understand whether C++ is an object-oriented programming language.

We’ll cover:

What is object-oriented programming?

Object-oriented programming is a programming paradigm based on classes and objects and four OOP concepts of inheritance, encapsulation, abstraction, and polymorphism. OOP is modeled off the fact that the real world consists of objects, rather than the variables and functions we use in software development. In OOP, each object has properties and functions that it can perform. Objects that share properties and functions can be grouped into classes and subclasses, which serve as blueprints for the properties and functions that an object will inherit.

The foundational OOP concepts are:

  • Inheritance: The ability of a class to inherit data and behaviors from another class

    • The class that inherits features is the derived class, while the class it inherits features from is the base class
  • Encapsulation: The binding of data to the functions that can perform operations on them

    • Enables data hiding, another important OOP technique
  • Abstraction: The masking of internal functions to present only high-level methods to the user

  • Polymorphism: The ability to perform the same task in various ways

Consider a sparrow and a penguin. Both have eyes, but they don’t share the ability to fly. Not all birds share the function of flight, therefore the base class of birds wouldn’t assign the function of flight, but the derived classes of flying birds would assign the flying function.

The following diagram illustrates the example of a birds class. Inheritance is what allows the derived classes to adopt the properties and functions from the base class.

diagram oop class inheritance

Another feature of object-oriented languages is that their syntax allows keywords to restrict and define access to classes, methods, and other members. These keywords are known as access specifiers (or access modifiers), which are essential to implementing encapsulation and abstraction.
The benefits of OOP are numerous. We can leverage OOP to develop applications with less code. Object-oriented programming helps reduce repetition in code, which reduces code complexity and data redundancy. It also increases the reusability of code. Users can create user-defined objects and classes which they can use in other applications.

Is C++ an object-oriented programming language?

C++ is widely considered an object-oriented programming language. Stroustrup developed C++ by adding object-oriented capabilities to the C programming language. When we say that a language is an object-oriented programming language, we often mean that it supports object-oriented programming. However, most popular languages that support OOP are not strictly object-oriented languages. They're often multi-paradigm languages that can support other popular paradigms. C++ falls into this category because it supports several paradigms. It's not an exclusively object-oriented language, but a functional and procedural language as well.

While it can be considered an OOP language, C++ isn’t a pure object-oriented language. Some reasons for this are:

  • Object is not a primary requirement: A pure OOP language treats all features in a program as objects. However, since C++ supports other programming paradigms, you can write a complete valid program without using a single object.
  • Class is not a primary requirement: The main() function can exist independent of a class.

Considering these points, it’s safe to say C++ is a partial object-oriented programming language.

Object-oriented programming in C++

C++ offers the essentials necessary to implement object-oriented programming. It has classes and objects, access specifiers, and the OOP concepts of inheritance, encapsulation, abstraction, and polymorphism.

Classes and objects

Classes are user-defined data types that form a blueprint for properties and functions. Objects are an instance of a class. Memory is allocated for objects, while classes don’t take up memory.

The syntax for class definition in C++ is:

class ClassName { 

  / *member variables and functions*/

}; // a semicolon ends the class
Enter fullscreen mode Exit fullscreen mode

The syntax for creating a class object in C++ is:

class ClassName { 

 / *member variables and functions*/
};

int main () {
  int x; // integer object
  ClassName c; // ClassName object
}
Enter fullscreen mode Exit fullscreen mode

Access specifiers

Access specifiers are keywords that serve to restrict and define permissions to access data in class members. There are three keywords that serve as access specifiers in C++: public, private, and protected.

Inheritance

We can implement inheritance in C++ by creating a derived class to a base class.
The syntax in C++ code for this is:

class DerivedclassName : AccessMode BaseclassName {
  // derived class body
}; // a semicolon ends derived class
Enter fullscreen mode Exit fullscreen mode

Here, AccessMode indicates the type of access specifier.

Encapsulation

Encapsulation in C++ is implemented through class and access specifiers.

Abstraction

Access specifiers in C++ help implement abstraction using classes in C++. Header files in C++ are another form of abstraction through which we can implement function calls and methods without needing to know the algorithms guiding a function.

Polymorphism

There are two types of polymorphism in C++:

  • Compile time polymorphism: Achieved through function overloading or operator overloading
  • Run time polymorphism: Achieved through function overriding and virtual functions

Wrapping up and next steps

If you’re looking for a practical technique to help you write applications with less code, OOP is a great technique to leverage in C++. C++ can be considered an object-oriented language. While it might not be a pure object-oriented language, it was designed specifically to enable object-oriented programming.

To help you get started with object-oriented programming in C++, Educative has created the C++ for Programmers learning path. This learning path consists of six modules. It covers the basics of C++ and progresses up to cover OOP techniques, data structures, C++ templates, and more.

Happy learning!

Continue reading about C++ on Educative

Start a discussion

Are you interested in learning OOP? What are your thoughts on the debate of C++ being an OOP language? Let us know in the comments below!

💖 💪 🙅 🚩
ericaeducative
ericaeducative

Posted on January 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related