Guest User

Untitled

a guest
Sep 18th, 2023
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 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.   Cat(const Cat &);
  30.   void print2();
  31.  
  32. private:
  33.   int body_length;
  34.   int tail_length;
  35. };
  36.  
  37. Cat::Cat() : Animal("n", "t", 0), body_length(0), tail_length(0) {
  38.   cout << "calling Cat no argument constructor..." << endl;
  39. }
  40.  
  41. Cat::Cat(string n, string t, int w, int b, int a)
  42.     : Animal(n, t, w), body_length(b), tail_length(a) {
  43.   cout << "calling Cat argument constructor..." << endl;
  44. }
  45.  
  46. void Cat::print2() {
  47.   print1();
  48.   cout << " body length:" << body_length << " tail length:" << tail_length;
  49. }
  50.  
  51. class Human : public Animal {
  52. public:
  53.   Human(string, string, int, int, string);
  54.   void print3();
  55.   Cat pet;
  56.  
  57. protected:
  58.   int height;
  59. };
  60.  
  61. Human::Human(string n, string t, int w, int h, string p)
  62.     : Animal(n, t, w), height(h) {
  63.   cout << "calling Human argument constructor..." << endl;
  64.   pet = Cat(p, "black", 7, 30, 20);
  65. }
  66.  
  67. void Human::print3() {
  68.   print1();
  69.   cout << " height:" << height << " pet:" << pet.name << endl;
  70. }
  71.  
  72. int main() {
  73.   Human Betty("Betty", "Asian", 46, 160, "Kitty");
  74.   Betty.print3();
  75.   Betty.pet.print2();
  76.   return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment