#include<iostream>
#include<cmath>
using namespace std;
class Carlo
{
private:
double weight;
double price;
static double all_weight;
static double all_price;
public:
Carlo(double w, double p);
~Carlo();
void printCarlo();
static void printAllCarlo();
};
double Carlo::all_weight = 0;
double Carlo::all_price = 0;
Carlo::Carlo(double w, double p)
{
weight = w;
price = p;
all_weight += weight;
all_price += price;
}
Carlo::~Carlo()
{
all_weight -= weight;
all_price -= price;
}
void Carlo::printCarlo()
{
cout << weight << "kg " << price << "yuan" << endl;
}
void Carlo::printAllCarlo()
{
if(fabs(all_weight)<1e-8&& fabs(all_price) < 1e-8)
cout << 0 << "kg " << 0 << "yuan" << endl;
else
cout << all_weight << "kg " << all_price << "yuan" << endl;
}
int main()
{
Carlo* g1 = new Carlo[3]{ Carlo(3.2,5.8),Carlo(6.6,3.52),Carlo(7.1,3.79) };
for (int i = 0; i < 3; i++)
{
g1[i].printCarlo();
}
Carlo::printAllCarlo();
Carlo* g2 = new Carlo(9, 8);
g2[0].printCarlo();
Carlo::printAllCarlo();
delete[]g1;
Carlo::printAllCarlo();
delete[]g2;
Carlo::printAllCarlo();
}
