CPP Program To implement Classes with a Constant Data Member

Aim:

To implement classes with constant data members.

Algorithm:

  1. Create the class called test
  2. Declare the integer const variable called I
  3. Initialization has occurred during the constructor
  4. It is a const data member in every object its independent copy is present.
  5. Initialize using constructor and the value of i cannot be changed
  6. Display the result.

Program:

#include<iostream>
using namespace std;
class data
{
private:
const int a;
public:
data():a(5){}
void print()
{
cout<<"CONSTANT VALUE :"<<a;
}
};
int main()
{
data A;
A.print();
return 0;
}

Execution:

INPUT : 
5

OUTPUT : 
CONSTANT VALUE: 5

Result:

Thus, the Implementation Classes with a Constant Data Member was executed successfully.

Leave a Comment