用多文件组织多个类的程序

mian.cpp

#include<iostream>
#include<cmath>
#include"d.h"
using namespace std;
int main()
{
    CPoint X(2,3),Y(5,4),Z(7,8);
    CTriangle C1(X,Y,Z);
    cout<<"该三角形的周长为:"<<C1.perimeter()<<"该三角形的面积为:"<<C1.area()<<endl;
    cout<<"该三角形"<<(C1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
    cout<<"改三角形"<<(C1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;
    return 0;
}

d.h

class CPoint
{
public:
    CPoint(double xx,double yy);
    double distance1(CPoint p)const;
private :
    double x,y;
};
class CTriangle
{
public:
    CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z) {} //给出三点的构造函数
    void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
    double  perimeter(void);//计算三角形的周长
    double  area(void);//计算并返回三角形的面积
    bool isRightTriangle(); //是否为直角三角形
    bool isIsoscelesTriangle(); //是否为等腰三角形
private:
    CPoint A,B,C; //三顶点
};

safds.cpp

#include"d.h"
#include<cmath>
CPoint::CPoint(double xx,double yy)
{
    x=xx;
    y=yy;
}
double CPoint::distance1(CPoint p)const
{
    double l;
    l=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
    return l;
}

asd.cpp

#include"d.h"
#include<cmath>
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
    A=X;
    B=Y;
    C=Z;
}
double  CTriangle::perimeter(void )
{
    double a=A.distance1(B),b=A.distance1(C),c=B.distance1(C);
    return (a+b+c);
}
double CTriangle::area(void)
{
    double a=A.distance1(B),b=A.distance1(C),c=B.distance1(C);
    double l=(a+b+c)/2;
    double s=sqrt(l* (l - a) * (l - b) * (l - c));
    return s;
}
bool CTriangle::isRightTriangle()
{
    double a=A.distance1(B),b=A.distance1(C),c=B.distance1(C);
    if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
        return true;
    else
        return false;
}
bool CTriangle::isIsoscelesTriangle()
{
    double a=A.distance1(B),b=A.distance1(C),c=B.distance1(C);
    if(a==b||c==b||a==c)
        return true;
    else
        return true;
}

用多文件组织多个类的程序_第1张图片

你可能感兴趣的:(用多文件组织多个类的程序)