Friday, February 26, 2010

C++ Discussions..

Below are the updates on our C++ discussions
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((static_cast(*this).func()));
}
const int& func()const{
return i;
}
};
int main()
{
A a1(2);
const A a2(5);
cout<cout<}

O/P ---> 9 9


---------------------------Will be update soon ----------------------------
Till then please help me in answering theses questions

Sunday, February 21, 2010

C++ Brain teasers !

-> What is IS-A relationship?
class Derived : public Base {};
class Derived : private Base {};
-> Are both of these inheritance IS-A ??
-> When is V-table formed
@Compile time ?? OR
@Run time ??
-> What is the rule of Function overloading??
like... how should two overloaded functions differ from each other.
-> Can we have a 2 overloaded funcitons which differ only in constnesss??
ex: void disco(char * sam) {}
void disco(char * sam) const {}
-Will these 2 functions be considered as overloaded or will it throw any error???
-> Can we have virtual functions as inline?
If NO then Y
If YES then HOW??
-> Whether we will achieve INLINING of these virtual functions
-> Can I redefine, a virtual function which is public in base class, in private of the derived class.
If NO then Y
If YES then HOW??
-> Will that private redefined function of derived class be accessible through base class pointer ??
-> Can we have a pure virtual destructor??

........LOADING