Is C++ an object-oriented programming language?
Understand object-oriented programming principles and how C++ incorporates these concepts. This lesson helps you identify whether C++ is fully or partially object-oriented and introduces key features such as classes, objects, inheritance, encapsulation, abstraction, and polymorphism within C++ programming.
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.
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.
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. Classes are just blueprints and do not occupy memory for data variables until an object is instantiated.
The syntax for class definition in C++ is:
class ClassName {/* member variables and functions */}; // a semicolon ends the class
The syntax for creating a class object in C++ is:
class ClassName {/* member variables and functions */};int main () {int x; // primitive variableClassName c; // ClassName instance (object)return 0;}
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}; // semicolon ends derived class
Here, AccessMode indicates the type of access specifier (public, private, or protected).
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.
The "Abstract Classes" or "Pure Virtual Functions" are used to define interfaces.
Polymorphism
There are two types of polymorphism in C++:
Compile time polymorphism: This is achieved through function overloading or operator overloading. The compiler determines which function to call based on the number and type of arguments passed.
Run time polymorphism: This is achieved through function overriding. This allows a function in a derived class to replace the implementation of a function in the base class.
Unlike languages like Java, functions in C++ are not virtual by default. If you use a Base class pointer to access a Derived class object, C++ will call the Base class version of the function by default (static binding).
To force the program to check the actual object type at runtime and call the correct Derived function (dynamic binding), we must use the virtual keyword in the Base class.
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!