When the class operator "+" is overloaded, I create a new object in dynamic memory and immediately return it. Where and how in the program do I need to delete it?
#include "pch.h" #include <iostream> #include <cmath> #include <ctime> using namespace std; #define N 10 class Fraction { private: int chys, zn; public: Fraction() { this->chys = 0; this->zn = 0; } Fraction(int chys, int zn) { this->chys = chys; this->zn = zn; } Fraction(const Fraction &obj) { this->chys = obj.chys; this->zn = obj.zn; } int GetChys() { return chys; } int GetZn() { return zn; } void setFraction(int x, int y) { chys = x; zn = y; } int CommonZn(Fraction *B) { int q = 0; int i = 0; for (i = 1; q != 1; i++) { if ((i % this->zn == 0) && (i % B->zn == 0)) { q = 1; this->chys *= (i / this->zn); B->chys *= (i / B->zn); } } this->zn = i - 1; B->zn = i - 1; return i - 1; } int skor(int *chys, int *zn) { int i = 0, b = 0; if (*chys > *zn) { b = *chys; } else (b = *zn); for (i = b; i > 0; i--) { if (*chys%i == 0 && *zn%i == 0) { *chys = *chys / i; *zn = *zn / i; return 0; } } } void comperizon(Fraction *A, Fraction *B) { if (A->chys > B->chys) { cout << endl; cout << "Перший дріб більший за другий" << endl; } else if (A->chys == B->chys) { cout << endl; cout << "Дроби однакові" << endl; } else if (A->chys < B->chys) { cout << endl; cout << "Другий дріб більший за перший" << endl; } } void addition(Fraction *A, Fraction *B) { this->chys = A->chys + B->chys; skor(&this->chys, &this->zn); cout << "\nСума дробів = "; cout << *this; cout << endl; } void subtraction(Fraction *A, Fraction *B) { this->chys = A->chys - B->chys; skor(&this->chys, &this->zn); cout << "\nРізниця дробів = "; cout << *this; cout << endl; } void multiplication(Fraction *A, Fraction *B) { this->chys = (A->chys * B->chys); this->zn = (A->zn * B->zn); skor(&this->chys, &this->zn); cout << "\nДобуток дробів = "; cout << *this; cout << endl; } void division(Fraction *A, Fraction *B) { this->chys = (A->chys * B->zn); this->zn = (A->zn * B->chys); skor(&this->chys, &this->zn); cout << "\nЧастка дробів = "; cout << *this; cout << endl; } Fraction const& operator=(Fraction &second) { chys = second.chys; zn = second.zn; return *this; } bool operator == (Fraction &obj) { this->CommonZn(&obj); return (this->chys == obj.chys); } bool operator >= (Fraction &obj) { this->CommonZn(&obj); return (this->chys >= obj.chys); } bool operator > (Fraction &obj) { this->CommonZn(&obj); return ((double)this->chys / this->zn > (double)obj.chys / obj.zn); } bool operator < (Fraction &obj) { this->CommonZn(&obj); return ((double)this->chys / this->zn < (double)obj.chys / obj.zn); } bool operator <= (Fraction &obj) { this->CommonZn(&obj); return (this->chys <= obj.chys); } friend Fraction& operator +(Fraction &first, Fraction &second); friend istream &operator >> (istream &in, Fraction &fraction); friend ostream &operator << (ostream &out, const Fraction &fr); ~Fraction() {} }; Fraction& operator +(Fraction &first, Fraction &second) { first.CommonZn(&second); return *new Fraction(first.chys + second.chys, first.zn); } istream &operator >> (istream &in, Fraction &fraction) { in >> fraction.chys; in.ignore(1); in >> fraction.zn; return in; } ostream &operator << (ostream &out, const Fraction &fr) { out << fr.chys << "/" << fr.zn; return out; } void Sort(Fraction *FrArray[]); //double avarage(Fraction FrArray[]); int main() { system("chcp 1251"); system("color f0"); system("cls"); srand(time(nullptr)); int chys1, zn1, chys2, zn2; Fraction *PtrFrArray[N]; for (int i = 0; i < N; i++) { PtrFrArray[i] = new Fraction(1 + rand() % 10, 1 + rand() % 10); cout << *PtrFrArray[i] << endl; } Sort(PtrFrArray); cout << endl << "Сортований масив: " << endl; for (int i = 0; i < N; ++i) { cout << *PtrFrArray[i] << endl; } cout << endl; double avar = 0.0; for (int i = 0; i < N; i++) { avar += (double) (*PtrFrArray[i]).GetChys() / (*PtrFrArray[i]).GetZn(); } cout << "Середнє значення масиву = " << avar/N << endl << endl; for (int i = 0; i < N; i++) { delete PtrFrArray[i]; } Fraction *A = new Fraction(); cout << "\nПерший дріб = " << endl; cin >> *A; Fraction *B = new Fraction(); cout << "\nДругий дріб = " << endl; cin >> *B; Fraction *C = new Fraction(*A); cout << "Enter C " << endl; cin >> *C; if (*A > *C && *C < *B) cout << "Успішне порівняння" << endl; else cout << "Помилка" << endl; cout << "A+B+C = " << *A + *B + *C; delete A; delete B; delete C; getchar(); getchar(); return 0; } void Sort(Fraction *FrArray[]) { Fraction changer; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (*FrArray[i] > *FrArray[j]) { changer = *FrArray[i]; *FrArray[i] = *FrArray[j]; *FrArray[j] = changer; } } } } /*double avarage(Fraction *FrArray[]) { double avar = 0.0; for (int i = 0; i < N; i++) { avar = avar + (double)(*FrArray[i]).GetChys()/ (*FrArray[i]).GetZn(); } return(avar/N); }*/