Skip to content

Instantly share code, notes, and snippets.

@iluxonchik
Created October 7, 2014 20:13
Show Gist options
  • Save iluxonchik/7ed9244e103dd5b7f878 to your computer and use it in GitHub Desktop.
Save iluxonchik/7ed9244e103dd5b7f878 to your computer and use it in GitHub Desktop.
#include "Animal.h"
#include <string>
#include<iostream>
Animal::Animal(int age, std::string name) : _name(name), _age(age) { }
Animal::~Animal() { }
void Animal::sleep(){ printf("Shhh! %s is sleeping...\n", this->name); }
// Operator overloading
bool Animal::operator==(const Animal &animal){
return (this->age == animal.age()) && (this->name == animal.name());
}
std::ostream &operator<<(std::ostream &o, const Animal &animal){
return o << "Name: " << animal.name() << " Age: " << animal.age();
}
#include <string>
class Animal
{
private:
int _age;
std::string _name;
public:
Animal(int age, std::string name);
~Animal();
void sleep();
int age(void) const { return _age; }
std::string name() const { return _name; }
bool operator==(const Animal &animal); // the other animal is referenced by "this"
friend std::ostream &operator<<(std::ostream &o, const Animal &animal);
};
#include "Cat.h"
#include <string>
#include <iostream>
Cat::Cat(int age, std::string name, int weight) : Animal::Animal(age, name), _weight(weight) {}
Cat::~Cat()
{
}
void Cat::climb(){
std::cout << this->name() + " is climbing...";
}
std::ostream &operator<<(std::ostream &o, const Cat cat){
o << (Animal&)cat << " Weight: " << cat.weight();
return o;
}
bool Cat::operator==(const Cat &cat){
return (Animal)*this == (Animal)cat && this->weight() == cat.weight();
}
#include "Animal.h"
#include <string>
class Cat : public Animal
{
private:
int _weight;
public:
Cat(int age, std::string name, int weight);
~Cat();
int weight() const { return _weight; }
void climb();
bool operator==(const Cat &cat);
friend std::ostream &operator<<(std::ostream &o, const Cat &cat);
};
#include "Dog.h"
#include <iostream>
#include <string>
Dog::Dog(int age, std::string name, int numLifes) : Animal::Animal(age, name), _numLifes(numLifes) {}
Dog::~Dog() {};
void Dog::bark()
{ std::cout << ((Animal*)this)->name() + " says: \"ruff, ruff!\""; }
bool Dog::operator==(const Dog &dog){
return (Animal)*this == (Animal)dog && this->numLifes() == dog.numLifes();
}
std::ostream& operator<<(std::ostream &o, const Dog &dog){
o << (Animal)dog << " Number Of Lifes: " << dog.numLifes();
}
#include "Animal.h"
#include <string>
#include <iostream>
class Dog : public Animal{
private:
int _numLifes;
public:
Dog(int age, std::string name, int numLifes);
~Dog();
void bark();
int numLifes() const { return _numLifes; }
bool operator==(const Dog &dog);
friend std::ostream& operator==(std::ofstream& o, const Dog &dog);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment