#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();
}
为什么会发生访问冲突
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- ewbie 2023-03-14 09:44关注
测试过了,
delete[]g2;
错了,改为delete g2;
,因为你是Carlo* g2 = new Carlo(9, 8);
,new单个的本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 matlab中使用gurobi时报错
- ¥15 WPF 大屏看板表格背景图片设置
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂