继承与派生 P228 test

//
// Created by 刘彪 on 2020/3/2.
//继承与派生 P228 test

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

class Line{
protected:
    double sizeA;
public:
    Line(){
        cout<<"输入线段的长度:";
        cin>>sizeA;
    }
    Line(double a){
        sizeA = a;
    }
    double getLength(){
        return sizeA;
    }
};
class Triangle:public Line{
protected:
    double sizeB,sizeC;
public:
    Triangle(){
        cout<<"输入线段长度:";
        cin >> sizeB;
        sizeC = sqrt(sizeB*sizeB + sizeA*sizeA);
    }
    void dispSize(){
        cout<<"直角三角形,三条边分别是:";
        cout<<"A:"<<sizeA<<",b:"<<sizeB<<",c:"<<sizeC<<endl;
    }
};

class Rectangle:public Triangle{
protected:
    double sizeD;
public:
    Rectangle(){
        sizeC = sizeA;
        sizeD = sizeB;
    }
    void dispSize(){
        cout<<"矩形,四条边分别是:";
        cout<<"A:"<<sizeA<<",b"<<sizeB<<",C:"<<sizeC<<",D:"<<sizeD<<endl;
    }
};

int main(){
    cout<<"线段操作"<<endl;
    Line *l = new Line();
    cout<<"线段长度为:"<<l->getLength()<<endl;
    cout<<"直接三角形操作"<<endl;
    Triangle *t = new Triangle;
    t->dispSize();
    cout<<"矩形操作"<<endl;
    Rectangle *r = new Rectangle();
    r->dispSize();
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务