Classes are the key component of C++ that allow object-oriented programming and are sometimes referred to as user-defined types. The basic goal of C++ programming is to bring object orientation to the C programming language.
An object's shape is specified by a class, which combines methods for handling data and data representation into one tidy package. Members of a class are the data and functions contained within the class.
C++ Class Definitions
When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the room data type using the keyword class as follows −
class Room {
public:
double length; // Length of a room
double breadth; // Breadth of a room
double height; // Height of a room
};
The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. We can also specify the members of a class as private or protected which we will discuss in a sub-section.
Define C++ Objects
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box −
Box Room1; // Declare Room1 of type Room
Box Room2; // Declare Room2 of type Room
Both of the objects Box1 and Box2 will have their own copy of data members.
Accessing the Data Members
The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear −
#include <iostream>
using namespace std;
class Room {
public:
double length; // Length of a room
double breadth; // Breadth of a room
double height; // Height of a room
};
int main() {
Room Room1; // Declare Room1 of type Room
Room Room2; // Declare Room2 of type Room
double volume = 0.0; // Store the volume of a box here
// Room 1 specification
Room1.height = 5.0;
Room1.length = 6.0;
Room1.breadth = 7.0;
// Room 2 specification
Room2.height = 10.0;
Room2.length = 12.0;
Room2.breadth = 13.0;
// volume of Room 1
volume = Room1.height * Room1.length * Room1.breadth;
cout << "Volume of Room1 : " << volume <<endl;
// volume of Room 2
volume = Room2.height * Room2.length * Room2.breadth;
cout << "Volume of Room2 : " << volume <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Volume of Room1 : 210
Volume of Room2 : 1560
It is important to note that private and protected members can not be accessed directly using direct member access operator (.).
We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.