Guest User

Untitled

a guest
Sep 18th, 2023
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Animal {
  5. public:
  6.   string name;
  7.   Animal(string, string, int);
  8.   void print1();
  9.  
  10. protected:
  11.   string type;
  12.   int weight;
  13. };
  14.  
  15. Animal::Animal(string n, string t, int w) {
  16.   name = n;
  17.   type = t;
  18.   weight = w;
  19. }
  20.  
  21. void Animal::print1() {
  22.   cout << "name:" << name << " type:" << type << " weight:" << weight;
  23. }
  24.  
  25. class Cat : public Animal {
  26. public:
  27.   Cat();
  28.   Cat(string, string, int, int, int);
  29.   void print2();
  30.  
  31. private:
  32.   int body_length;
  33.   int tail_length;
  34. };
  35.  
  36. Cat::Cat() : Animal("n", "t", 0), body_length(0), tail_length(0) {
  37.   cout << "no argument constructor" << endl;
  38. }
  39. Cat::Cat(string n, string t, int w, int b, int a)
  40.     : Animal(n, t, w), body_length(b), tail_length(a) {
  41.   cout << "argument constructor" << endl;
  42. }
  43.  
  44. void Cat::print2() {
  45.   print1();
  46.   cout << " body length:" << body_length << " tail length:" << tail_length;
  47. }
  48.  
  49. class Human : public Animal {
  50. public:
  51.   Human(string, string, int, int, string);
  52.   void print3();
  53.   Cat pet;
  54.  
  55. protected:
  56.   int height;
  57. };
  58.  
  59. Human::Human(string n, string t, int w, int h, string p)
  60.     : Animal(n, t, w), height(h) {
  61.   pet = Cat(p, "black", 7, 30, 20);
  62. }
  63.  
  64. void Human::print3() {
  65.   print1();
  66.   cout << " height:" << height << " pet:" << pet.name << endl;
  67. }
  68.  
  69. int main() {
  70.   Human Betty("Betty", "Asian", 46, 160, "Kitty");
  71.   Betty.print3();
  72.   Betty.pet.print2();
  73.   return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment