Advertisement
avr39ripe

cppInheritanceBasics

Aug 11th, 2021
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Parent
  4. {
  5.     int val;
  6. protected:
  7.     int nums[2]{ 10,20 };
  8.     //int x{10};
  9.     //int y{20};
  10.     int protVal{555};
  11. public:
  12.     Parent() : val{ 333 } { std::cout << "Parent DEFAULT ctor\n"; }
  13.     Parent(int valP) : val{ valP } { std::cout << "Parent ctor valP\n"; }
  14.     Parent(char symbP) : val{ symbP - 'A'} { std::cout << "Parent ctor symbP\n"; }
  15.     int getVal() const { return val; }
  16.     int getX() const { return nums[0]; }
  17.     int getY() const { return nums[1]; }
  18.     virtual void info() { std::cout << "I'm Parent!\n"; }
  19.     void greeting() { std::cout << "Hello, let me introduce myself, "; info(); }
  20.     ~Parent() { std::cout << "Parent dtor\n"; }
  21. };
  22.  
  23. class Child : public Parent
  24. {
  25. // super private:
  26. //  int val;
  27. protected:
  28.     char symb;
  29. public:
  30.     Child() : symb {'A' } { std::cout << "Child DEFAULT ctor\n"; }
  31.     Child(char symbP) : Parent{symbP}, symb { symbP } { std::cout << "Child ctor symbP\n"; }
  32.     char getSymb() const { return symb; }
  33.     void childInfo()const
  34.     {
  35.         std::cout << "Child: symb = " <<
  36.         symb << " val = " <<
  37.         getVal() << " protVal = " <<
  38.         protVal << "\n x = " << getX() << " y = " << getY() << '\n';
  39.     }
  40.     void info()
  41.     {
  42.         //std::cout << "Info from Parent - > \n\n"; Parent::info();
  43.         /*std::cout << "Info from Child - > \n\n";  */std::cout << "I'm Child!\n";
  44.     }
  45.     ~Child() { std::cout << "Child dtor\n"; }
  46. };
  47.  
  48. class AParent
  49. {
  50.  
  51. };
  52.  
  53. class BParent
  54. {
  55.  
  56. };
  57.  
  58.  
  59. class child : public AParent, public BParent
  60. {
  61.  
  62. };
  63.  
  64. class GrandChild : public Child
  65. {
  66.     bool flag;
  67.     using Child::childInfo;
  68. public:
  69.     GrandChild(bool flagP) : flag{ flagP } { std::cout << "GrandChild ctor flagP\n"; }
  70.     void grandChildInfo()const { std::cout << "GrandChild: symb = " << symb << " val = " << getVal() << " protVal = " << protVal << '\n'; }
  71.     void info()
  72.     {
  73.         std::cout << "I'm GrandChild!\n";
  74.     }
  75.     ~GrandChild() { std::cout << "GrandChild dtor\n"; }
  76. };
  77.  
  78. class Date
  79. {
  80.     uint16_t year;
  81.     uint8_t month;
  82.     uint8_t day;
  83.  
  84.     uint8_t checkMonth(uint8_t monthP)
  85.     {
  86.         if (monthP < 1) return 1;
  87.         if (monthP > 12) return 12;
  88.         return monthP;
  89.     }
  90.     uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  91.     {
  92.         const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
  93.         if (dayP < 1) return 1;
  94.         if (dayP > maxDays) return maxDays;
  95.         return dayP;
  96.     }
  97. public:
  98.     static uint8_t daysInMonth(uint8_t month, uint16_t year)
  99.     {
  100.         return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
  101.     }
  102.  
  103.     static bool isLeapYear(uint16_t year)
  104.     {
  105.         return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
  106.     }
  107.     static const uint8_t maxMonth{ 12 };
  108.  
  109.     Date() : Date(1, 1, 1970) {}
  110.     Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  111.         : year{ yearP }, month{ checkMonth(monthP) }, day{ checkDay(dayP,month,yearP) } {}
  112.  
  113.     Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
  114.     Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
  115.     Date& setYear(uint16_t yearP) { year = yearP; return *this; }
  116.  
  117.     uint8_t getDay() const { return day; }
  118.     uint8_t getMonth() const { return month; }
  119.     uint16_t getYear() const { return year; }
  120.  
  121.     Date& print() { std::cout << +day << '.' << +month << '.' << year << '\n'; return *this; }
  122. };
  123.  
  124. class Human
  125. {
  126.     std::string fname;
  127.     std::string sname;
  128.     uint8_t age;
  129.     uint8_t weight;
  130.     uint8_t height;
  131. protected:
  132.     Date bDate;
  133. public:
  134.     Human() : Human("John", "Doe", 39, 42, 150, { 1,1,1981 }) {}
  135.     Human(const std::string& fnameP, const std::string& snameP, uint8_t ageP, uint8_t weightP, uint8_t heightP, const Date& bDateP)
  136.         : fname{ fnameP }, sname{ snameP }, age{ ageP }, weight{ weightP }, height{heightP}, bDate{bDateP}
  137.     { std::cout << "Human constructed\n"; }
  138.     uint8_t getAge()const { return age; }
  139.     Human& setAge(uint8_t ageP) { age = ageP; return *this; }
  140.     ~Human() { std::cout << "Human Destructed\n"; }
  141.     // further Human class implementation..
  142. };
  143.  
  144.  
  145. class Student : public Human
  146. {
  147.     std::string uName;
  148.     double avgGrade;
  149. public:
  150.     // further student impementation
  151.     Date& getStudentBDate() { return bDate; }
  152.     //uint8_t getStudentWeight() { return weight; }
  153. };
  154.  
  155.  
  156. class Employee : public Human
  157. {
  158.     std::string orgName;
  159.     float salary;
  160.     uint32_t socialId;
  161. public:
  162.     // further Employee imp.
  163. };
  164.  
  165. int main()
  166. {
  167.  //   Human h1{};
  168.  //   Student s1{};
  169.  
  170.  //   std::cout << "Human" << + h1.getAge() << '\n';
  171.  //   std::cout << "Student" <<  + s1.getAge() << '\n';
  172.  //   std::cout << "Student "; s1.getStudentBDate().print();
  173.     //return 0;
  174.  
  175.     //Parent parent;
  176.     //parent.greeting();
  177.     //parent.info();
  178.     //Child child;
  179.     //child.greeting();
  180.     //child.info();
  181.     //child.childInfo();
  182.     //child.childInfo();
  183.     GrandChild gChild{true};
  184.     gChild.greeting();
  185.     //gChild.info();
  186.     //gChild.childInfo();
  187.     //gChild.grandChildInfo();
  188.  
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement