Saturday, April 27, 2019

overriding 'virtual void ' c++ error

The following code gives me an error.




Error: overriding 'virtual void Animal::getClass()', where it says virtual void getClass() { cout << "I'm an animal" << endl; }




Error: conflicting return type specified for 'virtual int Dog::getClass()', where it says getClass(){ cout << "I'm a dog" << endl; }




Also, it says:




Class 'Dog' has virtual method 'getClass' but non-virtual destructor





#include 
#include
#include
#include
#include
#include // srand, rand
#include

using namespace std;


class Animal{

public:
void getFamily(){ cout << "We are animals " << endl;}

virtual void getClass() { cout << "I'm an animal" << endl; }
};

class Dog : public Animal{


public:
getClass(){ cout << "I'm a dog" << endl; }

};

void whatClassAreYou(Animal *animal){

animal -> getClass();

}


int main(){

Animal *animal = new Animal;
Dog *dog = new Dog;

animal->getClass();
dog->getClass();

whatClassAreYou(animal);

whatClassAreYou(dog);


return 0;
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...