#include <iostream>
using namespace std;
struct Box
{
public:
Box(int,int,int); //声明带参数的构造函数
int volume();
private:
int length;
int width;
int height;
};
Box::Box(int len,int w,int h)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return (length*width*height);
}
int main()
{
Box b1(10,10,10);
cout<<"volume="<<b1.volume()<<endl;
return 0;
}
using namespace std;
struct Box
{
public:
Box(int,int,int); //声明带参数的构造函数
int volume();
private:
int length;
int width;
int height;
};
Box::Box(int len,int w,int h)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return (length*width*height);
}
int main()
{
Box b1(10,10,10);
cout<<"volume="<<b1.volume()<<endl;
return 0;
}
本文通过一个 C++ 的结构体示例介绍了如何定义结构体、构造函数及成员方法来计算体积。示例中创建了一个 Box 结构体,包含长度、宽度和高度三个属性,并实现了计算体积的方法。
6712

被折叠的 条评论
为什么被折叠?



