Wednesday, February 11, 2009

Interview FAQs : C++ (Answers End of Post)

1) Which is the parameter that is added to every non-static member function when it is called?
---------------------------------------------------------------------------------------
2)
class base
{
public:
int bval;
base(){ bval=0;}
};
class deri:public base
{
public:
int dval;
deri(){ dval=1;}
};
void SomeFunc(base *arr,int size)
{
for(int i=0; i <>bval;
cout << endl; } int main() { base BaseArr[5]; SomeFunc(BaseArr,5); deri DeriArr[5]; SomeFunc(DeriArr,5); }
---------------------------------------------------------------------------------------
3)
class base { public: void baseFun(){ cout << "from base" <<>baseFun();
}
int main()
{
base baseObject;
SomeFunc(&baseObject);
Deri deriObject;
SomeFunc(&deriObject);
}
---------------------------------------------------------------------------------------
4)
class base
{
public:
virtual void baseFun(){ cout << "from base" <<>baseFun();
}
int main()
{
base baseObject;
SomeFunc(&baseObject);
Deri deriObject;
SomeFunc(&deriObject);
}
---------------------------------------------------------------------------------------
5)
class some
{
public:
~some()
{
cout << "some's destructor" << endl; } }; void main() { some s; s.~some(); } ---------------------------------------------------------------------------------------
6) #include class fig2d { int dim1; int dim2; public: fig2d() { dim1=5; dim2=6;} virtual void operator<<(ostream & rhs); }; void fig2d::operator<<(ostream &rhs) { rhs <<>dim1 <<" "<<<" "; } /*class fig3d : public fig2d { int dim3; public: fig3d() { dim3=7;} virtual void operator << (ostream &rhs); }; void fig3d::operator << (ostream &rhs) { fig2d::operator <<(rhs); rhs <<>dim3;
}
*/

void main()
{
fig2d obj1;
// fig3d obj2;
obj1 << cout; // obj2 << cout; }
---------------------------------------------------------------------------------------
7) 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; }
---------------------------------------------------------------------------------------
8) 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;
}
---------------------------------------------------------------------------------------
9)
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 << re; cout << im;}
};
void main()
{
complex c3;
double i=5;
c3 = i;
c3.print();
}
---------------------------------------------------------------------------------------
C++ Answer
---------------------------------------------------------------------------------------
1. ‘this’ pointer

00000

2. 01010

3. from base
from base

4. from base
from Derived

5. some's destructor
some's destructor

6. 5 6

7. Runtime Error: Stack Overflow

8. Garbage value

9. 5,5

0 comments:

Post a Comment

© All Content is Under Copyright Protection © 2008-2009 VC++ Interview Tips | Site Best Viewed in Firefox