-7
#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 ?

ams
  • 24,923
  • 4
  • 54
  • 75
  • 2
    We aren't here to publish beginner books content in answers, that would come out way too broad. Please check the [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) from the FAQ and pick one. – πάντα ῥεῖ Sep 21 '15 at 09:26

2 Answers2

3

Now my question is the "Animal created" for constructor is for which object a or a_ and also the destructor is for which object a or a_ ?

Both. There is no need for two objects here.

And also please explain the procedure how the object here gets created and also the mechanism of copy constructor is applicable i.e. how the object is called and destroyed.

The object is created in createAnimal and returned to main, where it becomes a_. No copy construction is needed because it can be elided by extending the lifetime of the temporary.

The C++ standard specifically permits this optimization, one of the rare cases where optimizations are permitted that change the behavior of correct code.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

You could add more couts to find out. E.g. something like this:

Animal createAnimal()
{
    std::cout << " creation of a " << std::endl;
    Animal a;
    a.setName("Bertia");
    std::cout << " returning from createAnimal " << std::endl;
    return a;        
}


int main()
{
   std::cout << " calling createAnimal() " << std::endl; 
   Animal a_= createAnimal();
   std::cout << " calling a_.speak() " << std::endl;
   a_.speak();
   std::cout << " returning from main " << std::endl;
   return 0;

}

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Okk I did and got the output :: """""" calling createAnimal() creation of a Animal created returning from createAnimal calling a_.speak() My name is: Bertia returning from main Animal destroyed """"""""""" thus it means the animak created output is for 'a' object so why did not we got the deconstructor as soon we return from createAnimal() ? why it is after of main when 'a_' gets destroyed – Shivam Chauhan Sep 21 '15 at 09:40
  • @ShivamChauhan well, and now you (hopefully) have a chance to understand David Schwartz answer, because what you observe is just what he describes – 463035818_is_not_an_ai Sep 21 '15 at 09:44
  • @ShivamChauhan you edited the comment while I was typing... Sorry but I will not explain you why a_ gets destroyed at the end of main. If I did, I would just prevent you from reading about it in a book (and obviously you should do it) – 463035818_is_not_an_ai Sep 21 '15 at 09:46
  • What does he mean by saying ""Both. There is no need for two objects here."" – Shivam Chauhan Sep 21 '15 at 09:50
  • @ShivamChauhan Please dont get me wrong, but you are missing some C++ basics and as πάντα ῥεῖ pointed out in his comment, it would be too involved to write a self-contained answer that really anybody would be able to understand. For this purpose there are plenty of C++ beginners books and I strongly advise you to take a look at one of them. – 463035818_is_not_an_ai Sep 21 '15 at 10:02