Created
October 8, 2014 21:12
-
-
Save iluxonchik/f8e28b683198b45482c1 to your computer and use it in GitHub Desktop.
PO Practical Class 3 C++ Exercise
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 weight) : Animal(age, name), _weight(weight) {} | |
Dog::~Dog() {}; | |
void Dog::bark() | |
{ std::cout << ((Animal*)this)->name() + " says: \"ruff, ruff!\"" << std::endl; } | |
bool Dog::operator==(const Dog &dog){ | |
return (Animal)*this == (Animal)dog && this->weight() == dog.weight(); | |
} | |
std::ostream &operator<<(std::ostream &o, const Dog &dog){ | |
return o << (Animal)dog << " Weight: " << dog.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
#ifndef ANIMAL_H | |
#define ANIMAL_H | |
#include <string> | |
class Animal | |
{ | |
private: | |
int _age; | |
std::string _name; | |
public: | |
Animal(int age, std::string name); | |
Animal(int age); | |
~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); | |
}; | |
#endif |
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 numLifes) : Animal(age, name), _numLifes(numLifes) {} | |
Cat::~Cat() | |
{ | |
} | |
void Cat::climb(){ | |
std::cout << this->name() + " is climbing..." << std::endl; | |
} | |
std::ostream &operator<<(std::ostream &o, const Cat &cat){ | |
return o << (Animal)cat << " Number Of Lifes: " << cat.numLifes(); | |
} | |
bool Cat::operator==(const Cat &cat){ | |
return (Animal)*this == (Animal)cat && this->numLifes() == cat.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
#ifndef CAT_H | |
#define CAT_H | |
#include "Animal.h" | |
#include <string> | |
class Cat : public Animal | |
{ | |
private: | |
int _numLifes; | |
public: | |
Cat(int age, std::string name, int weight); | |
~Cat(); | |
int numLifes() const { return _numLifes; } | |
void climb(); | |
bool operator==(const Cat &cat); | |
friend std::ostream &operator<<(std::ostream &o, const Cat &cat); | |
}; | |
#endif |
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 weight) : Animal(age, name), _weight(weight) {} | |
Dog::~Dog() {}; | |
void Dog::bark() | |
{ std::cout << ((Animal*)this)->name() + " says: \"ruff, ruff!\"" << std::endl; } | |
bool Dog::operator==(const Dog &dog){ | |
return (Animal)*this == (Animal)dog && this->weight() == dog.weight(); | |
} | |
std::ostream &operator<<(std::ostream &o, const Dog &dog){ | |
return o << (Animal)dog << " Weight: " << dog.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
#ifndef DOG_H | |
#define DOG_H | |
#include "Animal.h" | |
#include <string> | |
#include <iostream> | |
class Dog : public Animal{ | |
private: | |
int _weight; | |
public: | |
Dog(int age, std::string name, int numLifes); | |
~Dog(); | |
void bark(); | |
int weight() const { return _weight; } | |
bool operator==(const Dog &dog); | |
friend std::ostream &operator<<(std::ostream &o, const Dog &dog); | |
}; | |
#endif |
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 "Cat.h" | |
#include "Dog.h" | |
#include <iostream> | |
#include <string> | |
int main(int argc, char* argv[]){ | |
Animal* a1 = new Animal(1, "Animal"); | |
Animal* a2 = new Animal(1, "Animal"); | |
Animal* a3 = new Animal(2); | |
Cat *c1 = new Cat(1, "Cat1", 1); | |
Cat* c2 = new Cat(1, "Cat1", 1); | |
Cat* c3 = new Cat(2, "Cat1", 1); | |
Dog* d1 = new Dog(1, "Dog1", 1); | |
Dog* d2 = new Dog(1, "Dog1", 1); | |
Dog* d3 = new Dog(1, "Dog1", 2); | |
(*c1 == *c2) ? (std::cout << "c1 == c2" << std::endl) : (std::cout << "c1 != c2" << std::endl); | |
(*c1 == *c3) ? (std::cout << "c1 == c3" << std::endl) : (std::cout << "c1 != c3" << std::endl); | |
(*d1 == *d2) ? (std::cout << "d1 == d2" << std::endl) : (std::cout << "d1 != d2" << std::endl); | |
(*d1 == *d3) ? (std::cout << "d1 == d3" << std::endl) : (std::cout << "d1 != d3" << std::endl); | |
(*a1 == *a2) ? (std::cout << "a1 == a2" << std::endl) : (std::cout << "a1 != a2" << std::endl); | |
(*a1 == *a3) ? (std::cout << "a1 == a3" << std::endl) : (std::cout << "a1 != a3" << std::endl); | |
std::cout << std::endl; | |
std::cout << *a1; | |
std::cout << std::endl; | |
std::cout << *d1; | |
std::cout << std::endl; | |
std::cout << *c2; | |
std::cout << std::endl; | |
a1->sleep(); | |
c2->climb(); | |
d1->bark(); | |
d1->sleep(); | |
c1->sleep(); | |
scanf("%d"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment