1. What is the purpose of a constructor??
2.When is Copy constructor used and when is assignment operator used??
e.g A obj1,obj2;
A obj3(obj1); //Which is invoked Copy constructor or Assignment ??
obj2 = obj3; // "" ""
3. Suppose a copy Constructor of class yaar
yaar (const yaar& rhs)
{
i = rhs.i; //assuming i as int data member.
}
DOLLAR Question -> Why do we pass the argument as Reference ??
Why cant we pass it by value??
4. And an assignment operator
yaar & operator= (const & yaar rhs)
{
i = rhs.i;
return *this;
}
Q -> Why is there a need to return *this . Any repercussions if not returned???
Okee......
5. -Now How to print the address of a vptr through an object ??
e.g bruce bobj; //using object bobj print address of vptr
-Also how to print the address of a virtual table of class bruce??
-Can we print the value of 'this' pointer ????
6. Consider below classes.
class DON
{
int xx;
public:
virtual int talash() { return 11;}
virtual void namumkin () = 0; //Pure virtual
}
class chota_DON : public DON
{
int yy;
public:
int talash() { return 0;}
void namumkin() {} //Pure virtual redefined by chota
}
DON bigb;
chota_DON smallb;
Q -> What are the sizes of objects 'bigb' and 'smallb' ???
-> How many vtables will be generated for theses classes and WHEN (@compile time/run)??
-> Y we cant instantiate an object of an abstract class ? What actually happens inside which stops us to create the object??
7. Consider ---
class sam
{
public:
static int a;
static int b;
static int c;
static int d;
};
sam sobj1;
Q-> What is the size of this sobj1 ?? (Let us assume 4 B of int)
8. What is the size of an empty class ? Y ?? reason it ?
9. Name all the functions C++ silently writes into the class if thoes functions are not defined.
10. Consider this..
class 3idiots {
int w;
public :
void dummy(); //Giving declarations but no definition.
};
Q -> Will the above code compile ??
Q -> If yes then when will we get the error ?
-----------------------------------------------------------------------------
Const related Qs
Q HOw can we avoid code duplication in two functions of which differ in constness?
class A
{
public:
const int a;
int i;
//A(int){i=9;}
A(int h):a(h),i(9){}
int func(){
return const_cast
}
const int& func()const{
return i;
}
};
int main()
{
A a1(2);
const A a2(5);
cout<
O/P ---> 9 9
---------------------------Will be update soon ----------------------------
Till then please help me in answering theses questions