[amp_mcq option1=”must be assigned values in any derived class” option2=”must never be initialized in a base class” option3=”must be initialized, rather than assigned values” option4=”must not exist if a class is to be a base class” correct=”option3″]
The correct answer is: C. must be initialized, rather than assigned values.
A reference variable is a variable that stores the address of another variable. A const class member is a member of a class that cannot be modified after it has been created.
When a reference variable is initialized, the address of the variable that it is referencing is stored in the reference variable. When a const class member is initialized, the value of the member is stored in the member.
In C++, reference variables and const class members must be initialized, rather than assigned values. This is because reference variables and const class members cannot be reassigned. If a reference variable or const class member is assigned a value, the compiler will generate an error.
Here is an example of how to initialize a reference variable:
c++
int x = 10;
int& y = x; // y is a reference to x
In this example, the reference variable y is initialized to the address of the variable x. The value of x cannot be changed, but the value of y can be changed. For example, the following code will change the value of y, but it will not change the value of x:
c++
y = 20;
Here is an example of how to initialize a const class member:
c++
class C {
public:
const int x = 10;
};
In this example, the const class member x is initialized to the value 10. The value of x cannot be changed, even if the object of type C is modified. For example, the following code will not compile:
c++
C c;
c.x = 20; // error: cannot modify const member `x`