 |
|

What will the following program output?
// Animal class
class Animal
{
private:
string type;
int age;
string intToString(int i);
public:
Animal();
Animal(string newType, int newAge);
virtual string getDescription();
virtual string getNoise()=0;
};
Animal::Animal()
{
age=0; type="Unknown";
}
Animal::Animal(string newType, int newAge)
{
age= newAge; type = newType;
}
// Converts an integer to a string
string Animal::intToString(int i)
{
stringstream ss;
ss << i;
return (ss.str());
}
string Animal::getDescription()
{
string sAge = intToString(age);
return string("Animal type: ") + type +
string(". Age: ") + sAge +
string(". Noise made: ") + getNoise();
}
// Cow class
class Cow : public Animal
{
private:
string breed;
public:
Cow();
Cow(int newAge, string newBreed);
string getBreed();
virtual string getDescription();
virtual string getNoise();
};
Cow::Cow() : Animal(), breed("")
{
}
Cow::Cow(int newAge, string newBreed)
:Animal("cow", newAge), breed(newBreed)
{
}
string Cow::getBreed()
{
return breed;
}
string Cow::getDescription()
{
return Animal::getDescription() +
". Breed: " + breed;
}
string Cow::getNoise()
{
return (string("Moo"));
}
// Main
int main()
{
Cow bessie(4,"jersey");
Animal dog("dalmatian", 5);
cout << bessie.getDescription() << endl;
cout << dog.getDescription() << endl;
return 0;
}
|
 |
| |
|
|
 |