Pet.h
#ifndef PET
#define PET
class Pet{ // Abstract Class
public:
Pet(std::string name);
Pet(); // Constructor Overloadding
void walk(int pace);
virtual void makeSound() = 0; // Abstract method
virtual void favoriteFood() = 0;
protected:
int age;
std::string name;
std::string hairColor;
};
#endif
Pet.cpp
#include <iostream>
#include <string>
#include "Pet.h"
using namespace std;
Pet::Pet(string name):name(name){
cout << "In Pet's Constructor\n";
}
Pet::Pet(){
string name;
cout << "The pet has no name, will you please give it a name?\n>>";
cin >> name;
this->name = name;
cout << "Awesome, the pet seems to love the name, " << name << endl;
}
void Pet::walk(int pace){
cout << name << " walks ";
if(pace>1)
cout << "paces";
else
cout << "pace";
cout << ".\n";
}
Dog.h
#ifndef DOG
#define DOG
#include "Pet.h"
class Dog : public Pet{
public:
Dog(std::string name);
void makeSound();
void favoriteFood();
};
#endif
Dog.cpp
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
Dog::Dog(string name):Pet(name){}
void Dog::makeSound(){
cout << "Woff\n";
}
void Dog::favoriteFood(){
cout << this->name << " loves to chew bones.\n";
}
Cat.h
#ifndef CAT
#define CAT
#include "Pet.h"
#include <vector>
class Cat : public Pet{ // Inheritance
public:
Cat(std::string name);
Cat();
Cat(const Cat& cloneCat); // Copy Constructor
~Cat(); // Destructor
Cat& operator=(const Cat& anotherCat);
void makeSound();
void favoriteFood();
void eat(std::string food);
void ate();
private:
void copy(const Cat& anotherCat);
std::vector<std::string> stomache;
};
#endif
Cat.cpp
#include <iostream>
#include <string>
#include "Cat.h"
using namespace std;
Cat::Cat(string name): Pet(name){}
Cat::Cat(): Pet(){}
Cat::Cat(const Cat& cloneCat) : Pet(cloneCat.name){
copy(cloneCat);
cout << "In Cat's Copy Constructor\n";
}
Cat::~Cat(){
stomache.clear();
}
Cat& Cat::operator=(const Cat& anotherCat){
if(this != &anotherCat){ //self check
copy(anotherCat);
}
return *this;
}
void Cat::makeSound(){
cout << "Meow\n";
}
void Cat::favoriteFood(){
cout << this->name << " loves to eat fish.\n";
}
void Cat::eat(string food){
cout << name << " gobbles up " << food << ".\n";
stomache.push_back(food);
}
void Cat::copy(const Cat& cloneCat){
this->age = cloneCat.age;
this->hairColor = cloneCat.hairColor;
for(size_t i = 0; i < cloneCat.stomache.size(); ++i){ // deep copying
this->stomache.push_back(cloneCat.stomache.at(i));
}
}
void Cat::ate(){
cout << name << " ate: \n";
for(int i = 0; i < stomache.size(); ++i){
cout << "\t" << i << ". " << &stomache[i] << ": " << stomache[i] << endl;
}
}
main.cpp
#include <iostream>
#include "Cat.h"
#include "Dog.h"
using namespace std;
int main(){
Cat neko("neko mimi");
Dog wan("wan chan");
Pet* aPet = new Cat("Cat King");
cout << "The following is the polymorphism.\n";
aPet->makeSound();
aPet->favoriteFood();
// Cat meowchan;
cout << "neko's address: " << &neko;
for(int i = 0; i <= 5; ++i)
neko.eat("fish");
neko.ate();
Cat kawaii = neko;
cout << "kawaii's address: " << &kawaii;
kawaii.ate();
return 0;
}




