ShiLuoTianKongch 2017-06-06 16:39 采纳率: 0%
浏览 954

c++如何补充代码,求面积、周长用继承

//shape.h

#ifndef SHAPE__H
#define SHAPE__H

#include
#include
static const float PI=3.14159;

class Shape{
public:

Shape():name_("Shape"),width_(0),height_(0),originx_(0),originy_(0){};

~Shape(){
    std::cout<<"~Shape() called\n";
}

public:

private:

};

class Circle: public Shape{
public:
Circle(float r): Shape(2*r,2*r,0,0,"Circle"), radius(r){
std::cout<<" Circle() called\n";
}
~Circle(){
std::cout<<" ~Circle() called\n";
}
float getarea(){
return PI*radius*radius;
}
float getperimeter(){
return 2*PI*radius;
}
private:

};

class Rectangle : public Shape{
public:

~Rectangle(){
    std::cout<<" ~Rectangle() called\n";
}

};

class Square: public Rectangle{
public:
Square(float w): Rectangle(w,w,"Square") {
std::cout<<" Square() called\n";
}
~Square(){
std::cout<<" ~Square() called\n";
}
};

#endif

//shape.cpp

#include "shape.h"
#include
#include
#include

using namespace std;

void test_1(){
cout<<"test_1 begin\n";

Shape* papers[5];
Circle michael(15);
Rectangle andrew(34.0,2.0);
Circle ann(12.5);
Square tim(9.99);
Rectangle jonathan(12.95,11.25);

papers[0] = &michael;
papers[1] = &andrew;
papers[2] = &ann;
papers[3] = &tim;
papers[4] = &jonathan;


for(int i=0;i<5;i++) {
    cout << "Shape " << setw(10) << papers[i]->getname()<<" area:" << setw(10) << papers[i]->getarea() << " perimeter:"<< setw(10)<< papers[i]->getperimeter()<< endl;
}
cout<<"test_1 end\n";

}
/* test_1 output
test_1 begin
Shape() called
Circle() called
Shape() called
Rectangle() called
Shape() called
Circle() called
Shape() called
Rectangle() called
Square() called
Shape() called
Rectangle() called
Shape Circle area: 706.858 perimeter: 94.2477
Shape Rectangle area: 68 perimeter: 72
Shape Circle area: 490.873 perimeter: 78.5397
Shape Square area: 99.8001 perimeter: 39.96
Shape Rectangle area: 145.688 perimeter: 48.4
test_1 end
~Rectangle() called
~Shape() called
~Square() called
~Rectangle() called
~Shape() called
~Circle() called
~Shape() called
~Rectangle() called
~Shape() called
~Circle() called
~Shape() called
*/

void test_2(){
cout<<"test_2 begin\n";

Shape* papers[5];
papers[0] = new Circle(15.0f);
papers[1] = new Rectangle(34.0f,2.0f);
papers[2] = new Circle(12.5f);
papers[3] = new Square(9.99f);
papers[4] = new Rectangle(12.95f,11.25f);

for(int i=0;i<5;i++) {
    cout << "Shape " << setw(10) << papers[i]->getname()<<" area:" << setw(10) << papers[i]->getarea() << " perimeter:"<< setw(10)<< papers[i]->getperimeter()<< endl;
}

for(int i=0;i<5;i++)
    delete papers[i];

cout<<"test_2 end\n";

}
/* test_2: output
test_2 begin
Shape() called
Circle() called
Shape() called
Rectangle() called
Shape() called
Circle() called
Shape() called
Rectangle() called
Square() called
Shape() called
Rectangle() called
Shape Circle area: 706.858 perimeter: 94.2477
Shape Rectangle area: 68 perimeter: 72
Shape Circle area: 490.873 perimeter: 78.5397
Shape Square area: 99.8001 perimeter: 39.96
Shape Rectangle area: 145.688 perimeter: 48.4
~Shape() called
~Shape() called
~Shape() called
~Shape() called
~Shape() called
test_2 end
*/

int main(){
test_1();
test_2();
return 0;
}

  • 写回答

1条回答 默认 最新

  • shen_wei 2017-06-07 07:48
    关注
     class Shape
    {
    public:
        Shape():name_("Shape"),width_(0),height_(0),originx_(0),originy_(0)
        {
    
        };
        Shape(int width,int height,int originx,int originy,string name)
        {
            width_ = width;
            height_ = height;
            originx_ = originx;
            originy_ = originy;
            name_ = name;
        };
        ~Shape()
        {
            std::cout<<"~Shape() called\n";
        }
    public: 
        string name_;
        int width_;
        int height_;
        int originx_;
        int originy_;
    private:
    };
    
    class Circle: public Shape
    {
    public:
        Circle(float r): Shape(2*r,2*r,0,0,"Circle"), radius(r)
        {
            std::cout<<" Circle() called\n";
        }
        ~Circle()
        {
            std::cout<<" ~Circle() called\n";
        }
        float getarea()
        {
            return PI*radius*radius;
        }
        float getperimeter()
        {
            return 2*PI*radius;
        }
    public:
        float radius;
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站