Which constructor will initialize the base class data member?
Which operator is used to declare the destructor?
What is the output of this program?
1. #include2. using namespace std; 3. class X 4. { 5. int m; 6. public: 7. X() : m(10) 8. { 9. } 10. X(int mm): m(mm) 11. { 12. } 13. int getm() 14. { 15. return m; 16. } 17. }; 18. class Y : public X 19. { 20. int n; 21. public: 22. Y(int nn) : n(nn) {} 23. int getn() { return n; } 24. }; 25. int main() 26. { 27. Y yobj( 100 ); 28. cout << yobj.getm() << " " << yobj.getn() << endl; 29. }
What is the output of this program?
1. #include2. using namespace std; 3. class Parent 4. { 5. public: 6. Parent (void) 7. { 8. cout << "Parent()\n"; 9. } 10. Parent (int i) 11. { 12. cout << "Parent("<< i << ")\n"; 13. }; 14. Parent (void) 15. { 16. cout << "~Parent()\n"; 17. }; 18. }; 19. class Child1 : public Parent { }; 20. class Child2 : public Parent 21. { 22. public: 23. Child2 (void) 24. { 25. cout << "Child2()\n"; 26. } 27. Child2 (int i) : Parent (i) 28. { 29. cout << "Child2(" << i << ")\n"; 30. } 31. ~Child2 (void) 32. { 33. cout << "~Child2()\n"; 34. } 35. }; 36. int main (void) 37. { 38. Child1 a; 39. Child2 b; 40. Child2 c(42); 41. return 0; 42. }
a) Parent()
Parent()
Child2()
Parent(42)
Child2(42)
~Child2()
~Parent()
~Child2()
~Parent()
~Parent()
b) error
c) runtime error
d) None of the mentioned
What is the output of this program?
1. #include2. using namespace std; 3. class Base 4. { 5. public: 6. int m; 7. Base(int n=0) 8. : m(n) 9. { 10. cout << "Base" << endl; 11. } 12. }; 13. class Derived: public Base 14. { 15. public: 16. double d; 17. Derived(double de = 0.0) 18. : d(de) 19. { 20. cout << "Derived" << endl; 21. } 22. }; 23. int main() 24. { 25. cout << "Instantiating Base" << endl; 26. Base cBase; 27. cout << "Instantiating Derived" << endl; 28. Derived cDerived; 29. return 0; 30. }
a) Instantiating Base
Base
Instantiating Derived
Base
Derived
b) Instantiating Base
Instantiating Derived
Base
Derived
c) Instantiating Base
Base
Instantiating Derived
Base
d) None of the mentioned