shiti2_4_2.cpp

来自「为初学者提供的最佳的C++程序设计源程序库」· C++ 代码 · 共 48 行

CPP
48
字号
#include<iostream.h>
class Cube
{
public:
	Cube()
	{
		length=width=high=1;
		cout<<"Cube's default constructor."<<endl;
	}
	Cube(int x,int y,int z)
	{
		length=x;
		width=y;
		high=z;
		cout<<"Cube's constructor."<<endl;
	}
	Cube(Cube& obj)
	{
		length=obj.length;
		width=obj.width;
		high=obj.high;
		cout<<"Cube's copy constructor."<<endl;
	}
	Cube& operator=(const Cube& obj)
	{
		length=obj.length;
		width=obj.width;
		high=obj.high;
		cout<<"Cube's assignment operator."<<endl;
		return *this;
	}
	int Surface(){return 2*(length*width+length*high+width*high);}
	int Volume(){return length*width*high;}
private:
	int length,width,high;
};
void main()
{
	Cube c1;
	cout<<"cube 1:surface="<<c1.Surface()<<"volume="<<c1.Volume()<<endl;
	Cube c2(10,10,10);
	cout<<"cube 2:surface="<<c2.Surface()<<"volume="<<c2.Volume()<<endl;
  c1=c2;
	cout<<"cube 1(2):surface="<<c1.Surface()<<"volume="<<c2.Volume()<<endl;
  Cube c3(c1);
  cout<<"cube 3(1):surface="<<c3.Surface()<<"volume="<<c3.Volume()<<endl;
}

⌨️ 快捷键说明

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