7. What is a modifier?
Ans:
A modifier, also called a modifying function is a member function that changes the value of at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as ‘mutators’.
|
Ex 1: Find the output of the following program
void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
cout <<"a="<<
}
Ans :
Compiler Error: 'ra',reference must be initialized
Explanation :
Pointers are different from references. One of the main
differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error |
Ex 2: Find the output of the following program
class complex{
double re;
double im;
public:
complex() : re(0),im(0) {}
complex(double n) { re=n,im=n;};
complex(int m,int n) { re=m,im=n;}
void print() { cout<
};
void main(){
complex c3;
double i=5;
c3 = i;
c3.print();
}
Ans:
5,5
Explanation:
Though no operator= function taking complex, double is defined, the double on the rhs is converted into a temporary object using the single argument constructor taking double and assigned to the lvalue. |