[YTU]_2916(Shape系列-2)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2916(Shape系列-2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
小聰不喜歡小強的Shape類,聲稱用Shape類做出的形狀不真實,于是小聰創建了Rectangle類,并且決定用該類做兩個矩形出來,送給好朋友小亮。Rectangle類有整型的數據成員color(小強的Shape類中的color可以繼續使用,無需新定義),浮點型的數據成員width和height,求面積的成員函數area()。但是小聰沒有為Rectangle類寫構造函數和成員函數,請幫助小聰完成Rectangle類。
小強寫的文件頭和Shape類:#include<iostream>
using namespace std;
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() { Rectangle rr=Rectangle(1,2,3); cout<<"Rectangle color:"<<rr.getcolor()<<endl <<"Rectangle width:"<<rr.getwidth()<<endl <<"Rectangle height:"<<rr.getheight()<<endl <<"Rectangle area:"<<rr.area()<<endl <<"Rectangle price:"<<rr.price()<<endl; return 0; } 提示:不用提交全部程序,只提交補充部分。
Input
無
Output
輸出小聰創建的矩形的相關數據。
Sample Output
Rectangle color:1Rectangle width:2Rectangle height:3Rectangle area:6Rectangle price:6#include<iostream> using namespace std; 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; } class Rectangle:public Shape { public:Rectangle(){}Rectangle(int c,int w,int h):Shape(c),width(w),height(h){}int getwidth();int getheight();int area();int price(); private:int width,height; };int Rectangle::getwidth() {return width;} int Rectangle::getheight() {return height;} int Rectangle::area() {return width*height;} int Rectangle::price() {return width+height+color;} int main(){Rectangle rr=Rectangle(1,2,3);cout<<"Rectangle color:"<<rr.getcolor()<<endl<<"Rectangle width:"<<rr.getwidth()<<endl<<"Rectangle height:"<<rr.getheight()<<endl<<"Rectangle area:"<<rr.area()<<endl<<"Rectangle price:"<<rr.price()<<endl;return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2916(Shape系列-2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2915(Shape系列-1
- 下一篇: [YTU]_2917(Shape系列-3