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

Lab 3 For this lab you\'ll work with pointers and polymorphism. Step 1 Download

ID: 3741966 • Letter: L

Question

Lab 3 For this lab you'll work with pointers and polymorphism. Step 1 Download the starter file animal.cpp and add it to a Visual Studio project. You will do all your work in this file. Step 2 In the starter file you're given a simple Animal class. The Animal has a name, a constructor which sets its name, and a speak) method which prints the noise the Animal makes and what sort of animal it is Find the I/STEP 2 comment and insert a Dog class that inherits from Animal (for all following steps insert the code at the appropriate comment). The Dog class should have its own constructor which sets its name and its own speak() method which displays "Woof! says name where name is replaced with the Animals name. Step 3 Insert a Cat class that inherits from Animal This will be identical to the Dog class, but its speakl) method will say "Meow! Says name Step 4 Write a void function called giveShot() that accepts a pointer to an Animal object as a parameter. getShot() displays "I'm giving the animal a shot.... then calls the Animal's speak) method Step 5 Create a Dog and Cat object on the free space using new. Using the constructor give the Dog and Cat names of your choosing. You'll need to save the address for each object into a pointer Step 6 Call giveshot) on your Dog object and Cat object created in step 5. Step 7 At this point you should be able to run your program. Try it out.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

// Base class Animal definition
class Animal
{
// To store animal name
string name;
public:
// Default constructor definition
Animal()
{
name = "";
}// End of default constructor.

// Parameterized constructor to assign parameter data to data member
Animal(string na)
{
name = na;
}// End of parameterized constructor

// Function to set name
void setName(string na)
{
name = na;
}// End of function

// Function to return name
string getName()
{
return name;
}// End of function

// Pure virtual function speak()
virtual void speak() = 0;
};// End of class Animal

// Class Dog defined derived from Animal class
class Dog : public Animal
{
// Data member to store sound
string sound;
public:
// Default constructor definition calls base class default constructor
Dog () : Animal()
{
sound = "";
}// End of default constructor.

// Parameterized constructor to assign parameter value to data member
// Also calls base class constructor and passes name
Dog (string name, string so) : Animal(name)
{
sound = so;
}// End of parameterized constructor

// Function to set sound
void setSound(string so)
{
sound = so;
}// End of function

// Function to return sound
string getSound()
{
return sound;
}// End of function

// Overrides speak function of Animal class
void speak()
{
// Displays sound and animal name
cout<<"""<<sound<<"!""<<" says "<<getName();
}// End of function
};// End of class Dog

class Cat : public Animal
{
// Data member to store sound
string sound;
public:
// Default constructor definition calls base class default constructor
Cat () : Animal()
{
sound = "";
}// End of default constructor.

// Parameterized constructor to assign parameter value to data member
// Also calls base class constructor and passes name
Cat (string name, string so) : Animal(name)
{
sound = so;
}// End of parameterized constructor

// Function to set sound
void setSound(string so)
{
sound = so;
}// End of function

// Function to return sound
string getSound()
{
return sound;
}// End of function

// Overrides speak function of Animal class
void speak()
{
// Displays sound and animal name
cout<<"""<<sound<<"!""<<" says "<<getName();
}// End of function
};// End of class Cat

// Function to display common message and calls the speak function
// Takes animal class pointer as parameter
void giveShort(Animal *ani)
{
cout<<" I'm giving the animal a shot ..."<<endl;
// Calls the function to display sound and animal name
ani->speak();
}// End of function

// main function definition
int main()
{
// Declares an Animal class pointer
Animal *animal;

// Creates an object of class Dog using parameterized constructor
// Assigns address to animal pointer
animal = new Dog("Bowser", "Woof");
// Calls the function to display name and sound
giveShort(animal);

// Creates an object of class Cat using parameterized constructor
// Assigns address to animal pointer
animal = new Cat("Fifi", "Meow");
// Calls the function to display name and sound
giveShort(animal);
}// End of main function

Sample Output:

I'm giving the animal a shot ...
"Woof!" says Bowser

I'm giving the animal a shot ...
"Meow!" says Fifi