Skip to content
Related Articles

Related Articles

Const member functions in C++

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Jun, 2022

Constant member functions are those functions which are denied permission to change the values of the data members of their class. To make a member function constant, the keyword “const” is appended to the function prototype and also to the function definition header.

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 member of const objects results in a compile-time error. 

Syntax: 

(i)    For function declaration within a class.

<return_type> <function_name>() const

Example:
Int get_data() const;

(ii)    For function definition within the class declaration.

<return_type> <function_name>() const
{
         //function body
}

Example:
int get_data() const
{
               //function body

}

(iii)    For function definition outside the class.

<return_type> <class_name> : : <function_name>() const
{
         //function body
}

Example:
int Demo :: get_data() const
{

}
  • When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.
  • 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.

A function becomes const when the const keyword is used in the function’s declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.
Following is a simple example of a const function. 

C++




// Example: Not a constant member function.
#include<iostream>
using namespace std;
 
class Demo
{
    int x;
     
      public:
     
    void set_data(int a)
    {
        x=a;
    }
  
    int get_data()
     {
         ++x;
         return x;
     }
  
};
 
 
main()
{
    Demo d;
    d.set_data(10);
    cout<<endl<<d.get_data();
 
    return 0;
}

Output

11

C++




// Example: of Constant member function
 
#include<iostream>
using namespace std;
 
class Demo
{
    int x;
 
      public:
     
    void set_data(int a)
    {
        x=a;
    }
  
     int get_data() const           //constant member function
     {
         ++x;                 // Error while attempting to modify the data member
         return x;
     }
  
};
 
 
main()
{
    Demo d;
    d.set_data(10);
    cout<<endl<<d.get_data();
 
    return 0;
}

C++




// Example: Constant member function defined outside the class
 
#include<iostream>
using namespace std;
 
class Demo
{
    int x;
 
      public:
     
    void set_data(int);
  
     int get_data() const;
  
};
 
void Demo::set_data(int a)
{
    x=a;
}
 
 int Demo::get_data() const
 {
     return x;
 }
 
main()
{
    Demo d;
    d.set_data(10);
    cout<<endl<<d.get_data();
 
    return 0;
}

Output

10

CPP




#include <iostream>
using namespace std;
 
class Test {
    int value;
 
public:
    Test(int v = 0) { value = v; }
 
    int getValue() const { return value; }
};
 
int main()
{
    Test t(20);
    cout << t.getValue();
    return 0;
}

Output

20

When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects. 
For example the following program has compiler errors.  

CPP




#include<iostream>
using namespace std;
 
class Test {
    int value;
public:
    Test(int v = 0) {value = v;}
    int getValue() {return value;}
};
 
int main() {
    const Test t;
    cout << t.getValue();
    return 0;
}

Output: 

 passing 'const Test' as 'this' argument of 'int 
Test::getValue()' discards qualifiers

Let’s look at another example:  

CPP




// Demonstration of constant object,
// show that constant object can only
// call const member function
#include<iostream>
using namespace std;
class Demo
{
    int value;
    public:
    Demo(int v = 0) {value = v;}
    void showMessage()
    {
        cout<<"Hello World We are Tushar, "
        "Ramswarup, Nilesh and Subhash Inside"
        " showMessage() Function"<<endl;
    }
    void display()const
    {
        cout<<"Hello world I'm Rancho "
        "Baba Inside display() Function"<<endl;
    }
};
int main()
{
   //Constant object are initialised at the time of declaration using constructor
    const Demo d1;
    //d1.showMessage();Error occurred if uncomment.
    d1.display();
    return(0);
}

Output

Hello world I'm Rancho Baba Inside display() Function
OUTPUT : Hello world I'm Rancho Baba Inside display() Function

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!