Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //заголовочный файл
- #include <iostream>
- using namespace std;
- //не стал дублировать комментарии с заголовочного файла "complex.h", почти тоже самое, только добавлено новое приватное поле
- class Volume {
- private:
- double width;
- double length;
- double height;
- public:
- Volume(double w = 5, double l = 3, double h = 2) { width = w, length = l, height = h; }
- Volume(Volume &a) : width(a.width), length(a.length), height(a.height) {}
- ~Volume() {}
- double GetWidth() {
- return width;
- }
- double GetLendth() {
- return length;
- }
- double GetHeight() {
- return height;
- }
- void SetVolume(double a, double b, double c) {
- width = a;
- length = b;
- height = c;
- }
- friend Volume operator+(Volume z1, Volume z2);//перегрузка оператора сложения
- friend Volume operator-(Volume z1, Volume z2);//перегрузка оператора вычитания
- friend Volume operator*(Volume z1, Volume z2);//перегрузка оператора умножения
- friend ostream& operator << (ostream &fo, Volume &fp);//перегрузка оператора вывода
- };
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- // cpp файл
- #include <iostream>
- #include "Volume.h"
- Volume operator+(Volume a, Volume b) {
- return Volume(a.width + b.width, a.height + b.height, a.length + b.length);
- }
- Volume operator-(Volume a, Volume b) {
- if (a.width - b.width >= 0 && a.height - b.height >= 0 && a.length - b.length >= 0)
- return Volume(a.width - b.width, a.height - b.height, a.length - b.length);
- else cout << "отрицательные значения";
- }
- Volume operator*(Volume a, Volume b) {
- return Volume(a.width * b.width, a.height * b.height, a.length * b.length);
- }
- ostream &operator<< (ostream &fo, Volume &fp)
- {
- //cout стал параметром fo в нашей функции перегрузки,
- //который станет ссылкой на cout при вызове этого оператора
- fo << "ширина = " << fp.width << endl << "длина = " << fp.length << endl <<"высота = " << fp.height << endl;
- return fo;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- //main
- #include <iostream>
- #include "volume.h"
- int main()
- {
- setlocale(LC_ALL, "rus");
- Volume d1, d;
- Volume dv2(3, 2, 2);
- d = operator+(d1, dv2);
- cout << d;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement