© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

12. What is an adaptor class or Wrapper class?

Ans:

A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non- object- oriented implementation.

Ex 1: Find the output of the following program
 
class opOverload{
public:
            bool operator==(opOverload temp);
};
 
bool opOverload::operator==(opOverload temp){
            if(*this  == temp ){
                        cout<<"The both are same objects\n";
                        return true;
            }
            else{
                        cout<<"The both are different\n";
                        return false;
            }
}
 
void main(){
            opOverload a1, a2;
            a1= =a2;
}
 
Ans :
            Runtime Error: Stack Overflow


Explanation :

Just like normal functions, operator functions can be called recursively. This program just illustrates that point, by calling the operator == function recursively, leading to an infinite loop.

Ex 2: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;
}
 
Ans : 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.

SLogix Student Projects
bottom