simple explanation if you may

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


thank you in advanced !

This will get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class Animal { //Super class, abstract, we cannot make instances of this
virtual void 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.
You are amazing , thank you very much now i think i know what to do !
No problem, glad I could help :)
Topic archived. No new replies allowed.