题目:
乎乎学习了面向对象程序设计,他看到以下正方体类(Cube)的声明程序,请帮他实现该类。并在main函数中完成以下应用,通过输入长宽高创建一个正方体对象c1, 并用c1通过拷贝构造函数创建c2,c2的长宽高分别为c1长宽高的十分之一,然后通过指向c2的指针p调用成员函数输出c2对象的相关信息,详见输出样例。
输入:
10 20 30
输出:
Parameter constructor is called.
Copy constructor is called.
The length of c2 is 1.
The width of c2 is 2.
The heigth of c2 is 3.
The bottom area of c2 is 2.
The volumn of c2 is 6.
Deconstructor is called.
Deconstructor is called.
代码:
#include<iostream>
using namespace std;
class Cube
{
private:
int height; int width; int length;
public:
Cube(int a,int b,int c):length(a),width(b),height(c)
{cout<<"Parameter constructor is called."<<endl;
cout<<"Copy constructor is called."<<endl;}
Cube(Cube &b)
{
length=b.length/10;
width=b.width/10;
height=b.height/10;
}
~Cube(){cout<<"Deconstructor is called."<<endl;}
void get_Cube()
{
cout<<"The length of c2 is "<<length<<"."<<endl;
cout<<"The width of c2 is "<<width<<"."<<"\n";
cout<<"The heigth of c2 is "<<height<<"."<<"\n";
cout<<"The bottom area of c2 is "<<length*width<<"."<<"\n";
cout<<"The volumn of c2 is "<<length*width*height<<"."<<"\n";
}
};
int main()
{
int a,b,c;
cin>>a>>b>>c;
Cube c1(a,b,c);
Cube c2=c1;
c2.get_Cube();
}