Q:

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

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Constructor and Destructor (set 5).

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

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.

Answer Program 2:

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.

Answer Program 3:

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++.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ this Pointer | Find output programs | Set 1... >>
<< C++ Constructor and Destructor | Find output progr...