Skip to content
Related Articles

Related Articles

Output of C++ Program | Set 1
  • Difficulty Level : Easy
  • Last Updated : 21 May, 2019

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;
}


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;
}


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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :