Question
Inheritance in C++ The file Animal.h contains an Animal class that stores the name and age of a generic animal. Besides the appropriate constructors, getters,
Inheritance in C++
The file Animal.h contains an Animal class that stores the name and age of a generic animal. Besides the appropriate constructors, getters, and setters, it has a function called feed() which prints out the message "Some food, please!"
Dogs are one kind of animal, so we can extend the Animal class to produce a Dog class. Create a Dog class which inherits from the Animal class and change its constructor and destructor to print more appropriate messages and change the feed() function to print a message saying "Dog food, please!"
Your class should be stored in a file called Dog.h. Your solution will be tested with the file animals.cpp.
SAMPLE OUTPUT: Creating Generic Animal Creating Dog Snoopy is 4 years old. Dog food, please! Deleting Dog Deleting Generic Animal
animals.cpp
#include
#include "Dog.h"
using namespace std;
int main(int argc, const char * argv[])
{
Dog myDog = Dog("Snoopy", 4);
cout << myDog.getName() << " is " << myDog.getAge() << " years old. ";
myDog.feed();
return 0;
}
Animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include
#include
#include
using namespace std;
class Animal {
protected:
string name;
int age;
public:
Animal() {
cout << "Creating Generic Animal" << endl;
name = "Generic Animal";
age = 0;
}
~Animal() {
cout << "Deleting Generic Animal" << endl;
}
string getName() {
return name;
}
void setName(string name) {
this->name = name;
}
int getAge() {
return age;
}
void setAge(int age) {
this->age = age;
}
void feed() {
cout << "Some food, please!" << endl;
}
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started