[YTU]_2915(Shape系列-1)
Description
小強開始迷戀彩色的Shape,于是決定做一個Shape類。Shape類有整型的數據成員color,求面積的成員函數area()。小強針對不知道千奇百怪的Shape如何求面積,于是就統一Shape的面積為10000。小聰看見小強做好的Shape類,決定用該類做兩個形狀出來,并測試其顏色和面積。但是小強沒有為Shape類寫構造函數和成員函數,請幫助小強完成Shape類。
小強寫的文件頭和Shape類的C++代碼如下
#include<iostream>
using namespace std;
class Shape
{
public:
Shape();
Shape(int c);
int getcolor();
double area();
private:
int color;
};
小聰測試的C++代碼如下
int main()
{
Shape ss1=Shape();
Shape ss2=Shape(5);
cout<<"Shape color:"<<ss1.getcolor()<<","
<< "Shape area:"<<ss1.area()<<endl;
cout<<"Shape color:"<<ss2.getcolor()<<","
<< "Shape area:"<<ss2.area()<<endl;
return 0;
}
提示:小強和小聰已經寫好的部分不用提交了,只提交補充部分就可以了。
Input
無
Output
輸出小聰測試的兩個Shape的顏色和面積。
Sample Input
無Sample Output
Shape color:0,Shape area:10000 Shape color:5,Shape area:10000#include<iostream> using namespace std; class Shape { public: Shape();Shape(int c);int getcolor();double area(); private:int color; }; Shape::Shape(){color=0;} Shape::Shape(int c){color=c;} int Shape::getcolor(){return color;} double Shape::area(){return 10000;} int main() {Shape ss1=Shape();Shape ss2=Shape(5);cout<<"Shape color:"<<ss1.getcolor()<<","<<"Shape area:"<<ss1.area()<<endl;cout<<"Shape color:"<<ss2.getcolor()<<","<<"Shape area:"<<ss2.area()<<endl;return 0;}總結
以上是生活随笔為你收集整理的[YTU]_2915(Shape系列-1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2638(编程题:多态--动
- 下一篇: [YTU]_2916(Shape系列-2