麻烦各位大侠看下这个问题,有关c++的,我太菜了

题目是:定义直角坐标系上的一个点类(Point)作为基类,派生出描述一条直线的类(Line)(提示:两点坐标确定一直线),再派生出三角形类(Triangle)(提示:三点... 题目是:定义直角坐标系上的一个点类(Point)作为基类,派生出描述一条直线的类(Line)(提示:两点坐标确定一直线),再派生出三角形类(Triangle)(提示:三点坐标确定一个三角形)。要求成员函数能求出两点间的距离,三角形的周长和面积。
下面是我的:
//point.h
#ifndef POINT_H
#define POINT_H
class point
{
protected:
double x,y;
public:
point(double a,double b)
{
x=a;y=b;
}
void display();
};
#endif
//line.h
#include "point.h"
#include <cmath>
class line:public point
{
protected:
double distance,x2,y2;
point p;
public:
line(double,double,double,double):point(x,y),p(x2,y2){}
void caldistance();
void linedisplay();
};
//point.cpp
#include "point.h"
#include <iostream>
using namespace std;
void point::display()
{
cout<<"输入的点的坐标为:("<<x<<","<<y<<")"<<endl;
}
//line.cpp
#include "line.h"
#include <iostream>
using namespace std;
void caldistance()
{
distance=sqrt((x2-p.x)*(x2-p.x)+(y2-p.y)*(y2-p.y));
}
void linedisplay()
{
cout<<"两点构成的直线距离为:"<<distance<<endl;
}
虽然没写完,但其实主要就是写到这里出问题实在太多写不下去了,我关于怎么调用的实在掌握的有点小乱。。。所以写出来也乱。。。关键就在于我想用派生类的构造函数,但自己怎么写都不对,麻烦各位指点。说的详细的(最好能有空多辅导辅导我的发个邮件给我吧:dfilth@126.com)加分
展开
 我来答
新段志亮
推荐于2016-11-28
知道答主
回答量:14
采纳率:0%
帮助的人:0
展开全部
#include <math.h>
#include <iostream>
using namespace std;

class point
{
protected:
double x1,y1;
public:
point(double a,double b)
{
x1=a;
y1=b;
}
~point()
{}
virtual void display();
};

class line:public point
{
protected:
double x2,y2;
public:
line(double x,double y ,double m,double n):point(x,y)
{
x2 = m;
y2 = n;
}
~line()
{}
double caldistance(double x2,double x1,double y2,double y1);
void display();
};

void point::display()
{
cout<<"输入的点的坐标为:("<<x1<<", "<<y1<<")"<<endl;
}

double line::caldistance(double x2,double x1,double y2,double y1)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
void line::display()
{
cout<<"两点构成的直线距离为:"<<caldistance(x2,x1,y2,y1)<<endl;
}

class Triangle:public line
{
protected :
double x3,y3;
public:
Triangle(double m,double n,double a,double b,double c,double):line(m,n,a,b)
{
x3 = a;
y3 = b;
}
~Triangle()
{}

double GetPerimeter()
{
return caldistance(x2,x1,y2,y1) + caldistance(x3,x1,y3,y1) + caldistance(x3,x2,y3,y2);
}
//求面积的算法自己做吧,我在这就不做了
//.......可以用“海伦公式”求面积,比较简单
//.......
void display()
{
cout <<"三角形的周长是:"<<GetPerimeter()<<endl;
}
};

int main()
{
point a(3.0,4.5),*p;
p = &a;
p->display();
line b(5,6,7,8);
p = &b;
p->display();
Triangle c(2,3,8,6,7,8);
p = &c;
p->display();
return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式