Pages

Tuesday, 14 April 2015

Advanced c++ interview questions and answers 3


1. How to define an postfix increment operator function. How to call it explicitly?

SHOW/HIDE ANSWER
Declaration,
    1 Integer operator++(int)
    2 {
    3  Integer v = *this;
    4  ++mV;
    5  return v;
    6 }
Calling it explicitly,
    1 Integer v1(2);
    2 Integer v2 = v1.operator++(0); 
 
 

2. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 public:
    6     void func()
    7     {
    8         std::cout << "Base::func" << std::endl;
    9     }
   10 };
   11 
   12 class Derived: public Base
   13 {
   14 public:
   15     void func(int i)
   16     {
   17         std::cout << "Derived::func" << std::endl;
   18     }
   19 };
   20 
   21 int main(int argc, char **argv)
   22 {
   23     Derived derived;
   24     derived.func();
   25     return 0;
   26 } 
SHOW/HIDE ANSWER
Compilation error.

3. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5     int i;
    6 public:
    7     Base(int v)
    8      : i(v)
    9     {}
   10 
   11     virtual void print()
   12     {
   13         std::cout << i << std::endl;
   14     }
   15 };
   16 
   17 class Derived: public Base
   18 {
   19     int j;
   20 
   21 public:
   22     Derived(int v)
   23      : Base(v),
   24        j(v)
   25     {}
   26 
   27     void print()
   28     {
   29         Base::print();
   30         std::cout << j << std::endl;
   31     }
   32 };
   33 
   34 int main(int argc, char **argv)
   35 {
   36     Derived d1(1);
   37     Derived d2(2);
   38 
   39     Base &b1 = d2;
   40 
   41     b1 = d1;
   42 
   43     d1.print();
   44     d2.print();
   45 
   46     return 0;
   47 } 

4. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5     int i;
    6 public:
    7     Base(int v)
    8     {}
    9 
   10     virtual void print()
   11     {
   12         std::cout << i << std::endl;
   13     }
   14 };
   15 
   16 class Derived: public Base
   17 {
   18     int j;
   19 
   20 public:
   21     Derived(int v)
   22      : Base(v),
   23        j(v)
   24     {}
   25 
   26     void print()
   27     {
   28         Base::print();
   29         std::cout << j << std::endl;
   30     }
   31 };
   32 
   33 void func(Base b)
   34 {
   35     if (dynamic_cast<Derived *>(&b) == 0) {
   36         std::cout << "It is not an instance of Derived";
   37     } else {
   38         std::cout << "It is an instance of Derived";
   39     }
   40 }
   41 
   42 int main(int argc, char **argv)
   43 {
   44     Derived d(1);
   45 
   46     func(d);
   47 
   48     return 0;
   49 } 

SHOW/HIDE ANSWER
It is not an instance of Derived

Slicing removed the Derived class part from the object.

5. Where is the location of vptr in an object?

SHOW/HIDE ANSWER
At the start of the object. In case of multiple inheritance there will be multiple vptr so other vptr will be with an offset.


6. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class A
    4 {
    5     int i;
    6 public:
    7     virtual ~A()
    8     {}
    9 
   10 };
   11 
   12 class B
   13 {
   14     int j;
   15 public:
   16     virtual ~B()
   17     {}
   18 };
   19 
   20 class C: public A, public B
   21 {
   22     int k;
   23 };
   24 
   25 int main(int argc, char **argv)
   26 {
   27     C obj;
   28 
   29     std::cout << "A:" << (A *)&obj << "\nB:" << (B *)&obj << "\nC:" << (C *)&obj << std::endl;
   30     return 0;
   31 }
SHOW/HIDE ANSWER

Sample output for a 32 bit build:
A:0xffc338dc
B:0xffc338e4
C:0xffc338dc

7. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class A
    4 {
    5 public:
    6     virtual ~A()
    7     {}
    8 };
    9 
   10 class B: protected A
   11 {
   12 public:
   13 };
   14 
   15 int main(int argc, char **argv)
   16 {
   17     B b;
   18     A *a = &b;
   19     return 0;
   20 }
SHOW/HIDE ANSWER
Compilation error. b can be converted in to a within the scope of B only in this case. So the following program will work fine.

    1 #include <iostream>
    2 
    3 class A
    4 {
    5 public:
    6     virtual ~A()
    7     {}
    8 
    9 };
   10 
   11 class B: protected A
   12 {
   13 public:
   14     static A * toA(B * b);
   15 };
   16 
   17 A * B::toA(B * b)
   18 {
   19     return b;
   20 }
   21 
   22 int main(int argc, char **argv)
   23 {
   24     B b;
   25 
   26     A *a = B::toA(&b);
   27 
   28     return 0;
   29 } 
 

8. Is there a way to know if operator[] is used for reading(rhs) or writing(lhs)?

SHOW/HIDE ANSWER
    1 #include <iostream>
    2 #include <algorithm>
    3 
    4 class Test
    5 {
    6     int mData[16];
    7 public:
    8     class Proxy
    9     {
   10         int &mCell;
   11     public:
   12         Proxy(int &cell)
   13          : mCell(cell)
   14         {}
   15 
   16         Proxy& operator=(int i)
   17         {
   18             std::string pass;
   19             std::cout << "password for writting :) ";
   20             std::cin >> pass;
   21             mCell = i;
   22         }
   23 
   24         operator int() const
   25         {
   26             return mCell;
   27         }
   28     };
   29 
   30     class ConstProxy: public Proxy
   31     {
   32     public:
   33         ConstProxy(const int &cell)
   34          : Proxy(const_cast<int &>(cell))
   35         {}
   36     };
   37 
   38 public:
   39     Test()
   40     {
   41         std::fill(mData, mData+16, 0);
   42     }
   43 
   44     ConstProxy operator[](int index) const
   45     {
   46         return ConstProxy(mData[index]);
   47     }
   48 
   49     Proxy operator[](int index)
   50     {
   51         return Proxy(mData[index]);
   52     }
   53 
   54     friend std::ostream& operator<<(std::ostream& os, const Test& t);
   55 };
   56 
   57 std::ostream& operator<<(std::ostream& os, const Test& t)
   58 {
   59     os << "[";
   60     for (int i=0; i<16; ++i) {
   61         os << t[i] << (i<15?", ":"");
   62     }
   63     os << "]";
   64     return os;
   65 }
   66 
   67 int main(int argc, char **argv)
   68 {
   69     Test t;
   70     t[0] = 1;
   71     std::cout << t << std::endl;
   72 }
   73 

9. How to create a non-derivable class?

SHOW/HIDE ANSWER
    1 class Sealed;
    2 
    3 class Lock
    4 {
    5 private:
    6     Lock() {}
    7     Lock(const Lock&) {}
    8     friend class Sealed;
    9 };
   10 
   11 class Sealed: public virtual Lock
   12 {
   13 public:
   14     Sealed() {}
   15 };
 
 
PREVIOUS HOME

No comments:

Post a Comment