Thread: Virtual Copy Constructor Help

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Virtual Copy Constructor Help

    //Listing 12.11 Virtual copy constructor

    #include <iostream>
    using namespace std;
    Code:
    class Mammal
    {
    public:
    	Mammal():itsAge(1) { cout << "Mammal constructor...\n"; }
    	virtual ~Mammal() { cout << "Mammal destructor...\n"; }
    	Mammal (const Mammal & rhs);
    	virtual void Speak() const { cout << "Mammal speak!\n"; }
    	virtual Mammal* Clone() { return new Mammal(*this); }
    	int GetAge()const { return itsAge; }
    protected:
    	int itsAge;
    };
    
    Mammal::Mammal (const Mammal & rhs):itsAge(rhs.GetAge())
    /*Could you please tell me what this constructor here does.. the extra ":" is confusing me */
    {
    	cout << "Mammal Copy Constructor...\n";
    }
    
    class Dog : public Mammal
    {
    public:
    	Dog() { cout << "Dog constructor...\n"; }
    	virtual ~Dog() { cout << "Dog destructor...\n"; }
    	Dog (const Dog & rhs);
    	void Speak()const { cout << "Woof!\n"; }
    	virtual Mammal* Clone() { return new Dog(*this); }
    };
    
    Dog::Dog(const Dog & rhs):
    Mammal(rhs)
    {
    	cout << "Dog copy constructor...\n";
    }
    
    class Cat : public Mammal
    {
    public:
    	Cat() { cout << "Cat constructor...\n"; }
    	~Cat() { cout << "Cat destructor...\n"; }
    	Cat (const Cat &);
    	void Speak()const { cout << "Meow!\n"; }
    	virtual Mammal* Clone() { return new Cat(*this); }
    };
    
    Cat::Cat(const Cat & rhs):
    Mammal(rhs)
    {
    	cout << "Cat copy constructor...\n";
    }
    
    enum ANIMALS { MAMMAL, DOG, CAT};
    const int NumAnimalTypes = 3;
    int main()
    {
    	Mammal *theArray[NumAnimalTypes];
    	Mammal* ptr;
    	int choice, i;
    	for ( i = 0; i<NumAnimalTypes; i++)
    	{
    		cout << "(1)dog (2)cat (3)Mammal: ";
    		cin >> choice;
    		switch (choice)
    		{
    		case DOG:	ptr = new Dog;
    					break;
    		case CAT:	ptr = new Cat;
    					break;
    		default:	ptr = new Mammal;
    					break;
    		}
    		theArray[i] = ptr;
    	}
    	Mammal *OtherArray[NumAnimalTypes];
    	for (i=0;i<NumAnimalTypes;i++)
    	{
    		theArray[i]->Speak();
    		OtherArray[i] = theArray[i]->Clone();
    	}
    	for (i=0;i<NumAnimalTypes;i++)
    		OtherArray[i]->Speak();
      int x;
      cin>>x;
    
    	return 0;
    
    }

    /*Could you please tell me what the last two for loops do exactly because I am a bit confused......Thank you in advance */
    Last edited by incognito; 01-23-2002 at 11:16 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Mammal::Mammal (const Mammal & rhs):itsAge(rhs.GetAge())
    /*Could you please tell me what this constructor here does.. the extra ":" is confusing me */
    {
    cout << "Mammal Copy Constructor...\n";
    }
    This is a copy constructor.This constructs a mammal object from another mammal object. The extra : that is putting you off is an initialization list. It is very similar to ....
    Code:
    Mammal::Mammal (const Mammal & rhs)
    /*Could you please tell me what this constructor here does.. the extra ":" is confusing me */
    {
    itsAge=rhs.GetAge();	
    cout << "Mammal Copy Constructor...\n";
    }
    /*Could you please tell me what the last two for loops do exactly because I am a bit confused......Thank you in advance */
    The first loop fills an array with pointers to dynamically created mammal objects (or derived from mammal objects).
    The second loop shows polymorphic calls to the objects member functions.
    The third loop shows polymorphic calls to the objects member functions.
    Run the program and note the output. What do you think is happening? Which functions get called and why?
    Last edited by Stoned_Coder; 01-24-2002 at 07:53 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok, so when you call a Mammal Clone constructor a copy of the constructor is sent....or am I wrong? Why are all of the Mammal clone constructors, and the Dog's and, etc, all declared to be virtual, I thought you only declared one of them the base method/or constructor virtual if you wanted to overload them.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    none of the constructors are virtual. The function clone() is. Its job is to call a copy constructor. It will create a copy of whatever type of object it is called through.It is virtual so that when working in terms of Mammal* pointers the right function for the object gets called.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM