C++ Classes and Objects
A class is a user-defined data type and can be used as the blueprint for creating different objects.
An object is an instance of a class. C++ class objects exhibit the properties and behaviors defined by its class.
A class can contain data members (i.e. variables) and member functions (i.e. functions) to describe the state and behavior of an object.
C++ data members and member function names are case sensitive.
A class definition has the following general syntax:
class class_name {
// data members
// member functions
};
Example:
class Rectangle {
public:
// data members
int length;
int breadth;
// member function
int computeArea() {
return length * breadth;
}
};
NOTE: In C++, class members are by default private. The members with private access specifier can only be accessed from within the class. So, we cannot access them directly from main()
. However, a member with public access specifier can be accessed from outside the class.
Syntax to create object in C++ language:
class_name object_name;
Example:
Rectangle rect;
Refer to a data member / member function:
This is accomplished by stating the name of the object, followed by a period (dot), followed by the name of the member inside the object:
object.member
Example:
// initializing data members
rect.length = 20;
rect.breadth = 15;
// calling member function
int area = rect.computeArea();
Example 1: all the members are public
Below is an example of objects and classes related to the Rectangle class that defines two public data members namely length and breadth. The class contains one public member function named computeArea().
#include <iostream>
using namespace std;
class Rectangle {
public:
// data members int length; int breadth;
// member function
int computeArea() { return length * breadth; } };
int main() {
// creating object of Rectangle class Rectangle rect;
// initializing public data members
rect.length = 20;
rect.breadth = 15;
// calling public member function
int area = rect.computeArea();
// printing area for rect
cout << "AREA : " << area;
return 0; }
Output:
AREA: 300
Example 2: private data members and public member functions
Here, we have the modified Rectangle class (taken from the previous example) with two private data members namely length and breadth. Since, the data members are declared with private access specifier; we cannot access them directly from main()
.
So, we initialize these data members by introducing one public member function in the class definition named inputData(). However, we keep the member function called computeArea() in the class like the previous example.
In this example, we have created two objects of the Rectangle class. See the program below.
#include <iostream>
using namespace std;
class Rectangle {
private:
// data members int length; int breadth;
public:
// member functions
void inputData(int l, int b) {
length = l;
breadth = b;
}
int computeArea() { return length * breadth; } };
int main() {
// creating objects of Rectangle class Rectangle rect1, rect2;
// initializing data members of rect1 rect1.inputData(20, 15);
// computing area for rect1
cout << "AREA1 : " << rect1.computeArea() << endl;
// initializing data members of rect2
rect2.inputData(30, 20);
// computing area for rect2
cout << "AREA2 : " << rect2.computeArea() << endl;
return 0; }
Output:
AREA1: 300
AREA2: 600
Example 3: member functions defined outside the class definition
In the previous example, we define the member functions within the class definition.
We can also define the member functions outside the class definition. To do this, we have to first declare these functions within the class scope like this:
// class definition
class Rectangle {
private:
// data members
int length;
int breadth;
public:
// member functions declarations
void inputData(int l, int b);
int computeArea();
};
Then, the member functions (declared above) can be defined outside the class using the scope resolution operator ::
as follows −
// member functions definitions
void Rectangle::inputData(int l, int b) {
length = l;
breadth = b;
}
int Rectangle::computeArea() { return length * breadth; }
Here, only important point is that we have to use the class name just before ::
operator, followed by the name of the function.
See the complete program below.
#include <iostream>
using namespace std;
class Rectangle {
private:
// data members int length; int breadth;
public:
// member functions declarations
void inputData(int l, int b);
int computeArea();
};
// member functions definitions
void Rectangle::inputData(int l, int b) {
length = l;
breadth = b;
}
int Rectangle::computeArea() {
return length * breadth;
}
int main() {
// creating objects of Rectangle class
Rectangle rect1, rect2;
// initializing data members of rect1
rect1.inputData(20, 15);
// computing area for rect1
cout << "AREA1 : " << rect1.computeArea() << endl;
// initializing data members of rect2
rect2.inputData(30, 20);
// computing area for rect2
cout << "AREA2 : " << rect2.computeArea() << endl;
return 0;
}
Output:
AREA1: 300
AREA2: 600