/*
* CS 70 Petting Zoo, version 3 (Inheritance with virtual functions)
*
* For simplicity, this file contains all the classes and functions
*/
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>
#include <cstddef>
// Base class
class Animal {
public:
explicit Animal(std::string name, size_t hunger = 0);
virtual ~Animal() = default;
virtual void speak() const;
virtual void feed();
protected:
std::string name_;
size_t hunger_;
};
Animal::Animal(std::string name, size_t hunger) : name_{name}, hunger_{hunger} {
// Nothing (else) to do
}
void Animal::speak() const {
// idk what it even means for a generic animal to speak
std::cout << name_ << " makes nonspecific animal noises" << std::endl;
}
void Animal::feed() {
std::cout << name_ << " eats" << std::endl;
if (hunger_ > 0) {
--hunger_;
}
}
// Derived class #1: Cow
class Cow : public Animal {
public:
explicit Cow(std::string name);
~Cow() override = default;
void speak() const override;
void party() const;
private:
double happiness_;
};
Cow::Cow(std::string name) : Animal{name, 3}, happiness_{7.5} {
// Nothing (else) to do
}
void Cow::speak() const {
std::cout << name_ << " says: Mooooo" << std::endl;
}
void Cow::party() const {
std::cout << name_ << " parties!!" << std::endl;
}
// Derived class #2: Raptor
class Raptor : public Animal {
public:
explicit Raptor(std::string name);
~Raptor() override = default;
void speak() const override;
private:
double anger_;
};
Raptor::Raptor(std::string name) : Animal{name, 42}, anger_{11} {
// Nothing (else) to do
}
void Raptor::speak() const {
std::cout << name_ << " says: Rawrrr" << std::endl;
}
void engageWith(Animal& animal) {
animal.speak();
animal.feed();
}
int main() {
Cow bessie("Bessie");
Raptor peri("Peri");
std::cout << "## Petting the Cow:" << std::endl;
engageWith(bessie);
std::cout << "\n## Petting the Raptor:" << std::endl;
engageWith(peri);
return 0;
}