运行结果应该不对 todo 继承与派生 P228

//
// Created by 刘彪 on 2020/3/2.
//运行结果应该不对  todo 继承与派生 P228

#include <iostream>
#include <cmath>
using namespace std;
class Point{
protected:
    double x,y;
public:
    Point(){}
    Point(double a,double b){
        x = a;
        y = b;
    }
    double getx(){return x;}
    double gety(){return y;}
};

class Line{
protected:
    Point p1,p2;
    double length,angle;
public:
    Line(double a, double b,double c,double d):p1(a,b),p2(c,d){
        init();
    }
    Line(Point a,Point b){
        p1 = a;
        p2 = b;
        init();
    }
    void init(){
        double x1 = p1.getx(),y1 = p1.gety();
        double x2 = p2.getx(),y2 = p2.gety();
        length = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
        angle = atan((y2-y1)/(x2-x1));
        angle = angle*180/3.1415926;
    }
    void dispxy(){
        cout<<"("<<p1.getx()<<","<<p1.gety()<<");("<<p2.getx()<<","<<p2.gety()<<")"<<endl;
    }
    void printLength(){
        cout<<"线段长度:"<<length<<endl;
    }
    void printAngle(){
        cout<<"与x轴的夹角:"<<angle<<"°"<<endl;
    }


};

class Rectangle:public Line{
protected:
    Line *line;
public:
    Rectangle(double a, double b,double c,double d,double e,double f,double g,double h):Line(a,b,c,d){
        line = new Line(e,f,g,h);
    }
    Rectangle(Point a,Point b,Point c,Point d):Line(a,b){
        line = new Line(c,d);
    }
    void disPoint(){
        cout<<"矩形4个顶点:\n";
        dispxy();
        line->dispxy();
    }
};

int main(){
    Point p1(0,0),p2(4,3),p3(12,89),p4(10,-50);
    Line l1(0,0,4,3);
    l1.dispxy();
    l1.printLength();
    l1.printAngle();
    Line l2(p1,p2);
    l2.dispxy();
    l2.printLength();
    l2.printAngle();
    Rectangle r1(12,45,89,10,10,23,56,1);
    r1.disPoint();
    Rectangle r2(p1,p2,p3,p4);
    r2.disPoint();

    return  0;
}

全部评论

相关推荐

面了100年面试不知...:今年白菜这么多,冬天可以狂吃了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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