以点类Point及平面图形类Plane为基础设计三角形类Triangle

以平面图形类Plane为基类公有派生三角形类Triangle,main(void)函数完成对其的测试。

Point类结构说明:

 
Point类的数据成员包括:
①私有数据成员:X坐标x(double型),Y坐标y(double型)。
Point类成员函数包括:
①有参构造函数Point(double, double)和拷贝构造函数Point(const  Point  &),其中有参构造函数参数默认值为0,输出信息“Point Constructor run”,拷贝构造函数输出信息“Point CopyConstructor run”
②析构函数,析构函数输出信息“Point Destructor run”
③公有函数成员void  setX(double)和double getX() const分别返回和设置X坐标
④公有函数成员void  setY(double)和double getY() const分别返回和设置Y坐标
⑤公有成员函数void show() const用于显示点的坐标信息,显示格式为:Point(X=<X坐标>,Y=<Y坐标>)
 

Plane类结构说明:

 
抽象类Plane类的成员函数包括:
①纯虚函数virtual double length()const用于计算平面图形的周长
②纯虚函数virtual double area()const用于计算平面图形的面积
 

Triangle类结构说明:

 
公有派生三角形类Triangle以平面图形类Plane为基类,Triangle类的结构说明如下:
Triangle类的数据成员包括:
①私有数据成员:A顶点a(Point型),B顶点b(Point型),C顶点c(Point型)。
Triangle类成员函数包括:
①有参构造函数Triangle(const Point &, const Point &, const Point &)和拷贝构造函数Triangle(const Triangle &),其中有参构造函数参数包括3个顶点,默认值为Point(0,0),输出信息“Triangle Constructor run”,拷贝构造函数输出信息“Triangle CopyConstructor run”
②析构函数,析构函数输出信息“Triangle Destructor run”
③公有函数成员void setA(const Point &)和Point getA()const分别返回和设置A顶点
④公有函数成员void setB(const Point &)和Point getB()const分别返回和设置B顶点
⑤公有函数成员void setC(const Point &)和Point getC()const分别返回和设置C顶点
⑥公有函数成员void show()const用于显示三角形的信息,显示格式为:
Triangle(A=<A顶点>,B=<B顶点>,C=<C顶点>))
⑦重载double area()const用于计算三角形的面积
⑧重载double length()const用于计算三角形的周长
 

非成员函数说明:

 
①求两点间距离double pointDis(const Point &p1,const Point &p2)


裁判测试程序样例:

 
#include <iostream>
#include<cmath>
using namespace std;
//点类Point
class Point{
private:
    double x;
    double y;
public:
    Point(double xv=0,double yv=0);/*构造函数*/
    Point(const Point &p);         /*拷贝构造*/
    ~Point();                      /*析构函数*/
    virtual void show()const;      /*显示*/
    void setX(double xv);          /*设置X坐标*/
    void setY(double yv);          /*设置Y坐标*/
    double getX() const;           /*获取X坐标*/
    double getY() const;           /*获取Y坐标*/
};
Point::Point(const double xv,const double yv){/*构造函数*/
    x=xv;
    y=yv;
    cout<<"Point Constructor run"<<endl;
}
Point::Point(const Point &p){/*拷贝构造*/
    x=p.x;
    y=p.y;
    cout<<"Point CopyConstructor run"<<endl;
}
Point::~Point(){/*析构函数*/
    cout<<"Point Destructor run"<<endl;
}
void Point::show() const{/*显示*/
    cout<<"Point(X="<<x<<",Y="<<y<<")";
}
void Point::setX(double xv){/*设置X坐标*/
    x=xv;
}
void Point::setY(double yv){/*设置Y坐标*/
    y=yv;
}
double Point::getX() const{/*获取X坐标*/
    return x;
}
double Point::getY() const{/*获取Y坐标*/
    return y;
}
class Plane{/*平面图形基类*/
public:
    virtual double length()const=0;/*周长*/
    virtual double area()const=0;  /*面积*/
};

/*请在这里填写答案*/

void length(Plane *p){/*平面图形的周长函数*/
    cout<<"Length="<<p->length()<<endl;
}
void area(Plane &p){/*平面图形的面积函数*/
    cout<<"Area="<<p.area()<<endl;
}
//主函数
int main(void){
    double x,y;
    Point p1,p2(1,1),p3(2,2);
    Triangle t1,t2(t1);
    t1.show();
    cout<<endl;
    area(t1);
    length(&t1);
    cin>>x>>y;
    p1.setX(x);
    p1.setY(y);
    t2.setA(p1);
    t2.setB(p2);
    t2.setC(p3);
    t2.show();
    cout<<endl;
    area(t2);
    length(&t2);
    return 0;
}
 

输入样例:

3.0  2.0
 

输出样例:

Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle Constructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle CopyConstructor run
Triangle(A=Point(X=0,Y=0),B=Point(X=0,Y=0),C=Point(X=0,Y=0))
Area=0
Length=0
Triangle(A=Point(X=3,Y=2),B=Point(X=1,Y=1),C=Point(X=2,Y=2))
Area=0.5
Length=4.65028
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run

答案:

double pointDis(const Point &p1,const Point &p2)
{
return sqrt(pow(p1.getX()-p2.getX(),2)+pow(p1.getY()-p2.getY(),2));
}
class Triangle:public Plane
{
private:
Point a;
Point b;
Point c;
public:
Triangle(const Point &o=Point(0,0),const Point &p=Point(0,0),const Point &q=Point(0,0)):a(o),b(p),c(q)
{
cout<<"Triangle Constructor run"<<endl;
}
Triangle(const Triangle &t):a(t.a),b(t.b),c(t.c)
{
cout<<"Triangle CopyConstructor run"<<endl;
}
~Triangle()
{
cout<<"Triangle Destructor run"<<endl;
}
void setA(const Point &t)
{
this->a=t;
}
Point getA() const
{
return a;
}
void setB(const Point &t)
{
this->b=t;
}
Point getB() const
{
return b;
}
void setC(const Point &t)
{
this->c=t;
}
Point getC() const
{
return c;
}
void show() const
{
cout<<"Triangle(A=";
a.show();
cout<<",B=";
b.show();
cout<<",C=";
c.show();
cout<<")";
}
double length() const
{
return pointDis(a,b)+pointDis(a,c)+pointDis(b,c);
}
double area() const
{
double p = length()/2;
return sqrt(p*(p-pointDis(a,b))*(p-pointDis(a,c))*(p-pointDis(b,c)));
}
};



 
posted @ 2023-05-16 16:27  不如喝点  阅读(57)  评论(0编辑  收藏  举报