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

Prerequisite: Constructors and Destructors

1) What is the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

This article is contributed by Pavan Gopal Rayapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


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