[YTU]_2917(Shape系列-3)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2917(Shape系列-3)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
送給小亮的Rectangle類已完成,送給小華Circle類還沒有完成。Circle類有整型的數據成員color(小強的Shape類中的color可以繼續使用,無需新定義),浮點型的數據成員radius,求面積的成員函數area()。但是小聰沒有為Circle類寫構造函數和成員函數,請幫助小聰完成Circle類。
小強寫的Shape類:
class Shape
{
public:
?Shape();
?Shape(int c);
?int getcolor();
?double area();
protected:
?int color;
};
Shape::Shape()
{
?color=0;
}
Shape::Shape(int c)
{
?color=c;
}
int Shape::getcolor()
{
?return color;
}
double Shape::area()
{
?return 10000;
}
小聰的測試函數:
int main()
{
Circle cc=Circle(3,1);
cout<<"Circle color:"<<cc.getcolor()<<endl
<< "Circle radius:"<<cc.getradius()<<endl
<< "Circle area:"<<cc.area()<<endl
<< "Circle price:"<<cc.price()<<endl;
return 0;
}
Input
無
Output
輸出小聰測試的Circle類的各個數據。
Sample Output
Circle color:3 Circle radius:1 Circle area:3.14 Circle price:9.42class Shape { public: Shape();Shape(int c);int getcolor();double area(); protected:int color; }; Shape::Shape() {color=0; } Shape::Shape(int c) {color=c; } int Shape::getcolor() {return color; } double Shape::area() {return 10000; } #include <iostream> #define PI 3.14 using namespace std; class Circle:public Shape { public:Circle(int c,float r):Shape(c),radius(r){}int getradius();float area();float price(); private:int radius; }; int Circle::getradius() {return radius;} float Circle::area() {return radius*radius*PI;} float Circle::price() {return color*PI;} int main() { Circle cc=Circle(3,1); cout<<"Circle color:"<<cc.getcolor()<<endl <<"Circle radius:"<<cc.getradius()<<endl <<"Circle area:"<<cc.area()<<endl <<"Circle price:"<<cc.price()<<endl; return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2917(Shape系列-3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2916(Shape系列-2
- 下一篇: [YTU]_2918( Shape系列-