#include<iostream>
using namespace std;
class Box
{
private:
float length;
float width;
float height;
public:
Box(float l,float w,float h);
Input();
Count();
};
Box::Box(float l,float w,float h)
{ length=l;
width=w;
height=h;
}
Box::Input()
{cin>>length>>width>>height;
}
Box::Count()
{
return length*width*height;
}
int main()
{ Box b1,b2,b3;
cout<<b1.Count()<<endl;
cout<<b2.Count()<<endl;
cout<<b2.Count()<<endl;
return 0;
}
1错了,哪里,给我修改一下
2主函数最后想换成用指针的怎么弄
Box b[3],*ps;
然后??
C++题目 计算3个长方体体积
答案:1 悬赏:60 手机版
解决时间 2021-02-08 21:05
- 提问者网友:ミ烙印ゝ
- 2021-02-08 13:39
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-02-08 14:58
#include<iostream>
using namespace std;
class Box
{
private:
float length;
float width;
float height;
public:
Box (float l, float w, float h);
void Input(); // 需要返回类型
float Count(); // 需要返回值
};
Box::Box (float l, float w, float h)
{
length = l;
width = w;
height = h;
}
void Box::Input() // 需要返回类型
{
cin >> length >> width >> height;
}
float Box::Count() // 需要返回值
{
return length * width * height;
}
int main()
{
Box b1(1, 2, 3); // 构造器需要参数
Box b2(2, 2, 2);
Box b3(3, 2, 1);
cout << b1.Count() << endl;
cout << b2.Count() << endl;
cout << b3.Count() << endl;
return 0;
}
// 指针对象
Box *b1 = new Box(1, 2, 3);
Box *b2 = new Box(2, 2, 2);
Box *b3 = new Box(3, 2, 1);
cout << b1->Count() << endl;
cout << b2->Count() << endl;
cout << b3->Count() << endl;
delete b1;
delete b2;
delete b3;
using namespace std;
class Box
{
private:
float length;
float width;
float height;
public:
Box (float l, float w, float h);
void Input(); // 需要返回类型
float Count(); // 需要返回值
};
Box::Box (float l, float w, float h)
{
length = l;
width = w;
height = h;
}
void Box::Input() // 需要返回类型
{
cin >> length >> width >> height;
}
float Box::Count() // 需要返回值
{
return length * width * height;
}
int main()
{
Box b1(1, 2, 3); // 构造器需要参数
Box b2(2, 2, 2);
Box b3(3, 2, 1);
cout << b1.Count() << endl;
cout << b2.Count() << endl;
cout << b3.Count() << endl;
return 0;
}
// 指针对象
Box *b1 = new Box(1, 2, 3);
Box *b2 = new Box(2, 2, 2);
Box *b3 = new Box(3, 2, 1);
cout << b1->Count() << endl;
cout << b2->Count() << endl;
cout << b3->Count() << endl;
delete b1;
delete b2;
delete b3;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯