Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. //заголовочный файл
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. //не стал дублировать комментарии с заголовочного файла "complex.h", почти тоже самое, только добавлено новое приватное поле
  6. class Volume {
  7. private:
  8. double width;
  9. double length;
  10. double height;
  11. public:
  12. Volume(double w = 5, double l = 3, double h = 2) { width = w, length = l, height = h; }
  13. Volume(Volume &a) : width(a.width), length(a.length), height(a.height) {}
  14. ~Volume() {}
  15.  
  16. double GetWidth() {
  17. return width;
  18. }
  19. double GetLendth() {
  20. return length;
  21. }
  22. double GetHeight() {
  23. return height;
  24. }
  25. void SetVolume(double a, double b, double c) {
  26. width = a;
  27. length = b;
  28. height = c;
  29. }
  30.  
  31. friend Volume operator+(Volume z1, Volume z2);//перегрузка оператора сложения
  32. friend Volume operator-(Volume z1, Volume z2);//перегрузка оператора вычитания
  33. friend Volume operator*(Volume z1, Volume z2);//перегрузка оператора умножения
  34. friend ostream& operator << (ostream &fo, Volume &fp);//перегрузка оператора вывода
  35.  
  36. };
  37. //////////////////////////////////////////////////////////////////////////////////
  38. //////////////////////////////////////////////////////////////////////////////////
  39. //////////////////////////////////////////////////////////////////////////////////
  40. //////////////////////////////////////////////////////////////////////////////////
  41. // cpp файл
  42. #include <iostream>
  43. #include "Volume.h"
  44. Volume operator+(Volume a, Volume b) {
  45. return Volume(a.width + b.width, a.height + b.height, a.length + b.length);
  46. }
  47.  
  48. Volume operator-(Volume a, Volume b) {
  49.  
  50. if (a.width - b.width >= 0 && a.height - b.height >= 0 && a.length - b.length >= 0)
  51. return Volume(a.width - b.width, a.height - b.height, a.length - b.length);
  52. else cout << "отрицательные значения";
  53. }
  54.  
  55. Volume operator*(Volume a, Volume b) {
  56. return Volume(a.width * b.width, a.height * b.height, a.length * b.length);
  57. }
  58. ostream &operator<< (ostream &fo, Volume &fp)
  59. {
  60. //cout стал параметром fo в нашей функции перегрузки,
  61. //который станет ссылкой на cout при вызове этого оператора
  62. fo << "ширина = " << fp.width << endl << "длина = " << fp.length << endl <<"высота = " << fp.height << endl;
  63.  
  64. return fo;
  65. }
  66. //////////////////////////////////////////////////////////////////////////////////
  67. //////////////////////////////////////////////////////////////////////////////////
  68. //////////////////////////////////////////////////////////////////////////////////
  69. //////////////////////////////////////////////////////////////////////////////////
  70.  
  71. //main
  72. #include <iostream>
  73. #include "volume.h"
  74.  
  75.  
  76.  
  77. int main()
  78. {
  79. setlocale(LC_ALL, "rus");
  80.  
  81. Volume d1, d;
  82. Volume dv2(3, 2, 2);
  83. d = operator+(d1, dv2);
  84. cout << d;
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement