#include<iostream>
using namespace std;
class Animal
{
private:
string name;
public:
Animal()
{
cout << "Animal created" << endl;
}
Animal(const Animal& other):
name(other.name){
cout << "Animal created by copying" << endl;
}
~Animal()
{
cout << "Animal destroyed" << endl;
}
void setName(string name)
{
this->name = name;
}
void speak()const{
cout << "My name is: " << name << endl;
}
};
Animal createAnimal()
{
Animal a;
a.setName("Bertia");
return a;
}
int main()
{
Animal a_= createAnimal();
a_.speak();
return 0;
}
I got the output:
Animal created
My name is: Bertia
Animal destroyed
The "Animal created" constructor called here is for which object a or a_ and also for destructor . Is it for called where we define Animal a or when we call createAnimal() for a_ And same goes for destructor , when does it get called after the end of main function for a_ or after the end of createAnimal() function for a ?