Writing Object-Oriented Code in C++

A Comprehensive Tutorial

Matteo Possamai
CodeX
3 min readJun 11, 2023

--

Photo by Przemyslaw Marczynski on Unsplash

Introduction:

C++ is a highly popular programming language renowned for its ability to write robust and efficient code, particularly when it comes to object-oriented programming (OOP).

OOP allows developers to organize their code into reusable modules, enhancing code readability, maintainability, and scalability. In this tutorial, we will delve into the fundamentals of writing object-oriented code in C++, exploring the proper usage of the class keyword, constructor functions, access modifiers, attributes, and the essential “this” keyword. By following these best practices, you’ll be equipped to write clean and efficient code that adheres to industry-standard OOP principles.

Understanding the Class Keyword: The class keyword serves as the foundation for creating objects and defining their behaviour in C++. It encapsulates related data and functions into a single unit, facilitating code modularity and reusability.

To define a class, begin with the class keyword, followed by the class name and a pair of curly braces. Let’s illustrate this with an example:

class MyClass {
// Class definition goes here
};

Constructor Functions:

Constructor functions allow you to initialize objects of a class. They are special member functions that are automatically called when an object is created.

The constructor’s primary purpose is to ensure that the object is properly initialized before any operations are performed on it. In C++, the constructor function has the same name as the class itself and doesn’t have a return type. Here’s an example:

class Car {
public:
Car() {
// Constructor code goes here
}
};

Access Modifiers

Access modifiers determine the accessibility of class members (attributes and functions) from outside the class. C++ provides three access modifiers: public, private, and protected. Public members are accessible from anywhere, private members are only accessible within the class, and protected members are accessible within the class and its derived classes. Consider the following example:

class Employee {
private:
int salary; // Private attribute
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};

Attributes and Accessing Them:

Attributes, also known as member variables, represent the data associated with a class. They store information that is relevant to the class’s behaviour and state.

To declare an attribute, include its data type and name within the class definition. Within class methods, you can access the attributes using the “this” pointer. Take a look at this example:

class Circle {
private:
double radius; // Private attribute
public:
void setRadius(double r) {
this->radius = r;
}
double getArea() {
return 3.14 * this->radius * this->radius;
}
};

The “this” Keyword: The “this” keyword is a pointer that refers to the current object instance within a class. It is primarily used to differentiate between class attributes and local variables or parameters that share the same name. By using “this”, you can access class members and invoke member functions. Let’s demonstrate the usage of the “this” keyword in the previous Circle class example.

Conclusion:

In this tutorial, we explored the fundamental concepts of writing object-oriented code in C++. We started by understanding the class keyword and how it forms the building block for creating objects.

We then delved into constructor functions, which allow us to initialize objects properly. Access modifiers provided us with control over the visibility of class members.

We also examined attributes, their declaration, and how to access them using the “this” pointer. By mastering these concepts, you are well on your way to writing clean, modular, and maintainable code in C++.

Remember, object-oriented programming in C++ offers numerous advantages, including code reusability, encapsulation, and modularity. By following the principles covered in this tutorial, you’ll be equipped to write efficient and elegant code, adhering to industry best practices. Happy coding!

--

--

Matteo Possamai
CodeX

Computer science student, technology enthusiast, interested in backend services, software development and Open Source.