To all:
I need a little more help in trying to understand Abstraction and Inheritance. I want to have an Animal that is either of class Dog or Cat. Pretend that the Dog and Cat have some common features represented by CommonAnimal. For now we will say that class CommonAnimal will define the color. The specific features for Dog and Cat are contained in their respective speak() methods. When I attempt to compile the source listed below I get the following compiler errors:
Code:
Error 596: "animal3.c", line 31 # Within the base class hierarchy of class CommonAnimal, virtual member function "void
std::Animal::get_color()" is overridden in both class Dog and class Cat, and neither instance hides the other.
virtual void get_color( void ) = 0;
^^^^^^^^^
Error 596: "animal3.c", line 32 # Within the base class hierarchy of class CommonAnimal, virtual member function "void
std::Animal::speak()" is overridden in both class Dog and class Cat, and neither instance hides the other.
virtual void speak( void ) = 0;
^^^^^
Error 226: "animal3.c", line 70 # No appropriate function found for call of 'operator ='. Last viable candidate was "std::Dog
&std::Dog::operator =(const std::Dog &)" ["animal3.c", line 36]. Argument of type 'std::Dog *' could not be converted to 'const
std::Dog &'.
*ptrDog = new Dog( dog_color );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error 226: "animal3.c", line 71 # No appropriate function found for call of 'operator ='. Last viable candidate was "std::Cat
&std::Cat::operator =(const std::Cat &)" ["animal3.c", line 48]. Argument of type 'std::Cat *' could not be converted to 'const
std::Cat &'.
*ptrCat = new Cat( cat_color );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What am I doing wrong? I think that I am close but I just cannot figure it out. The source code is listed below.
Code:
///////////////////////////////////////////////////////////////////////
// ************* //
// ** Animal ** //
// ** Virtual ** //
// ************* //
// ^ ^ //
// / IS-A \ //
// / \ //
// ********* ********* //
// ** Dog ** ** Cat ** //
// ********* ********* //
// ^ ^ //
// \ IS-A / //
// \ / //
// ****************** //
// ** CommonAnimal ** //
// ****************** //
///////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <string>
#define DOG 1
#define CAT 2
namespace std{
class Animal{
public:
Animal(){ cout << "Animal Constructor Called!" << endl; }
virtual ~Animal() = 0; // pure virtual function.
virtual void get_color( void ) = 0;
virtual void speak( void ) = 0;
private:
}; // END OF ANIMAL
class Dog : virtual public Animal {
public:
Dog(){}
Dog( string c ) :color (c){
cout << "Constructing Dog" << endl;
}
void get_color( void ) { cout << "Dog Color: " << color << endl; }
void speak( void ) { cout << "Dog Says: woof woof" << endl; }
private:
string color;
}; // END OF DOG
class Cat : virtual public Animal {
public:
Cat(){}
Cat( string c ): color (c) {
cout << "Constructing Cat" << endl;
}
void get_color( void ) { cout << "Cat Color: " << color << endl; }
void speak( void ) { cout << "Cat Says: meow" << endl; }
private:
string color;
}; // END OF CAT
class CommonAnimal : public Dog, public Cat {
public:
Dog *ptrDog;
Cat *ptrCat;
string dog_color; // Simulate information known from CommonAnimal.
string cat_color; // Simulate information known from CommonAnimal.
CommonAnimal( ){
dog_color = "BLACK";
cat_color = "WHITE";
*ptrDog = new Dog( dog_color );
*ptrCat = new Cat( cat_color );
}
~CommonAnimal() {
cout << "Destroying CommonAnimal" << endl;
if ( ptrDog != NULL ) { delete ptrDog; }
if ( ptrCat != NULL ) { delete ptrCat; }
}
void get_color( int a ) {
switch( a ) {
case DOG:
ptrDog->get_color();
break;
case CAT:
ptrCat->get_color();
break;
}
}
void speak( int a ) {
switch( a ) {
case DOG:
ptrDog->speak();
break;
case CAT:
ptrCat->speak();
break;
}
}
private:
}; // END COMMONANIMAL
}// END NAME SPACE.
using namespace std;
int main ( void ) {
CommonAnimal *ptrCA = new CommonAnimal();
ptrCA->get_color(1);
ptrCA->speak(1);
ptrCA->get_color(2);
ptrCA->speak(2);
if ( ptrCA != NULL ) { delete ptrCA; }
return ( EXIT_SUCCESS );
} // END OF MAIN
Thanks in advance to all that reply.
Sincerely,
DeadPoet
Related Posts:Abstract Classes and Object Creation and Access