C++ Classes and Objects Overview

 

C++ OOP tutorial

Object-oriented programming (OOP) is a principle of programming centered around representing real-world objects as part of code. This is done specifically using two concepts: classes and objects.

What are Classes and Objects in C++?

Classes, user-defined data types, are templates from which objects can be created. In turn, an object is an instance of a class. You can say that objects are the physical entity used in a running program for a specific purpose, whereas the class is the logical thing used to bind data and functions. A class can be thought of as the blueprint for an object.

For example, let’s consider the class Student. There may be many students with different names, taking different subjects, but they will share overlapping properties, such as a First Name, Last Name, a GPA, and so on. In this case, Student is the class and First Name, Last Name, GPA, and so forth are their properties.

We can use Object-oriented programming to conceptualize students, food, cars, and more real-life things and represent them to be manipulated in code. The way we do this is through Object-oriented programming principles. Among other languages, the modern C++ language uses these concepts as building blocks for its own object-oriented feature.

A cross-platform, general-purpose programming language, C++ was created by Bjarne Stroustrup as an extension of the C language. Essentially starting out as “C with Classes,” the language has since expanded to include generic, object-oriented, and functional features as well as being able to facilitate low-level memory manipulation.

The main purpose of C++ is to add object orientation to C programming and classes are the central feature that allows for object-oriented programming.

Read: An Object-oriented Programming Primer

Classes in C++

A Class is the building block of object-oriented programming and we start creating a class in C++ by using the keyword class, followed by a user-defined name. The body of the class that follows is defined within a set of curly brackets { } and a semicolon ; at the end to signify it is terminated. Here is an example of how to create a class in C++:

class Student {
	Access modifier:

	Data Members/variables;

	Member Functions() {}
};

By default, a class’s modifier is private, meaning that the data members made here are only accessible within this class. In order to use the data members and functions outside the class, you will need to make them public by using the public: modifier.

class Student {
public:

	Data Members/variables;

	Member Functions() {}
};

Data members you can declare are variables you want to be associated with your class; attributes for an object you perceive need to be manipulated:

class Student {
public:

	string name;
	float gpa;
	list classes;

	Member Functions() {}
};

Read: Creating Classes and Objects in Visual Basic

Objects in C++

Next, you will want to define the object. After declaring the class, only the specifications of an object are defined. No memory or storage is used at this point. In order to use the data and gain access to the class defined functions that manipulate that data you will need to start creating objects.

You start by declaring the type of object you want to create, then the class, followed by a name for that object. You can follow this up by building this first object up with its associated attributes. After that, you can use those attributes like any other variable, starting with printing them out, as in the following example:

int main {
Student Student01;
Student01.name = “William”
Student01.gpa = 2.2
Student01.classes = {“Math”, “Science”, “History”, “English”}

cout << “Name: “ << Student01.name << end1;
cout << “GPA: “ << Student01.gpa << end1;
cout << “Classes: “<< end1;
for (string classtitles : Student01.classes){
	cout << classtitles << end1;
}
}

Now we have our first object. If we want to create more, then we will need to repeat the process again for any number of unique students. But this will surely go against several coding principles, especially the one about keeping things simple. In order not to inundate yourself with repetitive and bloated code, you will want to consider constructors and class methods.

Constructors and Class Methods in C++

A constructor is a special method that will be called by the compiler every time an object is being created from a specific class. There are two conditions for forming a constructor; first, it must have the same name as the class it is in, and second, it must not have a return type.

class Student {
public:

	string name;
	float gpa;
	list classes;

	//Parameterized constructor
	Student(string Name, float GPA) {
		name = Name;
		gpa = GPA;
}
};

Now we can simplify our earlier code snippet:

int main {
Student Student01(“William”, “2.2”);
Student01.classes = {“Math”, “Science”, “History”, “English”}

cout << “Name: “ << Student01.name << end1;
cout << “GPA: “ << Student01.gpa << end1;
cout << “Classes: “<< end1;
for (string classtitles : Student01.classes){
	cout << classtitles << end1;
}
}

This is an example of a parameterized constructor, which takes in attributes as variables and helps construct an object. There are also default constructors and copy constructors.

Read: Explore Constructors and Destructors in C++

A default constructor is a constructor that does not take in any parameters and resorts to prepared values for an object when this constructor is called. When no constructor is made, or one is made with no body, the compiler will provide a default constructor implicitly. Note that a user can make their own default constructor with values they can set as default as well.

class Student {
public:

	string name;
	float gpa;
	list classes;

	//default constructor
	Student() {
		name = New Student;
		gpa = 0;
	}

	//Parameterized constructor
	Student(string Name, float GPA) {
		name = Name;
		gpa = GPA;
}
};

A copy constructor is a method that initializes a new object using another object of the same class.

class Student {
public:

	string name;
	float gpa;
	list classes;

	//default constructor
	Student() {
		name = New Student;
		gpa = 0;
	}

	//Parameterized constructor
	Student(string Name, float GPA) {
		name = Name;
		gpa = GPA;
}

//Copy constructor
Student(const Student &Student01) {
	name = Student01.name;
	gpa = Student01.gpa;
}
};

In the same way, one could create class methods that take in attributes and set in parameters for an object or produce more data:

class Student {
public:

	string name;
	float gpa;
	list classes;

	//class functions declaration
	float getGPA(float GPA);
	float setGPA(float GPA);

	//Parameterized constructor
	Student(string Name, float GPA) {
		name = Name;
		gpa = GPA;
}

};

//class functions
float getGPA(void) {
	return gpa
}
void setGPA(float GPA) {
	gpa = GPA
}

Other Features of C++ Object-oriented Programming

One more instance member function to know about is the destructor, which is a method that is called automatically to destroy an object that was made. The destructor’s properties include not having arguments, no return type, they cannot be declared static or const, and more. What happens when this method is called is that the program ends, the function ends, a block holding local variables ends, and a delete operator is called. Destructors have the same name as the class but are preceded by a tilde (~). They do not take any argument and do not return anything.

Languages with Object-oriented tendencies can feature a concept called multiple inheritance, which is also a feature within the C++ language. Multiple Inheritance itself is the way in which a class can inherit attributes from more than one parent class. Developers can use this characteristic of C++ to compartmentalize memory and make more classes less obtrusive of a job.

Conclusion of C++ Objects and Classes Tutorial

C++ is a versatile and powerful general-purpose language designed for system programming and embedded software, for software with constrained resources as well as large systems, with efficiency, performance, and flexibility as its features. You can find C++ in many areas, as it’s been useful in the development of many low-level applications, such as drivers and kernels, as well some higher-level applications, like desktop applications, servers, video games, and more.

The object-orientation of C++ allows for this versatility, proving that it can be a true asset in a developer’s toolset.

Read more C++ programming tutorials and how-tos.

William Hreiki
William Samer Hreiki is an IT professional with experience in logistics, web development, and cybersecurity. After studying political science abroad, he achieved a certificate in cyber-criminology at Boston University and a Master of Science in Cybersecurity at Northeastern University. Dedicated to aiding in networking and development best practices, William immerses himself in projects involving Internet of Things and Blockchain technology. In his spare time, he enjoys 3d modeling and exercising.

More by Author

Must Read