Find the output of the following program
class complex{
double re;
double im;
public:
complex() :
re(1),im(0.5) {}
bool
operator==(complex &rhs);
operator
int(){}
};
bool complex::operator == (complex &rhs){
if((this->re
== rhs.re) && (this->im == rhs.im))
return true;
else
return false;
}
int main(){
complex
c1;
cout<<
c1;
}
Answer : Garbage value
Explanation:
The
programmer wishes to print the complex object using output
re-direction operator, which he has not defined for his lass. But the compiler
instead of giving an error sees the conversion function
and converts the user defined object to standard object and prints
some garbage value.
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();
}
Answer:
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.
Find the output of the following program
void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
cout <<"a="<}
}
Answer :
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
What is a modifier?
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’.
What is an accessor?
An accessor is a class operation that does not modify the state of an object.
The accessor functions need to be declared as const operations
Differentiate between a template class and
class template.
Template class:
A generic definition or a parameterized class not instantiated until the client
provides the needed information. It’s jargon for plain templates.
Class template:
A class template specifies how individual classes can be constructed much like
the way a class specifies how individual objects can be constructed. It’s jargon
for plain classes.
When does a name clash occur?
A name clash occurs when a name is defined in more than one place. For example.,
two different class libraries could give two different classes the same name. If
you try to use many class libraries at the same time, there is a fair chance
that you will be unable to compile or link the program because of name clashes
Page Numbers :
1
2 3
4
5
6
7
8
9