Project 2: Pet The class Pet represents a domesticated animal. As you\'ll from i
ID: 3768290 • Letter: P
Question
Project 2: Pet
The class Pet represents a domesticated animal. As you'll from its class diagram, it is a concrete (instantiatable) class which might represent a household animal. The class Dog is a much more specific kind of Pet that identifies an animal of canine variety. Following the diagrams show below, create the classes Pet and Dog. A sample driver has been provided to guide your efforts. IN ORDER TO RECEIVE FULL CREDIT, YOUR SUBCLASS MUST CALL ITS PARENT CONSTRUCTOR AS WELL AS REUSE ITS PARENT'S PROTECTED MEMBERS.
Pet( );
Pet( std::string name, std::string variety, std::string says );
std::string getName( ) const;
std::string getVariety( ) const;
virtual std::string speak( );
Dog( );
Dog( std::string name );
virtual std::string speak( );
// THESE ARE PROTECTED!!!
std::string myName;
std::string myVariety;
std::string howIspeak;
// BY DEFAULT, DOG'S ARE
// THE VARIETY "Canine" AND
// SPEAK BY SAYING "Bark"
Pet * p = new Pet( "Sunset", "Cat", "meow..." );
Dog * d = new Dog( "Muffin" );
cout << "The " << p->getVariety( ) << " " << p->getName( ) << " speaks by saying " << p->speak( ) << endl;
cout << "The " << d->getVariety( ) << " " << d->getName( ) << " speaks by saying " << d->speak( ) << endl;
Pet DogPet( );
Pet( std::string name, std::string variety, std::string says );
std::string getName( ) const;
std::string getVariety( ) const;
virtual std::string speak( );
Dog( );
Dog( std::string name );
virtual std::string speak( );
// THESE ARE PROTECTED!!!
std::string myName;
std::string myVariety;
std::string howIspeak;
// BY DEFAULT, DOG'S ARE
// THE VARIETY "Canine" AND
// SPEAK BY SAYING "Bark"
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
class Pet {
protected:
std::string myName;
std::string myVariety;
std::string howIspeak;
public:
Pet( ) {
}
Pet( std::string name, std::string variety, std::string says ) {
myName = name;
myVariety=variety;
howIspeak = says;
}
std::string getName( ) const {
return myName;
}
std::string getVariety( ) const {
return myVariety;
}
virtual std::string speak( ) {
return howIspeak;
}
};
class Dog:public Pet {
public:
Dog( ) {
}
Dog( std::string name ) : Pet(name, "Canine", "Bark") {
}
virtual std::string speak( ) {
return howIspeak;
}
};
int main() {
Pet * p = new Pet( "Sunset", "Cat", "meow..." );
Dog * d = new Dog( "Muffin" );
cout << "The " << p->getVariety( ) << " " << p->getName( ) << " speaks by saying " << p->speak( ) << endl;
cout << "The " << d->getVariety( ) << " " << d->getName( ) << " speaks by saying " << d->speak( ) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.