Как сделать сортировку по полю ?И как сделать деструктор для данного класса?
#include <iostream>
#include <string>
using namespace std;
#pragma warning(disable: 4996)
class Computer
{
string nazwanie;
int model;
int price;
public:
friend bool operator> (const Computer& d1, const Computer& d2);
friend bool operator<= (const Computer& d1, const Computer& d2);
friend bool operator< (const Computer& d1, const Computer& d2);
friend bool operator>= (const Computer& d1, const Computer& d2);
Computer()
{
nazwanie;
model = 0;
price = 0;
}
Computer(string nazwanie, int model, int price)
{
this->nazwanie = nazwanie;
this->model = model;
this->price = price;
}
~Computer()
{
}
void vivod()
{
cout <<"Name of computer: "<< nazwanie<< ".Model number:"<< model << ".Price in $ : " << price << endl;
}
};
bool operator> (const Computer& d1, const Computer& d2)
{
return d1.price > d2.price;
}
bool operator>= (const Computer& d1, const Computer& d2)
{
return d1.price >= d2.price;
}
bool operator< (const Computer& d1, const Computer& d2)
{
return d1.price < d2.price;
}
bool operator<= (const Computer& d1, const Computer& d2)
{
return d1.price <= d2.price;
}
int main()
{
Computer p1("Dell", 349, 1024);
Computer p2("Asus", 1231, 2000);
Computer p2("Samsung", 1080, 1500);
p1.vivod();
p2.vivod();
if (p1 < p2)
cout << "Asus is more expensive then Dell"<<endl;
return 0;
}
Ответы (1 шт):
Автор решения: user13964273
→ Ссылка
Для сортировки достаточно вызвать функцию std::sort().
Деструктор этому классу не нужен.