by Dinesh Thakur Category: Classes in C++

The const qualifier is used with the variables of basic data types to prevent them from being modified by the function. In a similar way, const qualifier can also be applied to member functions, member function arguments and the objects of a class.

const Member Functions

 

A const or a constant member function can only read or retrieve the data members of the calling object without modifying them. If such a member function attempts to modify any data member of the calling object, a compile-time error is generated.

The syntax for defining a const member function is

return_type function_name (parameter_list) const

{

//body of the member function

}

To understand the concept of const member functions, consider the class weight. As the member function putdata () does not alter any data member of the calling objects, it can be declared as const. In addition, the sum_weight () member function simply adds the data members of the calling object and the object passed as argument without modifying the calling object, hence this function can also be declared as const. However, the function getdata () cannot be declared as const, as it assigns values to the data members of the calling object.

 

Example: Declaration and definition of const member functions

 

//Declaration of Const member functions inside the class

 

void putdata() const;

weight sum_weight (weight) const;

 

//Definition of const member functions outside the class

void weight: : putdata () const //display weight

{

//body of the function

weight weight :: sum_weight (weight w2) const

{

// const member function

//body of the function

//gram=100; //Error: cannot modify const object

}

//Calling const member functions inside the main() function

w3=w1.sum_weight(w2) ;

 

Now, if the sum_weight () function 'attempts to change the values of data members of object w1 (that is the calling object) within the function, a compile-time error Cannot modify a const object is raised.

 

const Member Function Arguments

 

A const member function cannot modify the values of data members of the calling object. However, it can alter the values of the data members of the objects passed by reference. Thus to prevent the member function from modifying the arguments passed to it by reference, the const qualifier can be used with member function arguments in both declaration and definition of the member function.

 

To understand the concept of const member function arguments, consider this example.

 

Example: A program to demonstrate const member function arguments

 

#include<iostream.h>

 

class weight

{

int kilogram;

int gram;

 

public:

 

void getdata();

void putdata() const;

weight sum_weight(const weight&) const;

 

} ;

void weight::getdata()

{

cout<<"/n Kilograms:";

cin>>kilogram;

cout<<"Grams:";

cin>>gram;

}

void weight :: putdata () const

{

cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";

}

weight weight::sum_weight(const weight& w2) const

{

 

weight temp;

temp.gram = gram + w2.gram;

temp.kilogram = temp.gram/1000;

temp.gram = temp.gram%1000;

temp.kilogram += kilogram+w2.kilogram;

return (temp) ;

}

int main ()

{

weight w1,w2 ,w3;

 

 

 

cout<<"Enter weight in kilograms and grams\n";

 

cout<<"\n Enter weight #1" ;

w3=w1.sum_weight (w2);

 

w1.getdata();

 

cout<<" \n Enter weight #2" ;

 

w2.getdata();

 

cout<<"/n Weight #1 = ";

 

w1.putdata();

 

cout<<"Weight #2 = ";

 

w2.putdata();

 

cout<<"Total Weight = ";

 

w3.putdata();

w3.sum_weight(w1,w2);

 

return 0;

}

 

In this example, the function sum_weight () as well as the argument w2 passed to it by reference are declared as const. Hence, if an attempt is made to alter any data of either w1 (the calling object) or w2 (object passed by reference), a compile-time error is generated.

 

canst Objects

 

Like member functions and member function arguments, the objects of a class can also be declared as const. An object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object. A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data members of const objects results in a compile-time error.

The syntax for declaring a const object is

             const class_name object_name;

Whenever an object is declared as const, it needs to be initialized at the time of declaration. However, the object initialization while declaring is possible only with the help of constructors.

 

 





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles