[YTU]_2916(Shape系列-2)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2916(Shape系列-2)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
小聰不喜歡小強(qiáng)的Shape類,聲稱用Shape類做出的形狀不真實(shí),于是小聰創(chuàng)建了Rectangle類,并且決定用該類做兩個(gè)矩形出來(lái),送給好朋友小亮。Rectangle類有整型的數(shù)據(jù)成員color(小強(qiáng)的Shape類中的color可以繼續(xù)使用,無(wú)需新定義),浮點(diǎn)型的數(shù)據(jù)成員width和height,求面積的成員函數(shù)area()。但是小聰沒(méi)有為Rectangle類寫(xiě)構(gòu)造函數(shù)和成員函數(shù),請(qǐng)幫助小聰完成Rectangle類。
小強(qiáng)寫(xiě)的文件頭和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;
}
小聰?shù)臏y(cè)試函數(shù): 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; } 提示:不用提交全部程序,只提交補(bǔ)充部分。
Input
無(wú)
Output
輸出小聰創(chuàng)建的矩形的相關(guān)數(shù)據(jù)。
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; }總結(jié)
以上是生活随笔為你收集整理的[YTU]_2916(Shape系列-2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [YTU]_2915(Shape系列-1
- 下一篇: [YTU]_2917(Shape系列-3