C++: Const Member Function Explained With Examples
1. Introduction
Const member function implies that the member function will not change the state of the object. The data member of the class represents the “state” of the object. So, the const member function grantees that it will not change the value in the data member till it returns to the caller. Let us see this with a code example.
2. The Code Example
Look at the C++ example code below. I explained the code in section 3.
// TestIt.cpp : Defines the entry point
// for the console application.
//
using namespace std;
class CRect
{
//Sample 01: Private Members
private:
int m_len;
int m_width;
public:
//Sample 02: Constructor
CRect(int L, int W)
{
m_len = L;
m_width = W;
}
//Sample 03: Print data members
void print()
{
cout << "Lenght = " << m_len
<< " Width = "<< m_width << endl ;
}
//Sample 04: Const Member Function
int GetArea()
{
m_len++ ;
return m_len * m_width;
}
};
int main()
{
//Sample 05: Create Rectangle Object
CRect rct(10,5);
//Sample 06: Print the dimension
rct.print();
//Sample 07: Print the Area of Rectangle
cout << "Area = " << rct.GetArea();
}
3. Explanation of the Example
1) To explain the const member function, a class called CRect is created. It has two member variables named as m_len and m_width. These member variables represent the state of the class. Both the member variables are in the private scope. That means they can be accessed only inside the class member functions.
//Sample 01: Private Members
private:
int m_len;
int m_width;
2) The constructor of the class sets the length and width of the CRect object. The constructor takes length (L) and width (W) of the rectangle as a parameter. These parameters are copied to the local members m_len and m_width inside the body of the constructor.
//Sample 02: Constructor
CRect(int L, int W)
{
m_len = L;
m_width = W;
}
3) A print member function prints the internal state of the object. That means it prints the length and width of the CRect object. Note that we used iostream object to print the values in the console output. The cout is the iostream object and it flushes the string through the operator <<. The Operator << pushes the output to the console. The Operator >> pushes the input from console to the program. We will see cout in some other article with more details.
//Sample 03: Print data members
void print()
{
cout << "Lenght = " << m_len
<< " Width = "<< m_width << endl ;
}
4) The GetArea member function calculates the area of the Rectangle by multiplying its internal members (i.e.) m_len and m_width. Also, note the const keyword at the end of the function parameter list. This tells that the member function GetArea is read-only function and it does not the change the data members m_len and m_width at any point in time.
Changing the member variable inside the function body will be detected by the compiler and the compiler will not allow changing the state of the object inside the constant member function. The picture shows how const member function differ from normal function. A normal function can access the member variable of the class and can replace the value contained in the member variable. But const member functions allows reading the data from the member variables and it does not allow writing a new value.
//Sample 04: Const Member Function
int GetArea() const
{
return m_len * m_width;
}
5) In the program entry, we created a rct object with the dimension of 10x5. Now, I can say the state of the rct object as length 10 and width 5. The print() member function called on the rct object will print the state of the rct object. Finally, we call the member function of the rct object GetArea(). Note that the member function is a const member function and it guarantees that this function does not make any changes to the rct object.
//Sample 05: Create Rectangle Object
CRect rct(10,5);
//Sample 06: Print the dimension
rct.print();
//Sample 07: Print the Area of Rectangle
cout << "Area = " << rct.GetArea();
© 2013 sirama
Comments
Ayisha on February 08, 2019:
It was useful and clear explanation thank you
Shalini on November 08, 2018:
Nice
sirama (author) on August 06, 2018:
Thank You pokhi
pokhi on August 06, 2018:
very good article and understandable..........
ibadee on July 06, 2018:
helpful it was!
Dhiraj on June 02, 2017:
Nice tutorial and heads up for good coding
sirama (author) on May 21, 2013:
So, it also should be a const method, right?
Definitely that is right. That is how people design their software. To keep the hub simple and explain the concept in picture I just picked a print method as Non const.
Narendra Kumar on May 21, 2013:
Hi,
What you said about GetArea() method is right.
But, why you are not making the print() method also as const?
print() method also doesn't make any changes to data.
So, it also should be a const method, right?
Thanks,
Narendra