#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();
}
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
deallocate and clean up c++ object and class member after get destroyed
Line::Line( double len): length(len) {
cout << "Object is being created, length = " << len << endl;
}
Line::Line( double len): length(len) {
cout << "Object is being created, length = " << len << endl;
}
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.
";
}
// Destructor
~House() {
std::cout << "Moved away from " << location;
}
};
Moved away from New York
Code Example |
---|
Cpp :: Find first and last digit of int |
Cpp :: std::string remove last |
Cpp :: how to make sound in c++ |
Cpp :: uses of c++ |
Cpp :: open a url with dev c |
Cpp :: binary add using strings |
Cpp :: set elements to 42 back |
Cpp :: c++ influenced |
Cpp :: how to make a c++ iostream program restart when finished |
C :: C output color font |
C :: install kubernetes kubectl on mac |
C :: How to install npm in alpine linux |
C :: Sorting number excluding elements in highest to lowest |
C :: space after format specifiers in c |
C :: printf with bool |
C :: how to search in a file in c |
C :: boilerplate code c |
C :: Succ de ch |
C :: convert number to string c |
C :: find smallest number in array in c |
C :: c assign pointer to struct |
C :: write a binary file c |
C :: sum average min max in c array |
C :: how to print sizes of various data types of C |
C :: c style string |
C :: try and catch in rust |
C :: How to Convert double to int in C |
C :: fgets c |
C :: printf("%3d ",XX); |
C :: turn a char array into double C |