Q:

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

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 4).

Program 1:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Sample {
private:
    int X;

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

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

int main()
{
    Sample* S;

    S = new Sample();

    S->set(10);
    S->print();

    return 0;
}

Program 2:

#include <iostream>
#include <stdlib.h>
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* S;

    S = new Sample();

    S->set(10);
    S->print();

    free(S);

    return 0;
}

Program 3:

#include <iostream>
#include <stdlib.h>
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* S;

    S = new Sample();

    S->set(10);
    S->print();

    delete (S);

    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

Explanation:

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

Coming to the main() function, here we declared a pointer of class Sample and instantiate using new operator and call set() and print() functions using the pointer. And when we use the new operator, it will call constructor, but when we use malloc() then the constructor will not call.

Answer Program 2:

Output:

Constructor called 10

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 declared a pointer of class Sample and instantiate using new operator and called set() and print() functions using pointer. And we called free() function to release the memory, but in this program, the destructor is not called, because when we use the free() function, the destructor will not call.

Answer Program 3:

Output:

Constructor called 10
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 declared a pointer of class Sample and instantiate using new operator, and called set() and print() functions using pointer. And we called delete operator to release the memory then it will also call the destructor. but when we use the free() function, the destructor will not call.

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++ Constructor and Destructor | Find output progr... >>
<< C++ Constructor and Destructor | Find output progr...