类,对象--示例代码

#include <iostream>
using namespace std;
class Box{
	private:
		double length;
		double width;
	public:
	void setLength(double length);
	void setWidth(double width);
	double getLength();
	double getWidth();
	double getArea();	
};
void Box::setLength(double length){
	this->length=length;
	
}
void Box::setWidth(double width){
	this->width=width;
}
double Box::getArea(){
	return length*width;
}
double Box::getLength(){
	return length;
}
double Box::getWidth(){
	return width;
}
int main(){
	Box box;
	box.setLength(6.0);
	box.setWidth(7.0);
	cout<<box.getArea()<<endl;
	return 0;
}

  

posted @ 2024-01-27 09:19  fushuxuan1  阅读(6)  评论(0)    收藏  举报