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

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

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

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.

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

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.

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

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.