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

1. Create a class called Mammal. All mammals have a weight, so its data should b

ID: 3825433 • Letter: 1

Question

1. Create a class called Mammal. All mammals have a weight, so its data should be the mammal’s weight. Provide a default constructor that sets the mammal’s weight to 0, and another constructor that allows the weight to be set by the client. To allow tracking of how your code executes, your constructors should each output a message when they are called (e.g., “Invoking Mammal default constructor” for the default constructor). Your Mammal class should also have a method called Speak that outputs a message (e.g., “Mammal is speaking”) when called. Your class should also have Get and Set methods to allow the weight to be accessed.

2. From the Mammal class derive Dog, Cat, Horse, and Pig classes. The derived classes should each have a constructor that outputs an appropriate message (e.g., the Dog constructor outputs “Invoking Dog constructor”). The derived classes should each have a data member to hold the animal’s name, and they should each have a method called Speak that overrides the Mammal Speak method. Dog Speak should output “Woof”, Cat Speak should output “Meow”, Horse Speak should output “I’m Mr. Ed”, and Pig Speak should output “Oink”. The class should include Get and Set methods to allow the name to be accessed.

3. Write a main function that uses the Mammal and derived classes as needed to do the following. You must perform the actions below in the sequence described: i.e., don’t take a shortcut to using dynamic memory allocation and virtual methods since they are the whole point of the lab.

            a. Use the rand() function to generate a random weight between 0 and 150 pounds (Think modulus. Note: they’re small horses and pigs!). Your program should use a seed value of 100 and set the seed only once.

b. Prompt the user to make an animal selection (e.g., (1) for dog, (2) for cat, (3) for horse, and (4) for pig and to enter a name for the animal. Dynamically create a Dog, Cat, Horse, or Pig object (depending on what the user entered) using the constructor that allows you to initialize the weight (use the randomly calculated weight). Save the object (Think array).

c. Repeat steps a and b five times. You do not know what animals the user will select or in what order so you must figure out how to create and store the appropriate objects.

            d. After the user has entered all five selections, execute another loop that cycles through the five selections and invokes the Speak method and also displays the name and weight for the animal. If you’ve done it properly, each of your outputs will correspond to the type of Mammal the user selected in the order they entered them.

            e. Finally, figure out how to have the Mammal “speak”, and add the code that causes it to happen.

C++

Explanation / Answer

#include <iostream>
#include <cstdlib> //header file for rand() function
#include <time.h>
using namespace std;

class Mammal
{
    private:
    int weight;

  
    public:
    Mammal()
    {
        weight = 0;
        cout<<"Invoking Mammal default constructor";
    }
    Mammal(int weight)
    {
        this->weight = weight;
    }
    virtual void speak()
    {
        cout<<"Mammal is speaking";
    }
    void setWeight(int weight)
    {
        this->weight = weight;
    }
    int getWeight()
    {
        return weight;
    }
    virtual void setName()
    {
    }
  
    virtual string getName()
    {
      
    }
};

class Dog :public Mammal
{
private:
string name;

public:
Dog()
{
     cout<<"Invoking Dog constructor";
}

void speak()
{
     cout<<" Woof";
}
  
void setName(string name)
{
     this->name = name;
}
string getName()
{
     return name;
}
};
class Cat :public Mammal
{
private:
string name;

public:
Cat()
{
     cout<<"Invoking cat constructor";
}

void speak()
{
     cout<<" Meow";
}
  
void setName(string name)
{
     this->name = name;
}
string getName()
{
     return name;
}
};
class Horse :public Mammal
{
private:
string name;

public:
Horse()
{
     cout<<"Invoking horse constructor";
}

void speak()
{
     cout<<" I’m Mr. Ed";
}
  
void setName(string name)
{
     this->name = name;
}
string getName()
{
     return name;
}
};
class Pig :public Mammal
{
private:
string name;

public:
Pig()
{
     cout<<"Invoking Pig constructor";
}

void speak()
{
     cout<<" Oink";
}
  
void setName(string name)
{
     this->name = name;
}
string getName()
{
     return name;
}
};

int main()
{
    Mammal *m[5];
    Dog d;
    Cat c;
    Horse h;
    Pig p;
    int weight,mammal,i;
    string name;
  
    for(i=0;i<5;i++)
    {
  
      weight = rand()%50 +100; //generate random number in the range between 0 and 150
  
   cout<<" Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig ";
   cin>>mammal;
   cout<<" Enter a name for the animal";
   cin>>name;
  
   switch(mammal)
   {
       case 1:
                m[i] = &d;
                d.setWeight(weight);
                d.setName(name);
                break;
       case 2: m[i] = &c;
                c.setWeight(weight);
                c.setName(name);
                break;
       case 3: m[i] = &h;
                h.setWeight(weight);
                h.setName(name);
                break;
       case 4: m[i] = &p;
                p.setWeight(weight);
                p.setName(name);
                break;
       default: cout<<" Invalid mammal";
                break;
   }
              
    }          
  
      for(i=0;i<5;i++)
      {
          cout<<" Name : "<<m[i]->getName()<<" Weight : "<<m[i]->getWeight();
          (*m[i]).speak();
      }
  
   return 0;
}

Output:

Invoking Mammal default constructorInvoking Dog constructorInvoking Mammal default constructorInvoking cat constructorInvoking Mammal default constructorInvoking horse constructorInvoking Mammal default constructorInvoking Pig constructor
Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig 3
Enter a name for the animal jacky
Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig 4
Enter a name for the animal Georgy
Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig 4
Enter a name for the animal Piggy
Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig 1
Enter a name for the animal Brutus
Enter animal Selection (1) for dog, (2) for cat, (3) for horse, and (4) for pig 2
Enter a name for the animal kitty
Name : Jacky   Weight : 133
I’m Mr. Ed
Name : georgy    Weight : 121
Oink
Name : Piggy   Weight : 127
Oink
Name : Brutus   Weight : 115
Woof
Name : kitty   Weight : 143
Meow