×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Constructor and Destructor | Find output programs | Set 5

This section contains the C++ find output programs with their explanations on C++ Constructor and Destructor (set 5).
Submitted by Nidhi, on June 12, 2020

Program 1:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        cout << "Constructor called ";
    }

    ~Sample()
    {
        cout << "Destructor called ";
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample S1;
    S1.set(10);

    Sample S2 = S1;

    S1.print();
    S2.print();

    return 0;
}

Output:

Constructor called 10
10
Destructor called Destructor called

Explanation:

In the above program, we created a class Sample with data member X, default constructor, destructor, set() and print() member functions.

Coming to the main() function, here we created two objects S1 and S2.

Here we created the S1 object and set values using set() member function. And then created S2 object and initialized it with S2 using the assignment operator, in this case, the constructor will call but we can copy the values of data members.

The constructor will be called for S1 and print() function for S1 will print 10 and then print() function for S2 will print 10 and the destructor will call for both S1 and S2.

Program 2:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        cout << "Constructor called " << endl;
    }

    Sample(Sample& ob)
    {
        cout << "Copy constructor called " << endl;
        X = ob.X;
    }

    ~Sample()
    {
        cout << "Destructor called " << endl;
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample S1;
    S1.set(10);

    Sample S2(S1);

    S1.print();
    S2.print();

    return 0;
}

Output:

Constructor called
Copy constructor called
10
10
Destructor called
Destructor called

Explanation:

In the above program, we created a class Sample with data member X, default constructor, copy constructor, destructor, set() and print() member functions.

Coming to the main() function, here we created two objects S1 and S2.

Here we created the S1 object and set values using set() member function. And then create an S2 object and initialized with S2 using the copy constructor.

So finally the constructor will call for S1, copy constructor will call for S2.

The print() function for S1 will print 10 and then print() function for S2 will print 10.

And then the destructor will call for both S1 and S2.

Program 3:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        X = 0;
    }
    Sample(Sample ob)
    {
        X = ob.X;
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample S1;
    S1.set(10);

    Sample S2(S1);

    S1.print();
    S2.print();

    return 0;
}

Output:

main.cpp:13:21: error: invalid constructor; you probably meant ‘Sample (const Sample&)’
     Sample(Sample ob)
                     ^

Explanation:

The above code will generate an error because we did not use ampersand '&' in the copy constructor. It is mandatory to define copy constructor in C++.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.