C++ Classes & Objects
C++ adds object-oriented programming to the C language, supporting object-oriented design. Classes are the core feature of C++ and are often referred to as user-defined types.
Classes specify the form of an object, including its data representation and methods for processing the data. Data and methods within a class are called members. Functions within a class are known as member functions.
C++ Class Definition
Defining a class is essentially defining a blueprint for a data type. This does not define any data, but it defines what the class name means, i.e., what an object of the class will include and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name. The body of the class is enclosed in curly braces. The class definition must be followed by a semicolon or a declaration list. For example, we define the Box data type using the keyword class as follows:
class Box
{
public:
double length; // Length of the box
double breadth; // Breadth of the box
double height; // Height of the box
};
The keyword public determines the access attributes of the class members. Public members are accessible outside the class within the scope of the class object. You can also specify class members as private or protected, which we will discuss later.
Defining C++ Objects
A class provides the blueprint for objects, so objects are created based on the class. Declaring an object of a class is similar to declaring a variable of a basic type. The following statements declare two objects of class Box:
Box Box1; // Declare Box1, of type Box
Box Box2; // Declare Box2, of type Box
Objects Box1 and Box2 have their own data members.
Accessing Data Members
Public data members of an object of a class can be accessed using the direct member access operator .
.
To better understand these concepts, let's try the following example:
Example
#include <iostream>
using namespace std;
class Box
{
public:
double length; // Length
double breadth; // Breadth
double height; // Height
// Member function declarations
double get(void);
void set(double len, double bre, double hei);
};
// Member function definitions
double Box::get(void)
{
return length * breadth * height;
}
void Box::set(double len, double bre, double hei)
{
length = len;
breadth = bre;
height = hei;
}
int main()
{
Box Box1; // Declare Box1, of type Box
Box Box2; // Declare Box2, of type Box
Box Box3; // Declare Box3, of type Box
double volume = 0.0; // To store the volume
// Box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// Box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// Volume of Box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1: " << volume << endl;
// Volume of Box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2: " << volume << endl;
// Box 3 specification
Box3.set(16.0, 8.0, 12.0);
volume = Box3.get();
cout << "Volume of Box3: " << volume << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Volume of Box1: 210
Volume of Box2: 1560
Volume of Box3: 1536
It is important to note that private and protected members cannot be directly accessed using the direct member access operator (.). We will learn how to access private and protected members in subsequent tutorials.
Detailed Explanation of Classes & Objects
So far, we have gained a basic understanding of classes and objects in C++. The following list also includes other concepts related to C++ classes and objects, which can be learned by clicking on the respective links.
Concept | Description |
---|---|
Class Member Functions | Class member functions are those whose definitions and prototypes are written inside the class definition, just like other variables in the class. |
Class Access Modifiers | Class members can be defined as public, private, or protected. By default, they are defined as private. |
Constructors & Destructors | A constructor is a special function that is called when a new object is created. A destructor is a special function that is called when the created object is deleted. |
C++ Copy Constructor | The copy constructor is a special constructor that initializes a new object using an object of the same class that was created earlier. |
C++ Friend Functions | Friend functions can access the private and protected members of a class. |
C++ Inline Functions | With inline functions, the compiler attempts to expand the code within the function body at the point of function call. |
C++ this Pointer | Each object has a special pointer this, which points to the object itself. |
C++ Pointer to Class | A pointer to a class is similar to a pointer to a structure. In fact, a class can be considered as a structure with functions. |
C++ Static Members of Class | Both data members and function members of a class can be declared as static. |