2774 - 圆桌类(多继承)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

设计一个圆类和一个桌子类,另设计一个圆桌类roundtable,
它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。
其中,类包含半径和求圆面积的成员函数getarea();
桌子类包含桌子高度和返回高度的成员函数getheight()。
roundtable类继承所有上述类的数据成员和成员函数,
添加了数据成员桌子颜色和相应的成员函数getcolor()。部分代码如下:

void display( roundtable &s)//显示圆桌数据
      {
          cout.precision(2);
 cout.setf(ios::fixed);
 cout<<"圆桌的面积:"<<s.getarea()<<",";
 cout<<"高度:"<<s.getheight()<<",";
          cout<<"颜色:"<<s.getcolor()<<"。"<<endl;
      }
int main()
{   float r,h;
    char color[10];
   while( cin>>r>>h>>color)
   {roundtable   rt(color,h,r);
   display(rt);}
     return 0;
}

题目输入

输入若干组数据,每组数据包含圆桌的半径,高度,颜色。

题目输出

对应输出圆桌的面积,高度,颜色。∏取3.14。结果保留两位小数。
注意:标点符号为中文标点。

输入/输出样例

输入格式

1.2 0.8 黄色
1.3 0.75 白色

输出格式

圆桌的面积:4.52,高度:0.80,颜色:黄色。
圆桌的面积:5.31,高度:0.75,颜色:白色。

C++解答

#include<iostream>
#include<string.h>
using namespace std;

class circle
{ 
protected:
    float r;
public:
    circle(float r0=0)
    { r=r0;}
       double getarea( )
    {return 3.14*r*r;}
   };

class table
{
protected:
    float high;
public:
    table(float high1=0)
    {high=high1;}
    float getheight( )
    {return high;}
    };

class roundtable:public circle,public table
{
private:
      string color;
public:
      roundtable(string color1,float high,float r)
          
          :table(high),circle(r)
      { 
        
        color=color1;
      }
 

     string  getcolor( )
      {return color;}
            
    
};
  
void display( roundtable &s)
      {
          cout.precision(2);
		  cout.setf(ios::fixed);
		  cout<<"圆桌的面积:"<<s.getarea()<<",";
		  cout<<"高度:"<<s.getheight()<<",";
          cout<<"颜色:"<<s.getcolor()<<"。"<<endl;
      }
int main()
{   float r,h;
    char color[10];
   while( cin>>r>>h>>color)
   {roundtable   rt(color,h,r);
   display(rt);}
   return 0; 
}