pls i want an answer today thank you

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
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
where should i replace getClass function thanks for your respond
Last edited on
you forgot to edit your post
what do you mean
Last edited on
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
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.
like this
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

 //

#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="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;
}
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.
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
Animal is a generic class to represent an animal. For a bird or a cat or a Fish, you set the members accordingly:
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Animal
{
private:
    string type;
    int legs;
    string sound;
public:
    // Constructor for an animal
    Animal(const string animalType, int numLegs, string theSound)
    {
	type = animalType;
	legs = numLegs;
	sound = theSound;
    }
    
    int getLegs() const
    {
	return legs;
    }
    string getSound() const
    {
	return sound;
    }
    string getClass() const
    {
	return type;
    }

    void printInfo()
    {
	cout << "This is a " << getClass() << ". It has " << legs << " legs and sound: "
	    << sound << "\n";
    }
};



class Bird:public Animal
{
public:
    Bird() : Animal("Bird", 2, "chirp")
    { }
};

int
main()
{
    Bird b;
    cout << b.getClass() << endl;
    b.printInfo();
    return 0;
}

Add to this to create the Fish and Dog classes.
Topic archived. No new replies allowed.