2921: Shape系列-7

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

提交: 156  解决: 129

题目描述

小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函数的问题,请大家根据题意,帮助小强完成改动后的Shape类。
小强写的各种类
class Rectangle:public Shape 



public: 

 Rectangle(int c,double w,double h); 

 double getwidth(); 

 double getheight(); 

 double area(); 

 double price(); 

protected: 

 double height; 

 double width; 

}; 

Rectangle::Rectangle(int c,double w,double h):Shape(c) 



 width=w; 

 height=h; 



double Rectangle::getwidth() 



 return width; 



double Rectangle::getheight() 



 return height; 



double Rectangle::area() 



 return height*width; 



double Rectangle::price() 



 return height*width*color; 

}   

class Circle:public Shape 



public: 

 Circle(int c,double r); 

 double getradius(); 

 double area(); 

 double price(); 

protected: 

 double radius; 

};   

Circle::Circle(int c,double r):Shape(c) 



 radius=r; 



double Circle::getradius() 



 return radius; 



double Circle::area() 



 return PI*radius*radius; 



double Circle::price() 



 return PI*radius*radius*color; 

}

class Triangle:  public Shape

{

public:

 Triangle(int c,double b,double h);

 double area();

protected:

 double base,height;

};

Triangle::Triangle(int c,double b,double h):Shape(c)

{

 base=b;height=h;

}

double Triangle::area()

{

 return 0.5*base*height;

}

int main() 



 Shape *pt[3];

 Rectangle rr(1,1,1);

 Circle cc(2,1);

 Triangle tt(2,1,3);

 pt[0]=&rr;

 pt[1]=&cc;

 pt[2]=&tt;

 cout<<"Rectangle area:"<<pt[0]->area()<<endl;

 cout<<"Circle area:"<<pt[1]->area()<<endl;

 cout<<"Triangle area:"<<pt[2]->area()<<endl;

return 0;  

}
提示:不用提交全部程序,只提交补充部分(包括头文件和π的定义)。

输入

输出

小强测试的各个类面积的数据

样例输出

Rectangle area:1
Circle area:3.14
Triangle area:1.5

im0qianqian_站在回忆的河边看着摇晃的渡船终年无声地摆渡,它们就这样安静地画下黄昏画下清晨......

#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
Shape(int c):color(c) {}
virtual double area()=0;
int color;
};
class Rectangle:public Shape
{
public:
Rectangle(int c,double w,double h);
double getwidth();
double getheight();
double area();
double price();
protected:
double height;
double width;
};
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
width=w;
height=h;
}
double Rectangle::getwidth()
{
return width;
}
double Rectangle::getheight()
{
return height;
}
double Rectangle::area()
{
return height*width;
}
double Rectangle::price()
{
return height*width*color;
}
class Circle:public Shape
{
public:
Circle(int c,double r);
double getradius();
double area();
double price();
protected:
double radius;
};
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
double Circle::getradius()
{
return radius;
}
double Circle::area()
{
return PI*radius*radius;
}
double Circle::price()
{
return PI*radius*radius*color;
}
class Triangle: public Shape
{
public:
Triangle(int c,double b,double h);
double area();
protected:
double base,height;
};
Triangle::Triangle(int c,double b,double h):Shape(c)
{
base=b;
height=h;
}
double Triangle::area()
{
return 0.5*base*height;
}
int main()
{
Shape *pt[3];
Rectangle rr(1,1,1);
Circle cc(2,1);
Triangle tt(2,1,3);
pt[0]=&rr;
pt[1]=&cc;
pt[2]=&tt;
cout<<"Rectangle area:"<<pt[0]->area()<<endl;
cout<<"Circle area:"<<pt[1]->area()<<endl;
cout<<"Triangle area:"<<pt[2]->area()<<endl;
return 0;
}

YTU 2920: Shape系列-7的更多相关文章

  1. YTU 2922: Shape系列-8

    2922: Shape系列-8 时间限制: 1 Sec  内存限制: 128 MB 提交: 172  解决: 99 题目描述 小聪又想借用小强的Shape类了,但是不巧的是小强去考英语四级去了,但是小 ...

  2. YTU 2918: Shape系列-4

    2918: Shape系列-4 时间限制: 1 Sec  内存限制: 128 MB 提交: 276  解决: 232 题目描述 小聪送给小亮和小华的形状他们都很喜欢,小亮和小华非要比一下他们两个的形状 ...

  3. YTU 2918: Shape系列-5

    2919: Shape系列-5 时间限制: 1 Sec  内存限制: 128 MB 提交: 251  解决: 199 题目描述 JC和Kitty听说小亮和小华有了Rectangle和Circle并用R ...

  4. YTU 2917: Shape系列-3

    2917: Shape系列-3 时间限制: 1 Sec  内存限制: 128 MB 提交: 372  解决: 237 题目描述 送给小亮的Rectangle类已完成,送给小华Circle类还没有完成. ...

  5. YTU 2916: Shape系列-2

    2916: Shape系列-2 时间限制: 1 Sec  内存限制: 128 MB 提交: 268  解决: 242 题目描述 小聪不喜欢小强的Shape类,声称用Shape类做出的形状不真实,于是小 ...

  6. YTU 2915: Shape系列-1

    2915: Shape系列-1 时间限制: 1 Sec  内存限制: 128 MB 提交: 283  解决: 221 题目描述 小强开始迷恋彩色的Shape,于是决定做一个Shape类.Shape类有 ...

  7. WPF 2D图形 Shape入门(一)--Shape

    本文是篇WPF Shape的入门文章 Shape 首先看看shape的继承链关系: 一个Shape具有哪些重要属性: 属性 说明 DefiningGeometry 默认的几何形状 RenderedGe ...

  8. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数013,shape模型

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数013,shape模型 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“pr ...

  9. Android系列:res之shape制作

    大家好,pls call me francis. nice to me you. 本文将介绍使用在Android中使用shape标签绘制drawable资源图片. 下面的代码是shap标签的基本使用情 ...

随机推荐

  1. jmeter-如何进行参数化-循环读取参数

    在进行测试的时候,测试数据是一项重要的准备工作,每次迭代的数据当不一样的时候,需要进行参数化,从参数化的文件中来读取测试数据. 本经验主要介绍的是用Csv Data配置元件来进行参数化. 方法/步骤 ...

  2. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  3. zoj 2932 The Seven Percent Solution

    The Seven Percent Solution Time Limit: 2 Seconds      Memory Limit: 65536 KB Uniform Resource Identi ...

  4. python和搜索

    # -*- coding: UTF-8 -*- import re # 搜索逻辑 def querylogic(list): query = {} if len(list) > 1 or len ...

  5. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  6. percona-toolkit工具安装

    1.yum安装 yum install perl-TermReadKey.x86_64 yum install perl-IO-Socket-SSL yum install perl-DBI.x86_ ...

  7. [codeforces722D]Generating Sets

    [codeforces722D]Generating Sets 试题描述 You are given a set Y of n distinct positive integers y1, y2, . ...

  8. Java并发编程:自己动手写一把可重入锁

    关于线程安全的例子,我前面的文章Java并发编程:线程安全和ThreadLocal里面提到了,简而言之就是多个线程在同时访问或修改公共资源的时候,由于不同线程抢占公共资源而导致的结果不确定性,就是在并 ...

  9. PHP统计目录中文件个数和文件大小

    <meta charset="utf-8"><?php $dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的 ...

  10. msp430项目编程13

    msp430中项目---温湿度检测系统 1.dht11工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习