Created
October 7, 2014 20:13
-
-
Save iluxonchik/7ed9244e103dd5b7f878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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