Question: C++ Code : #include #include #include #include #include #include class animal { public: virtual ~animal() { } virtual std::string kind() const = 0; }; using

C++

Code :

#include

#include

#include

#include

#include

#include

class animal

{

public:

virtual ~animal() { }

virtual std::string kind() const = 0;

};

using animal_ptr = std::unique_ptr;

template

animal_ptr make_animal(T&& t)

{

using type = std::remove_cvref_t;

return animal_ptr{ new type(std::forward(t)) };

}

// cat class which is child class of animal class cat : public animal { public: // virtual destructor virtual ~cat(){} // override and implement kind function std::string kind() const override { using namespace std::literals; return "Felis catus\"s"; } };

// end of cat class

class Dog : virtual public animal { string value; public: Dog() = delete; Dog(Dog &obj) = default; Dog &operator=(const Dog &) = default; Dog(string const &str) : value(str) { // something here } string kind() const override { return "Canis lupus familiaris"; } string const &name() { return value; } };

Questions :

define a class called dogcat as follows:

  • the class (multiply) inherits both publicly and virtually from both dog and from cat,
  • the class has a default constructor that invokes the parent dog constructor with the name "hybrid" and the default cat constructor,
  • the class has a constructor that has two parameters (dog const& d and cat const& c) and invokes the corresponding parameter class' copy constructor passing in d or c respectively,
  • the class has a defaulted copy constructor,
  • the class has a defaulted copy assignment operator, and,
  • the class has a kind() member function that overrides animal::kind and returns the result of calling dog's kind(), + (i.e., string append), the string " + ", + (i.e., string append), and calling cat's kind().
  • Writing Operator:

    To make it easier to create dogcat hyrbids, write an operator +() overload as follows:

  • the function has two parameters: dog const& d and cat const& c,
  • the function return type is dogcat, and,
  • the return expression must create a dogcat object from d and c passing in d and c to a dogcat constructo

Writing main()

The above classes will now be used in main() as follows:

  • declare a std::vector variable called v,
  • declare a variable, d, of type dog passing in the name "Fido",
  • default construct a variable of type cat called c,
  • call v.push_back() with the result of calling make_animal(d),
  • call v.emplace_back() with the result of dynamically allocating a cat value using operator new,
  • call v.push_back() with the result of calling make_animal(d+c).

PROGRAM OUTPUT

Canis lupus familiaris, name = Fido

Felis catus

Canis lupus familiaris + Felis catus, name = Fido

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!