Output of C++ Program | Set 1

Predict the output of below C++ programs.

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


Ouput:
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


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.