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
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:
const
object.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.
Using const in C++ class members:
const
for class members, making them read-only and preventing modification after initialization.class MyClass { public: const int constantValue; MyClass(int value) : constantValue(value) {} };
Const member functions in C++ classes:
class MyClass { public: void nonConstFunction() { // Modifying object state } void constFunction() const { // Not modifying object state } };
Mutable keyword in C++ and const members:
mutable
keyword, allowing modification of const
members within a const member function.class MyClass { public: mutable int mutableValue; void constFunction() const { // Can modify mutableValue here } };
C++ const member variable initialization:
class MyClass { public: const int constantValue; MyClass(int value) : constantValue(value) {} };