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

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

Program 1:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;
    int Y;

public:
    Sample()
    {
        X = 0;
        Y = 0;
    }
    Sample(int x, int y)
    {
        X = X;
        Y = Y;
    }
    void print()
    {
        cout << X << " " << Y << endl;
    }
};

int main()
{
    Sample S1;
    Sample S2(10, 20);

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

    return 0;
}

Output:

0 0
0 0 [or - Some garbage value]

Explanation:

In the above program, we created a Sample class with data member X and Y, two constructors one is default constructor and the other is the parameterized constructor.

The default constructor will initialize the data members by 0. But there is a problem with parameterized constructor; here we assign data members by itself instead of local parameters. Then, data members X, and Y will contain garbage values.

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

S1 initialized with the default constructor so the data members of S1 will be initialized with 0.

S2 object will call the parameterized constructor, so data members of S2 will contain garbage values.

When we call the print() function with S1, then it will print "0 0" on the console screen. And when we call the print() function with S2, then it will print garbage value on the console screen.

Program 2:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;
    int Y;

public:
    Sample()
    {
        X = 0;
        Y = 0;
    }
    Sample(int x, int y)
    {
        X = x;
        Y = y;
    }

    void print()
    {
        cout << X << " " << Y << endl;
    }
};

int main()
{
    Sample S1;
    Sample S2 = Sample(10, 20);
    Sample S3(30, 40);

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

    return 0;
}

Output:

0 0
10 20
30 40

Explanation:

In the above program, we created a Sample class with data member X and Y, we created two constructors one is the default constructor and the other is the parameterized constructor.

The default constructor will initialize the data members by 0, and the parameters constructor will initialize data members by specified values.

Coming to the main() function. Here we created 3 objects of S1, S2, and S3. S1 initialized by default construct whereas S2 and S3 initialized by the parameterized constructor. S2 and S3 are used in different ways to create objects with the parameterized constructor, but both have the same effect.

S1 initialized X and Y by 0 and 0 respectively.
S2 initialized X and Y by 10 and 20 respectively.
S3 initialized X and Y by 30 and 40 respectively.

We print the values of all objects using the print() function.

Program 3:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;
    int Y;

public:
    Sample()
    {
        X = 0;
        Y = 0;
    }
    void set(int x, int y)
    {
        X = x;
        Y = y;
    }

    void print()
    {
        cout << X << " " << Y << endl;
    }
};

int main()
{
    Sample S[2];
    Sample* PTR;

    PTR = S;

    PTR[0].set(10, 20);
    PTR[1].set(30, 40);

    PTR[0].print();
    PTR[1].print();

    return 0;
}

Output:

10 20
30 40

Explanation:

In the above program, we created a class Sample that contains two data members X and Y, one default constructor, set() and print() member functions.

Coming to the main() function, we created the array of objects with size 2, and a pointer that initialized with the base address of array objects. And then called set() and print() functions using the pointer, by this way functions will be called correctly and they print the assigned values.





Comments and Discussions!

Load comments ↻





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