In: Computer Science
Consider this class with a const variable and function:
class Student {
private:
const int IDNum;
public:
Student();
int getIDNum() const:
};
a. How do you initialize the const variable IDNum to 100? Write the
code:
b. Are getters normally declared const?
c. Are setters normally declared const?
d. Can a const function call a non-const function?
e. Can a non-const function call a const function?
f. Can a const function use a non-const variable?
g. Can a non-const function use a const variable?
a. How do you initialize the const variable IDNum to
100? Write the code:
We have to use initializer list as follows to initialize IDNum to
100 in the constructor
Student::Student():IDNum(100){
}
b. Are getters normally declared const?
Yes, getters are normally declared as const, since we simple return
the values of member variables without manipulating them or
updating them
c. Are setters normally declared const?
No, we cannot declare setters as const, because in setters we
modify/change
the value of member variables and doing so will give a compilation
error
d. Can a const function call a non-const
function?
No, const function can only call const functions
e. Can a non-const function call a const
function?
Yes, non-const function can call const function
f. Can a const function use a non-const
variable?
Yes, we can use non-const variables
g. Can a non-const function use a const
variable?
Yes, non-const function can use const variable