1. What is the difference between calling just throw and throw with an object in a catch block?
SHOW/HIDE ANSWER
A copy of the object is created if we throw with an object. With just throw, no copy is created.
2. What is a possible replacement for c static function in c++?
3. What is the size of an empty class, or class with only normal functions?
SHOW/HIDE ANSWER
Not zero, 1 for most compilers. The reason for this is to have different address for different object.
4. What is the size of class with only virtual functions?
5. Which class name the following program will print?
1 #include <typeinfo> 2 #include <iostream> 3 4 class B 5 { 6 }; 7 8 class D: public B 9 { 10 }; 11 12 int main(int argc, char **argv) 13 { 14 B *t = new D(); 15 std::cout << typeid(*t).name() << std::endl; 16 }
SHOW/HIDE ANSWER
B
For RTTI to work correctly we need atleast one virtual function in the base class.
For RTTI to work correctly we need atleast one virtual function in the base class.
6. What is the output of the following program?
1 #include <iostream> 2 3 void print(double v) 4 { 5 std::cout << v << std::endl; 6 } 7 8 void print(long v) 9 { 10 std::cout << v << std::endl; 11 } 12 13 int main(int argc, char **argv) 14 { 15 print(1); 16 }
7. What is the output of the following program?
1 #include <iostream> 2 3 void print(double v) 4 { 5 void print(long v); 6 print(v); 7 } 8 9 void print(long v) 10 { 11 std::cout << v << std::endl; 12 } 13 14 int main(int argc, char **argv) 15 { 16 print(1.0); 17 }
8. What is the output of the following program?
1 #include <iostream> 2 3 class Base 4 { 5 public: 6 void print(int v = 1) 7 { 8 std::cout << "Base : " << v << std::endl; 9 } 10 }; 11 12 class Derived: public Base 13 { 14 public: 15 void print(int v = 10) 16 { 17 std::cout << "Derived : " << v << std::endl; 18 } 19 }; 20 21 22 int main(int argc, char **argv) 23 { 24 Derived o1; 25 Base * o2 = &o1; 26 27 o1.print(); 28 o2->print(); 29 30 return 0; 31 }
9. How to declare a namespace alias?
10. Which is the macro that can be used to identify that we are using a c++ compiler?
11. How to declare c function in c++?
12. What is the difference between exit and abort?
SHOW/HIDE ANSWER
exit does a graceful process termination, it calls the destructors for all the constructed objects, with abort they are not called.
With exit the local With variables of the calling function and its callers will not have their destructors invoked.
With exit the local With variables of the calling function and its callers will not have their destructors invoked.
13. Can I have static members in an union?
14. Which are the operators that cannot be overloaded?
15. Suppose we have an Integer class as shown below, how do I support 2+ obj?
1 #include <iostream> 2 3 class Integer 4 { 5 int mV; 6 7 public: 8 explicit Integer(int v) 9 : mV(v) 10 {} 11 12 Integer operator+(int v) const 13 { 14 return Integer(mV+v); 15 } 16 17 int value() const 18 { 19 return mV; 20 } 21 }; 22 23 int main(int argc, char **argv) 24 { 25 Integer v1(2); 26 27 std::cout << (v1+2).value() << std::endl; 28 std::cout << (2+v1).value() << std::endl; 29 }
SHOW/HIDE ANSWER
Add a global operator+ function which takes int as first argument and Integer as the second argument.
1 Integer operator+(int v, Integer iv) 2 { 3 return iv+v; 4 }
16. Can I overload destructor?
17. Can I call destructor explicitly?
18. What is the output of the following program?
1 #include <iostream> 2 3 class Shape 4 { 5 public: 6 virtual ~Shape() 7 {}; 8 virtual void draw() = 0; 9 }; 10 11 class Circle: public Shape 12 { 13 public: 14 virtual void draw() 15 { 16 std::cout << "circle drawn" << std::endl; 17 } 18 }; 19 20 int main(int argc, char **argv) 21 { 22 Circle c; 23 Shape *sp = &c; 24 25 Circle *cp = &c; 26 Shape **spp = &cp; 27 28 (*spp)->draw(); 29 30 return 0; 31 }
19. Where virtual inheritance should be used in a hierarchy?
SHOW/HIDE ANSWER
If we have a diamond class hierarchy we should use the virtual inheritance just below the top of the diamond
20. What is the output of the following program?
1 2 #include <iostream> 3 4 class Base 5 { 6 public: 7 virtual ~Base() 8 {} 9 10 virtual void func1() = 0; 11 virtual void func2() = 0; 12 }; 13 14 class DerivedA: public virtual Base 15 { 16 public: 17 void func1() 18 { 19 func2(); 20 } 21 }; 22 23 class DerivedB: public virtual Base 24 { 25 public: 26 void func2() 27 { 28 std::cout << "DerivedB::func2()" << std::endl; 29 } 30 }; 31 32 class Join: public DerivedA, public DerivedB 33 {}; 34 35 int main(int argc, char **argv) 36 { 37 Join * j = new Join(); 38 39 DerivedA * da = j; 40 DerivedB * db = j; 41 42 da->func1(); 43 db->func1(); 44 45 delete j; 46 47 return 0; 48 }
SHOW/HIDE ANSWER
DerivedB::func2()
DerivedB::func2()
It is sometimes called "cross delegation". DerivedA ended up calling function of its sibling class DerivedB.
DerivedB::func2()
It is sometimes called "cross delegation". DerivedA ended up calling function of its sibling class DerivedB.
PREVIOUS | HOME | NEXT(Advanced c++ interview questions and answers 3) |
No comments:
Post a Comment