Virtual copy constructor : constructor « Class « C++ Tutorial





#include <iostream>
 
 class Animal
 {
 public:
     Animal():itsAge(1) { std::cout << "Animal constructor...\n"; }
     virtual ~Animal() { std::cout << "Animal destructor...\n"; }
     Animal (const Animal & rhs);
     virtual void Speak() const { std::cout << "Animal speak!\n"; }
     virtual Animal* Clone() { return new Animal(*this); } 
     int GetAge()const { return itsAge; }
 
 protected:
     int itsAge;
 };
 
 Animal::Animal (const Animal & rhs):itsAge(rhs.GetAge())
 {
     std::cout << "Animal Copy Constructor...\n";
 }
 
 class Dog : public Animal
 {
 public:
     Dog() { 
        std::cout << "Dog constructor...\n"; 
     }
     virtual ~Dog() { 
        std::cout << "Dog destructor...\n"; 
     }
     Dog (const Dog & rhs);
     void Speak()const { 
        std::cout << "Woof!\n"; 
     }
     virtual Animal* Clone() { return new Dog(*this); }
 };
 
 Dog::Dog(const Dog & rhs): Animal(rhs)
 {
     std::cout << "Dog copy constructor...\n";
 }
 
 class Cat : public Animal {
 public:
     Cat() { 
        std::cout << "Cat constructor...\n"; 
     }
     virtual ~Cat() { 
        std::cout << "Cat destructor...\n"; 
     }
     Cat (const Cat &);
     void Speak()const { 
        std::cout << "Meow!\n"; 
     }
     virtual Animal* Clone() { 
        return new Cat(*this); 
     }
 };
 
 Cat::Cat(const Cat & rhs): Animal(rhs)
 {
     std::cout << "Cat copy constructor...\n";
 }
 
 int main()
 {
     Animal *theArray[3];
     Animal* ptr;
     int choice,i;

     theArray[0] = new Dog;
     theArray[1] = new Cat;
     theArray[2] = new Animal;
      
     
     Animal *OtherArray[3];
     for (i=0;i<3;i++)
     {
         theArray[i]->Speak();
         OtherArray[i] = theArray[i]->Clone();
     }
     for (i=0;i<3;i++)
         OtherArray[i]->Speak();
     return 0;
 }
Animal constructor...
Dog constructor...
Animal constructor...
Cat constructor...
Animal constructor...
Woof!
Animal Copy Constructor...
Dog copy constructor...
Meow!
Animal Copy Constructor...
Cat copy constructor...
Animal speak!
Animal Copy Constructor...
Woof!
Meow!
Animal speak!










9.2.constructor
9.2.1.A parameterized constructor
9.2.2.Use constructor to initialize class fields
9.2.3.Initialize variables and conduct calculation in constructor
9.2.4.Overload the constructor
9.2.5.Copy constructors
9.2.6.Constructor as conversion operator
9.2.7.Virtual copy constructor
9.2.8.overload constructor
9.2.9.Overload constructor two ways: with initializer and without initializer
9.2.10.Call class constructor or not during class array declaration
9.2.11.Call default constructor when allocating an array dynamically
9.2.12.Call constructor from base class to initialize fields inherited from base class
9.2.13.Overload constructor for different data format
9.2.14.Defining and using a default class constructor
9.2.15.Constructor parameter with default value
9.2.16.If a constructor only has one parameter
9.2.17.The default constructor for class X is one that takes no arguments;