OJ 2212 Problem A 杯子
Description
定义一个Cup类,包含一个int类型的属性volume,表明杯子的容量。定义其构造函数、析构函数和拷贝构造函数,分别产生类似样例所示的输出。定义一个setVolume(int)函数,用于设置杯子的新容量。
Input
两个非负整数。
Output
见样例。
Sample Input
400
500
Sample Output
A cup of 0 ml is created.
A cup of 400 ml is created.
A cup of 400 ml is copied.
A cup of 500 ml is erased.
A cup of 400 ml is erased.
A cup of 0 ml is erased.
HINT
Append Code
int main()
{Cup c1;int i, j;cin>>i>>j;Cup c2(i), c3(c2);c3.setVolume(j);return 0;
}
Accepted Code
#include using namespace std;class Cup {int volume;
public:Cup(int v = 0) : volume(v) {cout << "A cup of " << volume << " ml is created.\n";}Cup(Cup& c) {volume = c.volume;cout << "A cup of " << volume << " ml is copied.\n";}~Cup() {cout << "A cup of " << volume << " ml is erased.\n";}Cup& setVolume(int v) {volume = v;return *this;}
};
int main()
{Cup c1;int i, j;cin>>i>>j;Cup c2(i), c3(c2);c3.setVolume(j);return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
