using Distructors to Release Resources with example





This video tutorial explains how to use distructors in c++ to release resources.
You will learn how to release the resources in the distructors, how to write the class definition to use the resources properly in detail with example.

source code for this tutorial

#include <iostream>
#include <string>

using namespace std;

class Human{
private:
    string *name;
    int *age;
public:
    Human(string iname, int iage){
        name = new string;
        age = new int;

        *name = iname;
        *age = iage;
    }

    void display(){
    cout << "Hi i am "<<*name<<" and i am "<<*age<<" years old"<<endl;
    }

    ~Human(){
    delete name;
    delete age;
    cout << "all memories are released"<<endl;
    }


};

int main()
{
    Human *anil = new Human("anil",24);
    anil->display();
    delete anil;

    return 0;
}

 
Back To Top