xxxxxxxxxx
deallocate and clean up c++ object and class member after get destroyed
xxxxxxxxxx
#include <iostream>
class Entity {
public:
float x, y;
Entity() {
x = 0.0f;
y = 0.0f;
// the above is not a good practice ,instead you can use constructor member initializer list to initialize variables
std::cout << "Created Entity" << std::endl;
std::cout << "x " << x << " y " << y << std::endl;
//This is a constructor and it gets called everytime we instantiate an object
}
~Entity() {
//This is a destructor object it gets called every time object is destroyed or its scope ends
//Note1:that this function can never return anything
//Note2:Followed by this ~ symbol the name of the function must be equal to class name
std::cout << "[Destroyed Entity]" << std::endl;
}
};
int main(){
{
Entity e1;
//here constructor is called and output => Created Entity
//here constructor is called and output => 0,0
}
//here Destructor is called and output => Destroyed Entity
// Destructor will get called here when compiler will get out of the end bracket and the lifetime of object ends
// have a graeater look in debug mode
std::cin.get();
}
xxxxxxxxxx
class A
{
// constructor
A()
{
cout << "Constructor called";
}
// destructor
~A()
{
cout << "Destructor called";
}
};
int main()
{
A obj1; // Constructor Called
int x = 1
if(x)
{
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj1
Destructors in c++ example
xxxxxxxxxx
// Example:
#include<iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"\n Constructor executed";
}
~Test()
{
cout<<"\n Destructor executed";
}
};
main()
{
Test t;
return 0;
}
xxxxxxxxxx
Line::Line( double len): length(len) {
cout << "Object is being created, length = " << len << endl;
}
xxxxxxxxxx
Line::Line( double len): length(len) {
cout << "Object is being created, length = " << len << endl;
}
xxxxxxxxxx
class base
{
public:
base() {}
base(const base &that) : m1_(that.m1_), m2_(that.m2_) {}
base &operator=(const base &that)
{
this->m1_ = that.m1_;
this->m2_ = that.m2_;
return *this;
}
~base() {}
private:
int m1_;
char *m2_;
};
class demo
{
public:
demo() {}
// Default constructs three base objects
demo(demo &d) {}
// Copies three base objects in ary_[]
demo &operator=(const demo &that)
{
this->ary_[0] = that.ary_[0];
this->ary_[1] = that.ary_[1];
this->ary_[2] = that.ary_[2];
return *this;
}
~demo() {}
// Default destructs three base objects
private:
base ary_[3];
};
class derived : public base
{
public:
derived() : base() {}
// Constructs m3_[]
derived(derived &that) : base(that) {} // Copies m3_[]
derived &operator=(const derived &that)
{
static_cast<base &>(*this) =
static_cast<const base &>(that);
this->m3_[0] = that.m3_[0];
this->m3_[1] = that.m3_[1];
this->m3_[2] = that.m3_[2];
return *this;
}
~derived() {} // Calls ~base( ), destructs 3 demo objects
private:
demo m3_[3];
};
xxxxxxxxxx
class House {
private:
std::string location;
int rooms;
public:
// Constructor with default parameters
House(std::string loc = "New York", int num = 5) {
location = loc;
rooms = num;
}
void summary() {
std::cout << location << " house with " << rooms << " rooms.\n";
}
// Destructor
~House() {
std::cout << "Moved away from " << location;
}
};