All about const in C++

Introduction

This article talks about all different usage of const qualifier in C++.

Background

Declaration:

Named constant or const variables

const int PI = 3.14

Declares a named constant called PI, which can be used later in the program where ever we want to use the value 3.14.

This is similar to #define macro, but using const is better as it is understood and used by the compiler itself and not just simple substitution in the program which is done by preprocessor on using #define.

const with pointers:

Here one has to be careful in using const to know whether it is used to determine the

Pointer as constant or

The Data what it points is constant or

Both i.e. both Data and Pointer are constant.

To declare a pointer that is pointing to a constant interger

const int *ptrToConstData1;
int const *ptrToConstData2;

Here, we cannot change the data it is pointing to.
To declare a const pointer that is pointing to an integer

int * const ptrIsConst;

This has to be declared and initialised and cannot be changed later to point to some other variable.

To declare a const pointer pointing to constant integer

const int * const ptrAndDataCnst;

Here both pointer and data are constant. As it is also a constant pointer, it has to be initialised while declaring itself.

Any const data members of a class has to be initialized and has to be done in the initilizer list of the class's constructor. Only static const data members can be intialized inside the class declaration.

//we have to initialise it in initializer list as below Abc(int j =0):myInt_m(j){ cout<<"Value of myInt_m is : "<<myInt_m<<endl; cout<<"Value of myDouble_m is : "<<myDouble_m<<endl;}};

int main(){ Abc abc(10);return(0);}

output:----------Value of myInt_m is : 10Value of myDouble_m is : 100.53

Return by const value:

Return by const is meaningful in case of returning:-

- const strings and arrays from functions as they are implemented as pointers. This is to avoid the program altering their lValue and getting crash.

- If a function is returning any user-defined data types like class objects. Where as It does not make sense returning const to a built-in types, as compiler anyway takes care of it.

In above cases by making that function returning a const will prevent using that returned value as an lValue in program as it will be a compier error. Otherwise, it will allow using that returned value as lValue.

int main(){//Trying to modify will lead to segmentation fault//Func1()[1] = 'K';

//Trying to modify will give a compiler error now//Func2()[1] = 'K';

//Compiler will anyway prevent using it as an lValue here as it is built-in data type//func3() = 3;

//Compiler will allow using func4 as lValue func4() = Abc(5);

//Compiler will prevent using func5 as lValue now//func5() = Abc(10);

return(0);}

output:---------Value of myInt is :0Value of myInt is :5

const variable as an argument:

To make sure, preventing the data that is passed as argument, whether it could be a reference or pointer variable (which was sent to avoid sending by value as copying in case of big user defined data is costly), getting aletred inside the function or callee, then we can send that data by const qualifier.

output:----------Case1: where Integer variable is not explicitlly associated with constBefore the value of a :10sqrtVal :100After the value of a :100

Case2: where Integer variable is explicitly associated with constBefore the value of a :100sqrtVal :100After the value of a :10

As shown in the above example, case-1 we can cast away the constness of a variable or object, that is not explicitly declared with const qualifier. Where as in case-2, if we try to cast away constness of a variable or object that has been explictly declared as const, results in undefined bahaviour.

Also, below is another example showing the usage of const_cast in a class's const member function.

Guys i need your help T_T my professor gave me a problem and i don't know how to do it, pls help me

"Creat a program that will compute for the grade of students and its equivalent grade point.. the formula for computing grade is GRADE=10%, ASSIGNMENT=20%,Seatwork=30%,Quiz=40%
Note that there are 2 assignment,quiz,and seatwork given