so basically have this project that needs to get done
i just couldn't understand the last part of one of the questions !
so after me making 4 classes 3 of them inherit one class"animal"
i'm supposed to do this in the main:
- use the pointer *ptr of the type animal that Indicates to a new object from each class using "new"
then use the same pointer to call the function speak()
so basically the three classes are cat,dog and horse that only have the function speak which only does cout<<"the voice";
and i have to choose a number in the main and gets the noise based on that choice !
if you dont understand please let me simplify it !
i just want to know what should i do
#include <iostream>
usingnamespace std;
class Animal { //Super class, abstract, we cannot make instances of this
virtualvoid speak()=0;
};
class Cat : protected Animal
{
void speak(){cout<<"Meow!"<<endl;}
};
class Dog : protected Animal
{
void speak(){cout<<"Bark!"<<endl;}
};
class Horse : protected Animal
{
void speak(){cout<<"WeeeeSnawww!"<<endl;}//lol horses say weesnawww
};
int main(){
Cat *myCat = new Cat();//Creates a pointer to a new 'cat' object
Dog *myDog = new Dog();//Creates a pointer to a new 'dog' object
Horse *myHorse = new Horse();//Creates a pointer to a new 'horse' object
//now to make them speak
myCat->speak();
myDog->speak();
myHorse->speak();
return 0;
}
You can figure out the rest, that is pretty much the entirety of your assignment :p
Note that with abstract classes (any class that has a virtual function definition in it) you cannot create them, only classes derived from them, with the ':' operator.