Output of C++ Program | Set 1
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 29
- Output of C++ Program | Set 20
- Output of C++ Program | Set 19
- Output of C++ Program | Set 13
- Output of C Program | Set 27
- Output of C++ Program | Set 12
- Output of C++ Program | Set 11
- Output of C Program | Set 28
- Output of C Program | Set 26
- Output of C program | Set 25
- Output of C++ Program | Set 14
- Output of C++ Program | Set 18
- Output of C++ Program | Set 17
- Output of C++ Program | Set 16
- Output of C++ Program | Set 15
Improved By : Akanksha_Rai



