#include <iostream> #include <cstring> using namespace std; class Animal { publi
ID: 3588012 • Letter: #
Question
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
Animal (const string& n = "", const string& s = "");
~Animal();
string getName();
string makeSound();
friend ostream &operator << (ostream &output, const Animal &A);
private:
string name;
string sound;
};
Animal::Animal(const string& n, const string& s) //constructor intialization of variables
{
name = n;
sound = s;
}
Animal::~Animal()
{
cout << "Object will now be deleted" << endl;
}
string Animal::getName() //getter functions to return name,health,attack and defense of Monster
{
return name;
}
string Animal::makeSound()
{
return sound;
}
ostream &operator<<(ostream &output, const Animal &S) //overloading operator
{
output << S.name << " " << "says" << " " << S.sound;
return output;
}
int main()
{
Animal* monkey = new Animal("Frank", "Squeak");
Animal* tiger = new Animal("Simba", "Chuff");
Animal* Dog = new Animal("Petals", "Woof");
cout<< *monkey <<endl;
cout<< *tiger <<endl;
cout<< *Dog << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
Animal (const string& n = "", const string& s = "");
~Animal();
string getName();
string makeSound();
friend ostream &operator << (ostream &output, const Animal &A);
private:
string name;
string sound;
};
Animal::Animal(const string& n, const string& s) //constructor intialization of variables
{
name = n;
sound = s;
}
Animal::~Animal()
{
cout << "Object will now be deleted" << endl;
}
string Animal::getName() //getter functions to return name,health,attack and defense of Monster
{
return name;
}
string Animal::makeSound()
{
return sound;
}
ostream &operator<<(ostream &output, const Animal &S) //overloading operator
{
output << S.name << " " << "says" << " " << S.sound;
return output;
}
int main()
{
Animal* monkey = new Animal("Frank", "Squeak");
Animal* tiger = new Animal("Simba", "Chuff");
Animal* Dog = new Animal("Petals", "Woof");
cout<< *monkey <<endl;
cout<< *tiger <<endl;
cout<< *Dog << endl;
return 0;
}
Question In a popular business simulation video game, Zoo Tycoon, players create a Zoo. This includes creating exhibits to display their favorite animals. The figure below shows an example of how different animals are put into different exhibits. In the game players can put animals in the same exhibit if the animals share similar resources for survival. For example, a zebra and gazelle can be put into the same exhibit since they both live in a savannah biome. In this lab you are required to create a class called Animal with the private fields called name and sound. The Animal class should ave the following definition: class Animalf private: string name;
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
Animal (const string& n = "", const string& s = "");
~Animal();
string getName();
string makeSound();
friend ostream &operator << (ostream &output, const Animal &A);
private:
string name;
string sound;
};
Animal::Animal(const string& n, const string& s) //constructor intialization of variables
{
name = n;
sound = s;
}
Animal::~Animal()
{
cout << "Object will now be deleted" << endl;
}
string Animal::getName() //getter functions to return name,health,attack and defense of Monster
{
return name;
}
string Animal::makeSound()
{
return sound;
}
ostream &operator<<(ostream &output, const Animal &S) //overloading operator
{
output << S.name << " " << "says" << " " << S.sound;
return output;
}
int main()
{
Animal* monkey = new Animal("Frank", "Squeak");
Animal* tiger = new Animal("Simba", "Chuff");
Animal* Dog = new Animal("Petals", "Woof");
cout<< *monkey <<endl;
cout<< *tiger <<endl;
cout<< *Dog << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.