c++面向对象 设计一个面基类,派生类为点,线,三角形三个!
下面是部分代码,可以实现三角形和线的长度的计算,但是三角形的面积没有实现,还有就是怎样加入旋转、平移等属性,将线段、三角形的端点坐标进行这些属性的操作#include<iostream>
#include <cmath>
using namespace std;
class Line
{
public:
Line (double x1,double y1) {X1=x1;Y1=y1;}
friend double Line_length (Line &a,Line &b);
//virtual void area();
private:
int X1,Y1;
};
double Line_length (Line &a,Line &b)
{
double Line_dis;
double dx=a.X1-b.X1;
double dy=a.Y1-b.Y1;
Line_dis=sqrt(dx*dx+dy*dy);
return Line_dis;
}
class Triangle
{
public:
Triangle (double x2,double y2,double z2) {X2=x2;Y2=y2;Z2=z2;}
friend double Triangle_length (Triangle &e,Triangle &d,Triangle &f);
void area();
private:
int X2,Y2,Z2;
};
double Triangle_length (Triangle &c,Triangle &d,Triangle &e)
{
double Triangle_grith;
double s1,s2,s3;
double dx1=c.X2-d.X2;
double dy1=c.Y2-d.Y2;
double dx2=c.X2-e.X2;
double dy2=c.Y2-e.Y2;
double dx3=d.X2-e.X2;
double dy3=d.Y2-e.Y2;
s1=sqrt(dx1*dx1+dy1*dy1);
s2=sqrt(dx2*dx2+dy2*dy2);
s3=sqrt(dx3*dx3+dy3*dy3);
Triangle_grith=s1+s2+s3;
return Triangle_grith;
}
int main()
{
float x1,y1,x2,y2;
double d1; /*线Line的距离*/
double d2; /*三角形Triangle 的周长*/
float a1,b1,a2,b2,a3,b3;
float c1=0; /*将Z坐标都初始化为0,视为在XY平面内*/
float c2=0;
float c3=0;
cout<<"/********计算线类Line**********/"<<endl<<endl;
cout<<"请输入A点坐标:"<<endl<<"A:";
cin>>x1>>y1;
cout<<"请输入B点坐标:"<<endl<<"B:";
cin>>x2>>y2;
Line p1(x1,y1),p2(x2,y2);
d1=Line_length(p1,p2);
cout<<"AB之间距离: "<<d1<<endl;
cout<<"/********计算三角形Triangle********/"<<endl<<endl;
cout<<"请输入C点坐标: "<<endl<<"C:";
cin>>a1>>b1;
cout<<"请输入D点坐标: "<<endl<<"D:";
cin>>a2>>b2;
cout<<"请输入E点坐标: "<<endl<<"E:";
cin>>a3>>b3;
Triangle T1(a1,b1,c1),T2(a2,b2,c2),T3(a3,b3,c3);
d2=Triangle_length(T1,T2,T3);
cout<<"三角形的周长 : "<<d2<<endl;
return 0;
}