Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Formal lett o/formal letter 4.pdf C a Search - + Automatic Zoom In this lab you

ID: 2247311 • Letter: F

Question

Formal lett o/formal letter 4.pdf C a Search - + Automatic Zoom In this lab you are required to create a class called Animal with the private fields calle name and sound. The Animal class should have the following definition: 1 class Animalf 2 private: string name; string sound; 5 public: Animal(const string& n- "", const string& s = ""); string getName O const; string makeSound O const; 8 The public methods of the class Animal are: the constructor, which initialises name and sou getName). which returns the name of the animal; and makeSoundO, which returns the sound animal makes. You must overload the operator

Explanation / Answer

Hi..

// the OOP version in C++
#include <iostream>

// base abstract class. that is what we use as the interface
class Animal1
{
public:
Animal1(char const* name): m_name(name) {}

// this is required to properly delete virtual classes
virtual ~Animal1() {}

// with this strange syntax we define an unimplemeted "interface" function
virtual void make_sound() = 0;

protected:
// shared data field
std::string m_name;
};

// derived class. that means that Dog is a more refined version of Animal1
// you usually inherit from only a single base class
class Dog: public Animal1
{
public:
// need to forward the constructor arguments
Dog(char const* name): Animal1(name) {}

// here we implement the interface
void make_sound() override
{
std::cout << m_name << " the dog said: bork!" << std::endl;
}

// type-specific method
void wag()
{
std::cout << "*" << m_name << " wags*" << std::endl;
}
};

// same as above, but with a different implementation
class Cat: public Animal1
{
public:
Cat(char const* name): Animal1(name) {}

void make_sound() override
{
std::cout << m_name << " the cat said: mow!" << std::endl;
}

void purr()
{
std::cout << "*" << m_name << " purrs*" << std::endl;
}
};

// We can now use Animal1 as the general type
// There is runtime type information that allows this (dynamic dispatch)
void Animal1_sound(Animal1& a)
{
// method called via the generic interface
a.make_sound();

// type information is still there, we can extract the original type by
// probing if dynamic cast works
auto d = dynamic_cast<Dog*>(&a);
if (d != nullptr)
d->wag();

auto c = dynamic_cast<Cat*>(&a);
if (c != nullptr)
c->purr();
}

// we can do static dispatch too, via templates (duck typing)
template <typename T>
void Animal1_sound_1(T& a)
{
a.make_sound();
// however, we can't easily access type information here without
// using specialization..
}

// ..and that's pretty much equivalent to overloading the function
// this works by making two different functions with internally mangled names
// for each argument combination
void Animal1_sound_2(Dog& a) { a.make_sound(); a.wag(); }
void Animal1_sound_2(Cat& a) { a.make_sound(); a.purr(); }

int main()
{
auto cat = Cat("kitty");
auto dog = Dog("puppy");
Animal1_sound(cat);
Animal1_sound(dog);
Animal1_sound_1(cat);
Animal1_sound_1(dog);
Animal1_sound_2(cat);
Animal1_sound_2(dog);
return 0;
}

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote