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 Object in C++

In C++, a const object is an instance of a class that cannot be modified after it is created. This tutorial will show you how to create and work with const objects in C++.

Creating const Objects

To create a const object, use the const keyword before the class name when declaring the object.

Example:

class MyClass {
public:
    int x;

    MyClass(int a) : x(a) {}

    int getX() const {
        return x;
    }
};

int main() {
    const MyClass obj(10); // Create a const object
    return 0;
}

In this example, we create a const object obj of the MyClass class. Since obj is a const object, its data members cannot be modified after it is created.

Working with const Objects

  • Accessing data members: You cannot directly access or modify the data members of a const object. To access the data members, you need to use const member functions.
int main() {
    const MyClass obj(10);

    // obj.x = 20; // Error: Cannot modify a const object
    std::cout << "x: " << obj.getX() << std::endl; // Access data member using a const member function

    return 0;
}
  • Calling member functions: You can only call const member functions on a const object. Non-const member functions are not allowed because they might modify the object.
class MyClass {
public:
    int x;

    MyClass(int a) : x(a) {}

    int getX() const {
        return x;
    }

    void setX(int a) {
        x = a;
    }
};

int main() {
    const MyClass obj(10);

    std::cout << "x: " << obj.getX() << std::endl; // Allowed: Calling a const member function

    // obj.setX(20); // Error: Cannot call a non-const member function on a const object

    return 0;
}

Why use const Objects?

Using const objects has several benefits:

  • It improves code safety by ensuring that an object's state remains constant after creation, preventing accidental modification.
  • It enforces a clear separation between modifying and non-modifying operations, improving code readability and maintainability.
  • It provides a form of self-documentation, indicating that the object's state should not change.

Complete Example

Here's a complete example demonstrating the use of const objects in C++:

#include <iostream>

class MyClass {
public:
    int x;

    MyClass(int a) : x(a) {}

    int getX() const {
        return x;
    }

    void setX(int a) {
        x = a;
    }
};

int main() {
    const MyClass constObj(10); // Create a const object
    MyClass nonConstObj(20);    // Create a non-const object

    std::cout << "constObj.x: " << constObj.getX() << std::endl;     // Access data member using a const member function
    std::cout << "nonConstObj.x: " << nonConstObj.getX() << std::endl;

    // constObj.setX(30); // Error: Cannot call a non-const member function on a const object
    nonConstObj.setX(40); // Allowed: Calling a non-const member function on a non-const object

    return 0;
}
  1. Creating and using const objects in C++:

    • Description: Introduces the concept of const objects and demonstrates their creation and usage in C++.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          int data;
      
          MyClass(int value) : data(value) {}
      };
      
      int main() {
          const MyClass obj(42); // Creating a const object
          std::cout << obj.data << std::endl; // Accessing data in a const object
      
          // obj.data = 10; // Error: Cannot modify data in a const object
      
          return 0;
      }
      
  2. Const member functions and const objects in C++:

    • Description: Introduces const member functions and demonstrates their use with const objects, ensuring the non-modifiability of the object.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          int data;
      
          MyClass(int value) : data(value) {}
      
          void display() const {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          const MyClass obj(42);
          obj.display(); // Calling a const member function on a const object
      
          // obj.data = 10; // Error: Cannot modify data in a const object
      
          return 0;
      }
      
  3. Const reference to objects in C++:

    • Description: Introduces const references to objects, allowing access to objects without the ability to modify them.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          int data;
      
          MyClass(int value) : data(value) {}
      };
      
      int main() {
          const MyClass obj(42);
          const MyClass& refObj = obj; // Const reference to a const object
      
          std::cout << refObj.data << std::endl;
      
          // refObj.data = 10; // Error: Cannot modify data through a const reference
      
          return 0;
      }
      
  4. C++ const object vs const pointer:

    • Description: Compares const objects and const pointers, highlighting their differences and use cases.
    • Example Code:
      #include <iostream>
      
      class MyClass {
      public:
          int data;
      
          MyClass(int value) : data(value) {}
      };
      
      int main() {
          const MyClass obj(42);
          const MyClass* constPtr = &obj; // Const pointer to a const object
      
          std::cout << constPtr->data << std::endl;
      
          // constPtr->data = 10; // Error: Cannot modify data through a const pointer
      
          return 0;
      }