Advertisement
Guest User

Animal 'inheritance'

a guest
Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. //animal.h
  2. #ifndef ANIMAL_H
  3. #define ANIMAL_H
  4.  
  5. #include <string>
  6. class Animal
  7. {
  8. public:
  9.     Animal(std::string animalClass_, std::string type_, unsigned age_);
  10.     std::string getAnimalClass() {return animalClass;}
  11.     std::string getType() {return type;}
  12.     unsigned getAge() {return age;}
  13.  
  14. protected:
  15.     std::string type;
  16.     std::string animalClass;
  17.     unsigned age;
  18. };
  19.  
  20. #endif // ANIMAL_H
  21.  
  22. //animal.cpp
  23. #include "animal.h"
  24.  
  25. Animal::Animal(std::string animalClass_, std::string type_, unsigned age_) :
  26.     type(type_),
  27.     animalClass(animalClass_),
  28.     age(age_)
  29. {}
  30.  
  31. //mammal.h
  32. #ifndef MAMMAL_H
  33. #define MAMMAL_H
  34. #include "animal.h"
  35.  
  36. class Mammal : public Animal
  37. {
  38. public:
  39.     Mammal(std::string type_, std::string name_, unsigned age_);
  40.     std::string getName() {return name;}
  41. protected:
  42.     std::string name;
  43. };
  44.  
  45. #endif // MAMMAL_H
  46.  
  47. //mammal.cpp
  48. #include "mammal.h"
  49.  
  50. Mammal::Mammal(std::string type_, std::string name_, unsigned age_) :
  51.     Animal("Ssaki", type_, age_),
  52.     name(name_)
  53. {}
  54.  
  55. //dog.h
  56. #ifndef DOG_H
  57. #define DOG_H
  58.  
  59. #include "mammal.h"
  60. class Dog : public Mammal
  61. {
  62. public:
  63.     Dog(std::string name_, std::string owner_, unsigned age_);
  64.     std::string getOwnerName() {return owner;}
  65. private:
  66.     std::string owner;
  67. };
  68.  
  69. #endif // DOG_H
  70.  
  71. //dog.cpp
  72. #include "dog.h"
  73.  
  74. Dog::Dog(std::string name_, std::string owner_, unsigned age_) :
  75.     Mammal ("Pies", name_, age_),
  76.     owner(owner_)
  77. {}
  78.  
  79. //main.cpp
  80. #include <iostream>
  81. #include "dog.h"
  82.  
  83. using namespace std;
  84.  
  85. int main()
  86. {
  87.     Dog leos("Leo", "Jakub Warchol", 4);
  88.  
  89.     cout << "Gromada: " << leos.getAnimalClass() << endl;
  90.     cout << "Gatunek: " << leos.getType() << endl;
  91.     cout << "Imie: " << leos.getName() << endl;
  92.     cout << "Wlasciciel: " << leos.getOwnerName() << endl;
  93.     cout << "Wiek: " << leos.getAge() << endl;
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement