defineclass.cpp

来自「C++的一些简单代码 希望能给初学者带来小小的帮助」· C++ 代码 · 共 59 行

CPP
59
字号
#include <iostream>
using std::cout;
using std::endl;

class Box{
public:
	double length;
	double width;
	double height;

	//Construtor
	Box();
	Box(double lvalue,double wvalue,double hvalue):length(lvalue),width(wvalue),height(hvalue){
		cout<<"Box constructor called"<<endl;
		/*length=lengthValue;
		width=widthValue;
		height=heightValue;*/
	}

	//Function to calculate the volume of a box
	double volume(){
		return length*width*height;
	}
};

Box::Box(){
	cout<<"Default constructor called"<<endl;
	length=width=height=1.0;
}

	
void main(){
	Box firstBox(80.0,50.0,40.0);
    cout<<endl;
	cout<<"Size of first Box object is "
		<<firstBox.length<<" by "
		<<firstBox.width<<" by "
		<<firstBox.height
		<<endl;
	cout<<"Volume of first Box object is "<<firstBox.volume()
		<<endl;

	/*cout<<endl;

	Box secondBox;
	secondBox.length=10.0;
	secondBox.width=5.0;
	secondBox.height=4.0;


	cout<<endl;
	cout<<"Size of first Box object is "
		<<secondBox.length<<" by "
		<<secondBox.width<<" by "
		<<secondBox.height
		<<endl;
	cout<<"Volume of second Box object is "<<secondBox.volume()
		<<endl;*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?