YTUOJ——Shape系列-2

题目描述

小聪不喜欢小强的Shape类,声称用Shape类做出的形状不真实,于是小聪创建了Rectangle类,并且决定用该类做两个矩形出来,送给好朋友小亮。Rectangle类有整型的数据成员color(小强的Shape类中的color可以继续使用,无需新定义),浮点型的数据成员widthheight,求面积的成员函数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;
}

提示:不用提交全部程序,只提交补充部分。

输入

输出

输出小聪创建的矩形的相关数据

样例输出

Rectangle color:1
Rectangle width:2
Rectangle height:3
Rectangle area:6
Rectangle price:6

答案

#include<iostream>
using namespace std;
class Shape{
public:
    Shape();
    Shape(int c);
    int getcolor();
    double area();
protected:
    int color;
    float width;
    float height;
};
Shape::Shape(){
    color=0;
}
Shape::Shape(int c){
    color=c;
}
int Shape::getcolor(){
    return color;
}
double Shape::area(){
    return 10000;
}
//提交的部分 S
class Rectangle{
public:
    Rectangle();
    Rectangle(int c, float w, float h);
    int getcolor();
    float area();
    int getwidth();
    int getheight();
    int price();
protected:
    int color;
    float width;
    float height;
};
Rectangle::Rectangle(int c, float w, float h){
    color = c;
    width = w;
    height = h;
}
int Rectangle::getcolor(){
    return color;
}
float Rectangle::area(){
    return width * height;
}
int Rectangle::getwidth(){
    return width;
}
int Rectangle::getheight(){
    return height;
}
int Rectangle::price(){
    return 6;
}
//提交的部分 E

//小聪的测试函数:
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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值