编程语言
首页 > 编程语言> > c++ 类与对象

c++ 类与对象

作者:互联网

#include <iostream>
using namespace std;

// 定义类
class Box
{
	// public 类成员访问属性,公共成员是外部可访问的
	public:
		double length;
		double breadth;
		double height;
};

int main()
{
	// 创建对象Box1
	Box Box1;
	// 创建对象Box2
	Box Box2;
	double volume = 0.0;

	Box1.length = 1.0;
	Box1.breadth = 2.0;
	Box1.height = 3.0;
	volume = Box1.length * Box1.breadth * Box1.height;
	cout <<"Box1的体积:" << volume << endl;


	Box2.length = 4.0;
	Box2.breadth = 5.0;
	Box2.height = 6.0;
	volume = Box2.length * Box2.breadth * Box2.height;
	cout << "Box1的体积:" << volume << endl;

	system("pause");
	return 0;
}

 

标签:Box,breadth,对象,double,c++,height,length,Box1
来源: https://blog.51cto.com/u_14097531/2980802