Object-Oriented Thinking in C++: Constant Member Functions

C++ also enables you to specify a constant member function to tell the compiler that Point the function should not change the value of any data fields in the object.

You used the const keyword to specify a constant parameter to tell the compiler that the parameter should not be changed in the function. You can also use the const keyword to constant function     specify a constant member function (or simply constant function) to tell the compiler that the function does not change the data fields in the object. To do so, place the const keyword at the end of the function header. For example, you may redefine the Circle class in Listing 10.6 as shown in Listing 10.9, and the header file is implemented in Listing 10.10.

Listing 10.9 CircleWithConstantMemberFunctions.h

1 #ifndef CIRCLE_H
2
#define CIRCLE_H
3
4
class Circle
5 {
6   
public:
7    Circle();
8    Circle(
double);
9   
double getArea() const;
10   
double getRadius() const;
11   
void setRadius(double);
12   
static int getNumberOfObjects();
13
14   
private:
15   
double radius;
16   
static int numberOfObjects;
17 };
18
19
#endif

LisTing 10.10 CircleWithConstantMemberFunctions.cpp 

1 #include “CircleWithConstantMemberFunctions.h”
2
3
int Circle::numberOfObjects = 0;
4
5
// Construct a circle object
6 Circle::Circle()
7 {
8    radius =
1;
9    numberOfObjects++;
10 }
11
12
// Construct a circle object
13 Circle::Circle(double newRadius)
14 {
15     radius = newRadius;
16     numberOfObjects++;
17 }
18
19
// Return the area of this circle
20 double Circle::getArea() const
21 {
22     
return radius * radius * 3.14159;
23 }

24
25
// Return the radius of this circle
26 double Circle::getRadius() const
27 {
28     
return radius;
29 }
30
31
// Set a new radius
32 void Circle::setRadius(double newRadius)
33 {
34    radius = (newRadius >=
0) ? newRadius : 0;
35 }
36
37
// Return the number of circle objects
38 int Circle::getNumberOfObjects()
39 {
40   
return numberOfObjects;
41 }

Only instance member functions can be defined as constant functions. Like constant param­eters, constant functions are for defensive programming. If your function mistakenly changes the value of data fields in a function, a compile error will be reported. Note that you can define only instant functions constant, not static functions. An instance get function should always be defined as a constant member function, because it does not change the contents of the object.

If a function does not change the object being passed, you should define the parameter constant using the const keyword like this:

void printCircle(const Circle& c)

{

cout << “The area of the circle of “

<< c.getRadius() << ” is ” << c.getArea() << endl;

}

Note that this code will not compile if the getRadius() or getArea() function is not defined const. If you use the Circle class defined in Listing 9.9, the preceding function will not compile, because the getRadius() and getArea() are not defined const. However, if you use the Circle class defined in Listing 10.9, the preceding function will compile, because the getRadius() and getArea() are defined const.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *