Predict the output of below C++ programs.
Question 1
// Assume that integers take 4 bytes. #include<iostream> using namespace std; class Test { static int i; int j; }; int Test::i; int main() { cout << sizeof(Test); return 0; } |
chevron_right
filter_none
Output: 4 (size of integer)
static data members do not contribute in size of an object. So ‘i’ is not considered in size of Test. Also, all functions (static and non-static both) do not contribute in size.
Question 2
#include<iostream> using namespace std; class Base1 { public: Base1() { cout << " Base1's constructor called" << endl; } }; class Base2 { public: Base2() { cout << "Base2's constructor called" << endl; } }; class Derived: public Base1, public Base2 { public: Derived() { cout << "Derived's constructor called" << endl; } }; int main() { Derived d; return 0; } |
chevron_right
filter_none
Output:
Base1’s constructor called
Base2’s constructor called
Derived’s constructor called
In case of Multiple Inheritance, constructors of base classes are always called in derivation order from left to right and Destructors are called in reverse order.
Recommended Posts:
- Output of C Program | Set 17
- Output of C++ Program | Set 2
- Output of C++ Program | Set 3
- Output of C Program | Set 18
- Output of C Program | Set 19
- Output of C Program | Set 20
- Output of C++ Program | Set 4
- Output of C++ Program | Set 5
- Output of C++ Program | Set 6
- Output of C Program | Set 21
- Output of C++ Program | Set 7
- Output of C++ Program | Set 9
- Output of C++ Program | Set 8
- Output of C Program | Set 22
- Output of Java Program | Set 1
- Output of Java Program | Set 2
- Output of C++ Program | Set 10
- Output of C Program | Set 23
- Output of Java Program | Set 3
- Output of C Program | Set 24
Improved By : Akanksha_Rai

