C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

const Member Variables and Functions in C++

In C++, the const keyword is used to declare variables or functions as constant, meaning their value cannot be changed after initialization. This tutorial will discuss the use of const with member variables and member functions in C++ classes.

const Member Variables

A const member variable must be initialized at the time of object creation. It cannot be changed after the object is created. To initialize a const member variable, you need to use a member initializer list in the constructor.

Example:

class Circle {
    const double pi;
    double radius;

public:
    Circle(double r) : pi(3.14159), radius(r) {} // Initialize const member variable using member initializer list

    double area() const {
        return pi * radius * radius;
    }
};

In this example, pi is a const member variable. It is initialized using a member initializer list in the constructor, and its value cannot be changed after the Circle object is created.

const Member Functions

A const member function is a function that does not modify the state of the object on which it is called. To declare a member function as const, add the const keyword after the function's parameter list.

Example:

class Circle {
    double radius;

public:
    Circle(double r) : radius(r) {}

    double getRadius() const { // Declare the function as const
        return radius;
    }

    void setRadius(double r) {
        radius = r;
    }
};

In this example, the getRadius() function is declared as const because it does not modify the state of the Circle object. The setRadius() function, however, is not const because it modifies the object's radius.

Why use const Member Functions?

Using const member functions has several benefits:

  1. It enforces a clear separation between functions that modify the object's state and those that don't, improving code readability and maintainability.
  2. It allows you to call the member function on a const object.
  3. It prevents accidental modification of the object's state within the function, ensuring that the function behaves as intended.

Complete Example

Here's a complete example that demonstrates the use of const with member variables and member functions:

#include <iostream>

class Circle {
    const double pi;
    double radius;

public:
    Circle(double r) : pi(3.14159), radius(r) {}

    double area() const {
        return pi * radius * radius;
    }

    double getRadius() const {
        return radius;
    }

    void setRadius(double r) {
        radius = r;
    }
};

int main() {
    Circle circle(5);

    std::cout << "Radius: " << circle.getRadius() << std::endl;
    std::cout << "Area: " << circle.area() << std::endl;

    circle.setRadius(10);

    std::cout << "New radius: " << circle.getRadius() << std::endl;
    std::cout << "New area: " << circle.area() << std::endl;

    return 0;
}

In conclusion, the const keyword in C++ is used to declare member variables and member functions as constant. const member variables must be initialized at the time of object creation and cannot be changed afterward. const member functions do not modify the state of the object on which they are called, providing a clear separation between modifying and non-modifying functions and ensuring that the function behaves as intended.

  1. Using const in C++ class members:

    • Description: Introduces the concept of using const for class members, making them read-only and preventing modification after initialization.
    • Example Code:
      class MyClass {
      public:
          const int constantValue;
      
          MyClass(int value) : constantValue(value) {}
      };
      
  2. Const member functions in C++ classes:

    • Description: Introduces const member functions that do not modify the object's state, allowing them to be called on const objects.
    • Example Code:
      class MyClass {
      public:
          void nonConstFunction() {
              // Modifying object state
          }
      
          void constFunction() const {
              // Not modifying object state
          }
      };
      
  3. Mutable keyword in C++ and const members:

    • Description: Introduces the mutable keyword, allowing modification of const members within a const member function.
    • Example Code:
      class MyClass {
      public:
          mutable int mutableValue;
      
          void constFunction() const {
              // Can modify mutableValue here
          }
      };
      
  4. C++ const member variable initialization:

    • Description: Demonstrates various ways to initialize const member variables during object construction.
    • Example Code:
      class MyClass {
      public:
          const int constantValue;
      
          MyClass(int value) : constantValue(value) {}
      };