May 24, 2020 at 4:00pm UTC
why this code is wrong and how i can fix it
please can you tell me why this is wrong
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
//
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Animal
{
private :
string Animal ;
int legs;
string sound;
public :
int getLegs()
const {return legs;}
string getSound()
const {return sound;}
string getClass()
const {return Animal;}
void printInfo() {
cout <<"This is a " <<getClass()<<". It has " <<legs<<" legs and sound: " <<sound<<"\n" ;
}
};
class Bird: public Animal
{
private :
int legs = 2;
string sound = "chirp" ;
string Bird;
public :
string getClass()
const {return Bird;}
};
class Dog: public Animal
{
private :
int legs = 4;
string sound = "bark" ;
string Dog ;
public :
string getClass()
const {return Dog ;}
};
class Fish: public Animal
{
private :
int legs = 0;
string sound = "blop" ;
string fish;
public :
string getClass()
const {return fish;}
};
int main()
{
Animal a;
Dog d;
Bird b;
Fish f;
cout<<a.getClass()<<endl;
cout<<d.getClass()<<endl;
cout<<b.getClass()<<endl;
cout<<f.getClass()<<endl;
return 0;
}
Last edited on May 24, 2020 at 5:01pm UTC
May 24, 2020 at 4:02pm UTC
string getClass();
You never implement your getClass() member function in Animal.
Please edit your post and use code formatting
[code]
// your code here
[/code]
Last edited on May 24, 2020 at 4:03pm UTC
May 24, 2020 at 4:10pm UTC
where should i replace getClass function thanks for your respond
Last edited on May 24, 2020 at 4:48pm UTC
May 24, 2020 at 4:45pm UTC
you forgot to edit your post
May 24, 2020 at 4:48pm UTC
what do you mean
Last edited on May 24, 2020 at 4:49pm UTC
May 24, 2020 at 5:05pm UTC
now i understand how this forum works i change the code and now is compiled properly but there is not any result thanks a lot
May 24, 2020 at 5:17pm UTC
There is no result because your variables are not initialized with some values.
ex. class Dog has string Dog;
member but it's value is nothing.
it should be string Dog = "Dog" ;
and so for all other variables, must be assigned some value so that getClass()
returns a value instead of empty string.
May 24, 2020 at 5:45pm UTC
yes, but you updated only Dog, what about fish, bird and other animals?
you need to initialize all those variables if you want to see their values in console.
May 24, 2020 at 6:01pm UTC
Also, why do you have functions like "printInfo" if you never call them?
What are the actual requirements of the assignment? Does the assignment talk about 'polymorphism'? (It's possible that the answer to this is 'no', but just thought I'd ask)
Last edited on May 24, 2020 at 6:01pm UTC