Day 14 Class and Object in C++
Day 14 Class and Object in C++
So far, we learned about functions and variables. Sometimes it's desirable to put
related functions and data in one place so that it's logical and easier to work with.
Suppose, we need to store the length, breadth, and height of a rectangular room and
calculate its area and volume.
To handle this task, we can create three variables, say, length, breadth,
and height along with the functions calculateArea() and calculateVolume().
However, in C++, rather than creating separate variables and functions, we can also
wrap these related data and functions in a single place (by creating objects). This
programming paradigm is known as object-oriented programming.
But before we can create objects and use them in C++, we first need to learn
about classes.
C++ Class
We can think of a class as a sketch (prototype) of a house. It contains all the details
about the floors, doors, windows, etc. Based on these descriptions we build the house.
House is the object.
Create a Class
A class is defined in C++ using keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a
semicolon at the end.
class className {
// some data
// some functions
};
For example,
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
return length * breadth;
}
double calculateVolume(){
return length * breadth * height;
}
};
The variables length, breadth, and height declared inside the class are known as data
members. And, the functions calculateArea() and calculateVolume() are known
as member functions of a class.
C++ Objects
When a class is defined, only the specification for the object is defined; no memory or
storage is allocated.
To use the data and access functions defined in the class, we need to create objects.
We can access the data members and member functions of a class by using a . (dot)
operator. For example,
room2.calculateArea();
This will call the calculateArea() function inside the Room class for object room2.
Similarly, the data members can be accessed as:
room1.length = 5.5;
In this case, it initializes the length variable of room1 to 5.5.
Example 1: Object and Class in C++ Programming
// Program to illustrate the working of// objects and class in C++ Programming
#include <iostream>using namespace std;
// create a classclass Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
Output
In this program, we have used the Room class and its object room1 to calculate the
area and volume of a room.
In main(), we assigned the values of length, breadth, and height with the code:
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
We then called the functions calculateArea() and calculateVolume() to perform the
necessary calculations.
Note the use of the keyword public in the program. This means the members are
public and can be accessed anywhere from the program.
As per our needs, we can also create private members using the private keyword. The
private members of a class can only be accessed from within the class. For example,
class Test {
private:
int a;
void function1() { }
public:
int b;
void function2() { }
}
Here, a and function1() are private. Thus they cannot be accessed from outside the
class. On the other hand, b and function2() are accessible from everywhere in the
program.
Example 2: Using public and private in C++ Class
// Program to illustrate the working of// public and private in C++ Class
#include <iostream>using namespace std;
class Room {
private:
double length;
double breadth;
double height;
public:
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
Output
The above example is nearly identical to the first example, except that the class
variables are now private.
Since the variables are now private, we cannot access them directly from main().
Hence, using the following code would be invalid:
// invalid code
obj.length = 42.5;
obj.breadth = 30.8;
obj.height = 19.2;
Instead, we use the public function initData() to initialize the private variables via the
function parameters double len, double brth, and double hgt.