Advertisement
Domerk

Наследование_1

Mar 17th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. //by Aksenoff
  2.  
  3. //geom.h
  4.  
  5. #ifndef MNOG_H //Директивы #ifndef, #define, #endif служат для защиты от многократного копирования
  6. #define MNOG_H
  7.  
  8. class Mnog //Класс многоугольников - в данном примере используется как класс-предок.
  9. {
  10. protected: //защищённые члены класса могут использоваться при наследовании, а приватные - нет
  11.     double shirina, visota;
  12.     Mnog(double, double);
  13. };
  14.  
  15. #endif
  16.  
  17. //=======================================
  18. //pram.h
  19.  
  20. #ifndef PRAM_H
  21. #define PRAM_H
  22.  
  23. #include "geom.h"
  24.  
  25. class Pram: public Mnog //Создаём класс прямоугольник - потомок класса многоугольников
  26. {
  27. public:
  28.     double ploshad();
  29.     Pram(double a, double b): Mnog(a, b) {};
  30. };
  31.  
  32. #endif
  33.  
  34. //=======================================
  35. //treug.h
  36.  
  37. #ifndef TREUG_H
  38. #define TREUG_H
  39.  
  40. #include "geom.h"
  41.  
  42. class Treug: public Mnog {
  43. public:
  44.     double ploshad();
  45.     Treug(double a, double b): Mnog(a, b) {};
  46. };
  47.  
  48. #endif
  49.  
  50. //=======================================
  51. //geom.cpp
  52.  
  53. #include "geom.h"
  54.  
  55. Mnog::Mnog(double x, double y)
  56. {
  57.     shirina = x;
  58.     visota = y;
  59. }
  60.  
  61. //=======================================
  62. //pram.cpp
  63.  
  64. #include "pram.h"
  65.  
  66. double Pram::ploshad()
  67. {
  68.     return shirina*visota;
  69. }
  70.  
  71.  
  72. //=======================================
  73. //treug.cpp
  74.  
  75. #include "treug.h"
  76.  
  77. double Treug::ploshad()
  78. {
  79.     return shirina*visota*0.5;
  80. }
  81.  
  82. //=======================================
  83. //main.cpp
  84.  
  85. #include "pram.h"
  86. #include "treug.h"
  87. #include "iostream"
  88.  
  89. int main()
  90. {
  91.     Treug tr1(10, 20);
  92.     std::cout << tr1.ploshad();
  93.     std::cin.sync();
  94.     std::cin.clear();
  95.     std::cin.get();
  96.     return 0;
  97. }
  98.  
  99. //========================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement