Open In App

Output of C++ programs | Set 27 (Constructors and Destructors)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Constructors and Destructors

1) What is the output of the following program?




#include <iostream>
using namespace std;
class Sand
{
    public: Sand()
        {
            cout << "Sand ";
        }
        ~Sand()
        {
            cout << "~Sand ";
        }
};
class Rock
{
    public: Rock()
        {
            cout << "Rock ";
        }
        ~Rock()
        {
            cout << "~Rock ";
        }
};
class Hill: public Sand
    {
        Rock data_;
        public: Hill()
            {
                cout << "Hill " << endl;
            }
            ~Hill()
            {
                cout << "~Hill ";
            }
    };
  
int main()
{
    Hill h;
    return 0;
}


Output:

Sand Rock Hill 
~Hill ~Rock ~Sand 

Explanation: First the base class object is constructor is called , then the embedded object (data member) is
constructor is called and finally the inherited constructor is executed. The order of destruction is the reverse.

2) What is the output of the following program?




class gfg
{
        ~gfg()
        {
            cout << "I love coding";
        }
    public:    gfg() 
        {
            cout << "I am at your service";
        }
};
int main()
{
   gfg mygfg;
}


Output: Compilation error

error: ‘gfg::~gfg()’ is private

Explanation:
Program will not compile as the destructor is private.
3) What is the output of the following program?




#include <iostream>
using namespace std;
class
    public: void foo() 
    {
        cout << "X::f ";
    }
    virtual void go()
    {
        cout << "X::g ";
    }
};
class Y: public
    {
        public: virtual void foo() 
            {
                cout << "Y::f ";
            }
        virtual void go() 
            {
                 cout << "Y::g "
            }    
     };
int main()
{
    X x;
    Y y;
    X& rx = y;
    rx.foo();
    rx.go();
    return 0;
}


Output:

X::f Y::g

Explanation:Method X::f is non-polymorphic in X (it gets polymorphic only in class Y ). Hence rx binds to the reference type ( X& ) and not with the underlying object ( Y& ).

4) What is the output of the following program?




class A
{
    public: A(int)
    {
    }
};
  
void f()    
{
    A a[10];
}
  
int main()
{
 f();
}


Output: compilation error

no matching function for call to ‘A::A()’

Explanation: add public A(){}; to the definition of class A. You need a parameter-less constructor to be able to create an instance of your class. Current constructor requires two input string parameters. Normally C++ implies having such a constructor ( = default parameter-less constructor) if there is no other constructor declared. By declaring your first constructor with two parameters you overwrite this default behavior and now you have to declare this constructor explicitly.

5) What is the output of the following program?




#include <iostream>
using namespace std;
   
class GFG
{
public:
      GFG()
      {
         cout << "Hi from GFG. ";
      }
} g;
   
int main()
{
    cout << "You are in Main";
    return 0;
}


Output:

Hi from GFG. You are in Main

Explanation: g is a object of class GFG for which constructor is fired first.Then statement of cout of main is printed.



Last Updated : 12 Jun, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads