DEV Community

Cover image for C++: An Object-Oriented Programming Language
Tyler Meyer
Tyler Meyer

Posted on

C++: An Object-Oriented Programming Language

What is C++?

According to a definition written on TechTarget, "C++ is an object-oriented programming language and a superset of the C language." [1]

When we call C++ a superset of the C language, this means that C++ can still run code from C and still produce the same result, but C++ comes with support for classes and objects. With the introduction of classes, C++ becomes the object-oriented programming language that is used to create large-scale applications as well as projects with constrained memory demands. A good place to start learning C++ is to learn about object-oriented programming.

Object-Oriented Programming

In a later example, I will show key features of the C++ language, but I will not be able to fully show you object-oriented programming, which I'll be shortening to OPP for the rest of this article.

As the name suggests, this implementation revolves around the concept of objects. According to Wikipedia, "computer programs are designed by making them out of objects that interact with one another in OPP." [2] This philosophy is great because objects can hold both data and methods and this information can be bundled together without having to be written with other code that it does not need to interact with. This is one of the benefits of OPP, and I am going to go over four key aspects of OPP, Inheritance, Abstraction, Encapsulation, and Polymorphism.

Inheritance

With classes, we can create objects that share certain property names and methods, but at the time of creating the object, we can pass in values that make the instance of the object different from others with the same properties and methods. Each object inherits a particular set of properties and methods.

Abstraction

Hiding information away from functions that should not be using that information is the practice of abstraction. Objects allow us to couple data and methods together in such a way that functions outside the object cannot use the data inside the object. This prevents misusing code written for a particular purpose that would lead to bugs in other parts of your program.

Encapsulation

Encapsulation is the practice I alluded to earlier where you keep data and methods bundled together. Specifically, the data held in the object is only interacted with by methods within the same object. This allows for editing code in a class without creating issues with code outside of the class. Working to remove unnecessary connections between different parts of your code is known as decoupling and it leads to high cohesion in your code.

Polymorphism

This last aspect allows your code to be more readable. When creating subclasses from a particular superclass, you may want to use a method with the same name in each subclass but want it to differ depending on which subclass the object being called is an instance of. The method takes on many forms, known as polymorphism, to produce the desired result for the object that calls the method.

Key Features of C++

We are going to cover a few features of C++. I'll explain how each line of a simple program works to show how C++ handles different aspects of writing code. While I explain this code, I'll reference JavaScript as a comparison. This explanation will be a taste of C++, and if you're looking for a deeper explanation of the language, I recommend using W3 Schools where I learned everything I am about to go over. If you're short on time, Fireship's "C++ in 100 Seconds" is about the fastest way you can see the different benefits of using C++ language in certain projects. Without further ado, let's check out some C++:

#include <iostream>
using namespace std;

int hello(string name) {
    cout << "Hello, " << name << endl;
    return 0;
}

int main()
{
    hello("Tyler");
    hello("Bob");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here is a simple program written in C++. This is a popular coding example used to first explain C++, but I've modified it to show off a few more key features.

#include <iostream>
Enter fullscreen mode Exit fullscreen mode

Let's start with the first line. This is known as a "Header File Library". In JavaScript, we would do something like this to import a library of functions, like underscore.js, to use instead of writing out the functions ourselves. If we want to use any of the objects from the standard library, we need to include them. This gives us the flexibility to only load the objects we need for the program we are writing. This particular library "iostream" contains objects that can read user inputs and output data.

using namespace std;
Enter fullscreen mode Exit fullscreen mode

This is a line of convenience code. Normally to use any object from the standard library, we would have to prefix it with std::. On line 5, I make use of the cout and endl from the "iostream" standard library. Without this line of code, I would need to write std::cout and std::endl every time I use them. As your program gets larger, this means you'll be rewriting std:: many times just to use objects from the standard library, so we'll save ourselves a headache and write this one line of code.

int hello(string name) {
Enter fullscreen mode Exit fullscreen mode

This is how you declare a function in C++. The function is named 'hello' and it has one parameter called 'name'. While this is similar to JavaScript, there are some noticeable differences. The only signifier that makes this a function is the parentheses after the function name and the code block that follows using braces. The other difference is the inclusion of the keywords int and string. Similar to TypeScript, these keywords indicate what values are expected from the function and the parameter. int tells C++ that the function 'hello' should always return an integer, and if it doesn't, to either truncate a decimal point off to return an integer or throw an error to indicate a different value was returned. If you're not expecting to return anything, you should put void instead. The string keyword indicates that future arguments for this function need to be string data types or else C++ will throw an error. The 'hello' function will not run until it is declared, which I will go over soon.

    cout << "Hello, " << name << endl;
Enter fullscreen mode Exit fullscreen mode

Here is the first statement of the function. Whenever you conclude a statement, you must always end it with a semicolon. JavaScript gives a little bit of leeway on this, but it cannot forgotten in C++. cout is an object from the 'iostream' library used together with the insertion operator << to output the value to the right (to the terminal by default). We can chain outputs with more <<, which allows us to use the name parameter in the output and include a line break with the endl object from the 'iostream' library. When printing to the terminal, it is common practice to include endl at the end to make sure each output to the terminal appears on a different line.

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

By returning 0 at the end of the function, it satisfies the condition of int needing an integer to be returned from 'hello'.

int main()
{
    hello("Tyler");
    hello("Bob");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

main() is a predefined function in C++ used to execute code. Any code inside its function body will be executed and there is no need to execute main() later. A few common practices I've seen in C++ code: use int and return 0 at the end of main(), and put the entire code block below the function. Inside main(), we call 'hello' twice with different arguments resulting in the following being printed to the terminal:

Hello, Tyler
Hello, Bob
Enter fullscreen mode Exit fullscreen mode

Final Thoughts on C++

I first started looking into C++ because I heard it was used in game design. With the low memory usage, I can see how it is preferable when designing large games. The object-oriented nature of the language lends itself to naturally reusing more code and even including a custom library of objects for use on smaller applications for simple hardware. Finally, the requirement of including the data type for functions and variables leads to more thoughtful planning for your code as well as a safety net for errors when a data type accidentally gets changed. I look forward to exploring C++ further and I hope you check it out too!

Happy Coding,
Tyler Meyer

Sources

Sources:
[1] https://www.techtarget.com/searchdatamanagement/definition/C

[2] https://en.wikipedia.org/wiki/Object-oriented_programming

[3] https://www.w3schools.com/cpp/cpp_intro.asp

[4] https://www.youtube.com/watch?v=MNeX4EGtR5Y

Top comments (0)