给个例子你参考下
//c.h文件
#ifndef c_h
#define c_h
class point
{
double x;
double y;
public:
point(double x1=0,double y1=0);
double get_x();
double get_y();
void draw();
};
class line:public point
{
private:
point p1;
point p2;
public:
line(point xp1,point xp2);
void draw();
double Distance();
};
class rect :public point
{
private:
point u1;
point u2;
public:
rect(point ru1,point ru2);
void draw();
double Area();
};
#endif
////////////////////////////////
//work.cpp文件
#include <iostream>
#include <cmath>
#include "c.h"
using namespace std;
point::point(double x1,double y1):x(x1),y(y1)
{}
double point::get_x()
{ return x;}
double point::get_y()
{ return y;}
void point::draw()
{
cout<<"point的x坐标值是:"<<x<<"
point的y坐标值是:"<<y<<endl;
}
line::line(point xp1,point xp2):p1(xp1.get_x(),xp1.get_y()),p2(xp2.get_x(),xp2.get_y())
{}
void line::draw()
{
cout<<"line的起点是point的x坐标值是:"<<p1.get_x()<<"
point的y坐标值是:"<<p1.get_y()<<endl;
cout<<"line的终点为point的x坐标值是:"<<p2.get_x()<<"
point的y坐标值是:"<<p2.get_y()<<endl;
}
double line::Distance()
{
double m=(p2.get_x()-p1.get_x())*(p2.get_x()-p1.get_x())+(p2.get_y()-p1.get_y())*
(p2.get_y()-p1.get_y());
return sqrt(m);
}
rect::rect(point ru1,point ru2):u1(ru1.get_x(),ru1.get_y()),u2(ru2.get_x(),ru2.get_y())
{}
void rect::draw()
{
cout<<"rect的起点为point的x坐标值是:"<<u1.get_x()<<"
point的y坐标值是:"<<u1.get_y()<<endl;
cout<<"rect的终点为point的x坐标值是:"<<u2.get_x()<<" point的y坐标值是:"<<u2.get_y()<<endl;
}
double rect::Area()
{
return fabs((u2.get_x()-u1.get_x())*(u2.get_y()-u1.get_y()));
}
//////////////////////////
//main.cpp文件
#include "c.h"
#include <iostream>
using namespace std;
int
main()
{
point p(2,3);
point p11(3,8);
point p21(7,5);
line l(p11,p21);
rect r(p11,p21);
p.draw();
l.draw();
r.draw();
cout<<"线的长度为:"<<l.Distance()<<endl;
cout<<"矩形的面积为:"<<r.Area()<<endl;
return 0;
}