inheritance.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. class Human
  5. {
  6. protected:
  7. string name;
  8. int age;
  9. public:
  10. Human(string n, int a) : name(n), age(a)
  11. {
  12. cout << "ctor Human" << endl;
  13. }
  14. string getName() const {
  15. return name;
  16. }
  17. int gatAge() const {
  18. return age;
  19. }
  20. };
  21. class Employee : public Human
  22. {
  23. friend void applyTax(Employee &employee);
  24. private:
  25. double wage;
  26. public:
  27. Employee(string n, int a, double w) : wage(w), Human(n, a)
  28. {
  29. cout << "ctor Employee" << endl;
  30. }
  31. double getWage() const
  32. {
  33. return wage;
  34. }
  35. void print() const
  36. {
  37. cout << name << " " << age << " " << wage << endl;
  38. }
  39. };
  40. // Дружественная функция для доступа к приватным членам
  41. void applyTax(Employee &employee)
  42. {
  43. employee.wage *= 0.87;
  44. }
  45. // Приватный тип наследования. Можно записать как: class pEmployee : private Human
  46. class pEmployee : private Human
  47. {
  48. private:
  49. double wage;
  50. public:
  51. pEmployee(string n, int a, double w) : wage(w), Human(n, a)
  52. {
  53. cout << "ctor Employee" << endl;
  54. }
  55. };
  56. // --------------------------------------------------------------------------------- //
  57. class Shape
  58. {
  59. public:
  60. virtual void show() = 0;
  61. };
  62. class Circle : public Shape
  63. {
  64. public:
  65. void show() {
  66. cout << "Circle" << endl;
  67. }
  68. };
  69. class Triangle : Shape {
  70. public:
  71. void show() {
  72. cout << "Triangle" << endl;
  73. }
  74. };
  75. // --------------------------------------------------------------------------------- //
  76. class D {
  77. public:
  78. D() {cout << "D";}
  79. virtual ~D() {cout << "~D";}
  80. };
  81. class A : public D {
  82. public:
  83. A() {cout << "A";}
  84. ~A() {cout << "~A";}
  85. };
  86. class C : public D {
  87. public:
  88. C() {cout << "C";}
  89. virtual ~C() {cout << "~C";}
  90. };
  91. class B : public A,C {
  92. public:
  93. B() {cout << "B";}
  94. ~B() {cout << "~B";}
  95. };
  96. // --------------------------------------------------------------------------------- //
  97. enum sex_t {male, female};
  98. class Person {
  99. private:
  100. string name;
  101. int age;
  102. sex_t sex;
  103. float weight;
  104. public:
  105. Person(string n, int a, sex_t s, float w) : name(n), age(a), sex(s), weight(w) {}
  106. void setName(string n) {
  107. name = n;
  108. }
  109. };
  110. class Student : public Person {
  111. };
  112. int main()
  113. {
  114. Employee worker("Ivan", 20, 100000);
  115. applyTax(worker);
  116. worker.print();
  117. // pEmployee worker2("Sergey", 20, 120000);
  118. // worker2.print();
  119. Shape* shape1;
  120. shape1 = new Circle();
  121. shape1->show();
  122. A* b = new B();
  123. delete b;
  124. return 0;
  125. }