Constant concepts (C++) with Examples

Constant concepts C++:-

  • Constant is a data type qualifier which provide extra quality to our normal variable and that quality is :- After initializing a constant vairable with appropriate value which we can not change that initialized value.
  • In C and C++, rules about constant variable is that we have to initialize that constant variable at the time of declaration of that variable.
  • We can not initialize the variable after declaring it.

 

const int I;

i=10; // Error

 

  • In this case, if the variable I is the local variable then at the time of declaration that I is initialized with the garbage value and that garbage value is considered as a constant value.
  • And if I is the global variable then value of th variable I is initialize with 0 and that  0 is considered as a constant value.
  • Because of the above 2 reasons we have to write a syntax in such a way that,

 

const int i=10; // Allowed

 

In C++, we can apply a constant term for the following things,

1)    Constant data members of the class.

2)    Constant member function of the class.

3)    Constant object of the class.

4)    Constant parameter the class member functions.

 

 

Constant data members of the class:-

 

class demo

{

public:

int i,j;

const int k;

demo():k(10) // This is the initialization list to initialize the constant variables

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

};

 

int main()

{

demo d;

//

// Error because we are changing the constant member

//

// d.k++;

return 0;

}

 

  • Using this manner we can initialize the constant variable as well as a non constant variables.
  • While initializing we can provide a user defined value or some constant default value.

 

//

 

Constant object of the class:-

  • We can create a constant object of the class by writing const keyword while creating the object.
  • If the object is the constant object means all the members of that object are constant.
  • If the object is the constant object then that object can call constant member functions only.
  • Mens it can not call the normal member functions.

 

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

};

 

int main()

{

const demo d; // constant object

//

// Error because we are changing the member of constant object

// d.i++;

return 0;

}

 

//

 

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

int fun()

{

++i;

}

};

int main()

{

const demo c;

demo d;

//

// Constant object can not call non constant methods

//

c.fun(); // Error

d.fun();

return 0;

}

 

//

 

Member function of the class is constant:-

  • If  the member function of the class is constant and that member function is non static function the n that function must be called by same object.
  • If the member function is constant then we can not modify the values of the caller object.
  • To make a member function as a constant we have to write a ‘const’ keyword after the function prototype.

 

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

//

// constant method

//

int fun() const

{

cout<<“in the constant function”;

return 0;

}

//

// Non constant method

//

int gun()

{

cout<<“in the non constant function”;

return 0;

}

};

int main()

{

//

// constant object

//

const demo c;

demo d;

//

// Constant object can call the constant methods

//

c.fun();

//

// Non constant object can call the constant method

//

d.fun();

//

// Non constant object can call the non constant method

//

d.gun();

//

// constant object can not call the non constant method

//

c.gun();

return 0;

}

//

 

Constants in C

 

Constant parameter to a member function:-

  • If a parameter to a member function is constant then we can not change that parameter but only we can change a caller object’s contents.

 

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

//

// constant method

//

int fun(int k) const

{

cout<<“in the constant function”;

k++; // Allowed

i++ // Error because it is constant method

return 0;

}

};

int main()

{

//

// constant object

//

const demo c;

//

// Constant object can call constant methods

//

c.fun();

return 0;

}

//

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

//

// constant method

//

int fun(const int k) const

{

cout<<“in the constant function”;

k++; // Error because it is a constant parameter

i++; // Error because it is a constant method

return 0;

}

};

int main()

{

//

// constant object

//

const demo c;

//

// Constant object can call constant methods

//

c.fun(10);

return 0;

}

//

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

//

// constant method

//

int fun(demo *obj) const

{

cout<<“in the constant function”;

(obj->i)++; // Allowed because it is function parameter

i++; // Error because it is constant method

return 0;

}

};

int main()

{

//

// constant object

//

const demo c;

demo d;

//

// Constant object can call constant methods

//

c.fun(&d);

return 0;

}

//

class demo

{

public:

int i,j;

demo()

{

i=20;

j=20;

}

~demo()

{

cout<<“In the destructor\n”;

}

//

// constant method

//

int fun(const demo *obj) const

{

cout<<“in the constant function”;

(obj->i)++; // Error because parameter is also constant

i++; // Error because it is constant method

return 0;

}

};

int main()

{

//

// constant object

//

const demo c;

demo d;

//

// Constant object can call constant methods

//

c.fun(&c);

c.fun(&d); // Allowed to pass non constant object

return 0;

}

//

class demo

{

public:

int i,j;

//

// It is not allowed to make the constructor

// and destructor constant

//

demo() const // Error

{

i=20;

j=20;

}

~demo() const // Error

{

cout<<“In the destructor\n”;

}

};

int main()

{

//

// constant object

//

demo d;

return 0;

}

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply