Constant Object Constant Member Function

Estimated reading: 3 minutes 24 Views

Introduction

In C++, a constant variable is a variable whose value cannot be changed once it has been initialized. The value remains constant throughout the execution of the program. Constant variables are declared using the const keyword.

Constant Object

  • Declaration: A constant object is declared by using the const keyword along with the object’s type.
  • Immutable: Once declared as const, the member variables of the object cannot be changed.
  • Calling Member Functions: A constant object can only call constant member functions of its class. This prevents any operations that might modify the object’s internal state.

Constant Member Function

  • Declaration: A member function is declared as constant by appending the const keyword to its declaration and definition.
  • Promise Not to Modify: A constant member function implicitly promises not to modify any of the non-static data members of its class.
  • Enforce Correctness: This helps the compiler enforce correctness and prevents unintentional side effects.

Example

#include <iostream>
using namespace std;
class time {
    private:
        int h, m, s;
    public:
        void print() const //constant function
        {
            cout << "Time = " << h << ":" << m << s << endl;
        }
        time(int i, int j, int k)
        {
            h = i;
            m = j;
            s = k;
        }
};
int main()
{
    const time noon(12, 0, 0); //constant object
    noon.print();

    return 0;
}

In the provided code, both constant functions and constant objects play important roles:

Constant Function (print() const):

  • The member function print() is declared as const, making it a constant member function.
  • A constant member function ensures that it does not modify the state of the object it is called on. It promises not to modify any non-static member variables of the class.
  • In this code, the print() function is responsible for displaying the time stored in the time object. Since it does not modify the object’s state, it is declared as const.
  • By declaring print() as a constant member function, it allows constant objects to call this function.

Constant Object (const time noon(12, 0, 0)):

  • An object named noon of the class time is created and declared as const. This makes noon a constant object.
  • A constant object is an object whose state cannot be modified after initialization. Any attempt to modify its state will result in a compilation error.
  • In this code, the noon object represents a specific time, 12:00:00, and it cannot be changed once created.
  • Constant objects are useful when you want to ensure that the object’s state remains unchanged throughout its lifetime, providing guarantees of immutability and safety.
  • By declaring noon as a constant object, it ensures that the print() function, which is also marked as const, can be called on it. This allows the print() function to access the noon object’s state without fear of modification.

In summary, the constant function (print() const) ensures that it does not modify the object’s state, and the constant object (const time noon(12, 0, 0)) guarantees that its state remains unchanged throughout its lifetime. These concepts work together to enforce immutability and safety in the code.

Share this

Constant Object Constant Member Function

Or copy link

CONTENTS
English