Open In App

Output of C++ Program | Set 1

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 21 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads