Lets put some Dogs and Cats in a bag. The purpose of this project is to practice
ID: 645753 • Letter: L
Question
Lets put some Dogs and Cats in a bag. The purpose of this project is to practice polymorphism. A bag can hold any kind of a pet, so the method to bag a pet is defined to be abstract/virtual at the base class level, but actually invoked in the application menu with a specific cat or dog in mind.
Attached please find the interface file defining Pets class hierarchy and Bag.
Pet_T is a base class. Every pet has a name.
Cat_T and Dog_T are classes derived from Pet_T
The base class has a virtual method "WhoAmI", but the implementation should be specific to the derived classes. If invoked on a cat named Fluffy, the method needs to print a message: My name is Fluffy. I am a cat.
A Bag_T can hold several pets - a vector is used to store the pointers to pets.
Capacity of a bag is established in the constructor. The vector can grow up to that capacity.
There is a method "addToBag" to simulate adding a pet to the bag
The overloaded operator << on the Bag_T class should print all the pets in the bag
Please write the following code:
The implementation file PetsInBag.cpp which provides the code for the methods listed in the attached header file
A test app PetsInBagApp.cpp which will instantiate a bag of some size, and provide a menu:
0) Quit
1) Add a Cat
2) Add a Dog
3) List/Print content of the bag (the animals can introduce themselves using "whoAmI" method)
Explanation / Answer
Answers:
#PetsInBag.cpp
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
public class pet_t
{
public:
char name[10];
public:
void virtual whoamI()=0;
}
public class dog_t:public pet_t
{
dog_t(char str[10])
{
name=str;
}
void whoamI()
{
cout<<
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.