14---7rectangle类-----类复合和继承的综合实例

头文件:

pont.h:

 

#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED

#include<iostream>
using namespace std;

class point
{
    friend ostream &operator<<(ostream &output,const point &p);
    public:
       //重载的构造函数
       point(double =0,double =0);
       point(const point &p);
       //析构函数
    ~point();

    //重载定值函数
    void setpoint(double a,double b);
    void setpoint(point &p);

   //取值函数
   double getx()const
   {
       return x;
   }

   double gety()const
   {
       return y;
   }

   private:
   double x;
   double y;

};
//下面是两种不同的设定值的方式,重载函数的用法
point ::point (double a,double b)
{
    x=a;
    y=b;
}
point ::point(const point &p)
{
    x=p.getx();//x=p.getx()
    y=p.gety();//y=p.gety()也行的哦
}

point ::~point(){}

void point ::setpoint(double a,double b)
{
    x=a;
    y=b;
}

void point::setpoint(point &p)
{
    x=p.getx();
    y=p.gety();
}

ostream&operator<<(ostream &output,const point &p)
{
    output<<"("<<p.getx()<<","<<p.gety()<<")";
    return output;
}



#endif // POINT_H_INCLUDED


头文件:

line.h:

#ifndef LINE_H_INCLUDED
#define LINE_H_INCLUDED
#include<iostream>
#include<math.h>
#include"point.h"
using namespace std;

class line
{
    friend ostream &operator<<(ostream &,line &);
    public:
      //重载的构造函数
      line (double startx=0,double starty=0,double endx=0,double endy=0);
      line (point start,point end);
      line (line &line);
      //析构~~~
      ~line();
      //重载定值函数
      void setline(double startx=0,double starty=0,double endx=0,double endy=0);
      void setline(point,point);

      double getlength() ; //计算线段长度,为求周长和面积准备

      //取值函数
      point getstartpoint(){return startpoint;}
      point getendpoint(){return endpoint;}

      private:
        point startpoint;
        point endpoint;

};

line::line(double startx,double starty,double endx,double endy)
    :startpoint(startx,starty),endpoint(endx,endy)
{
}

line::line(point start,point end)
    :startpoint(start),endpoint(end) //调用point类的构造函数(重载的第二个哦)
{
}

line::line(line&line)//利用point的复制构造函数
    :startpoint(line.getstartpoint()),endpoint(line.getendpoint())
{
}


line::~line()
{
}

//利用point的setpoint()函数取值
void line::setline(double startx,double starty,double endx,double endy)
//startpoint(startx,starty),endpoint(endx,endy)
{
    startpoint.setpoint(startx,starty);
    endpoint.setpoint(endx,endy);
}

void line::setline(point start,point end)
//:能用否?是否只能在构造函数中使用啊?
{
    startpoint.setpoint(start);
    endpoint.setpoint(end);
}

double line::getlength()
{
    double x1=startpoint.getx();//在line类中使用pooint类的成员只能通过point的成员函数
    double y1=startpoint.gety();
    double x2=endpoint.getx();
    double y2=endpoint.gety();

    return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

ostream &operator <<(ostream &output,  line &line)
{
    output<<line.startpoint<<"-->"<<line.endpoint<<".";
    output<<"It's lenght is:"<<line.getlength();

    return output;
}



#endif // LINE_H_INCLUDED


头文件:

polygon.h:

 

#ifndef POIYGON_H_INCLUDED
#define POIYGON_H_INCLUDED

#include<iostream>
#include"line.h"
#define MAX 10

class  polygon
{
    friend ostream&operator<<(ostream&,polygon &);
    public:
      polygon (point points[],int num);
      ~polygon();
      int getnumber();
      double getperimeter();


      protected:
        line borders[MAX];
        int number;
} ;

polygon::polygon(point points[],int num)
{
    number=num;
    for(int i=0;i<num;i++)//对于每一条边初始化
      borders[i].setline(points[i],points[(i+1)%num]);

}

polygon ::~polygon(){}

//计算周长
double polygon::getperimeter()
{
    double perimeter=0.0;
    for(int i=0;i<number;i++)
      perimeter+=borders[i].getlength();

      return perimeter;
}

//得到边数
int polygon::getnumber()
{
    return number;
}


ostream &operator<<(ostream &output,polygon &polygon)
{
    output<<"The ploygon has"<<polygon.number<<"borders:";
    for(int i=0;i<polygon.number;i++)
    output<<endl<<"\tborder["<<i<<"]:"<<polygon.borders[i];
    output<<endl<<"\tIts perimeter is:"<<polygon.getperimeter()<<endl;

    return output;
}
#endif // POIYGON_H_INCLUDED


头文件:

rectangle.h:

#ifndef POIYGON_H_INCLUDED
#define POIYGON_H_INCLUDED

#include<iostream>
#include"line.h"
#define MAX 10

class  polygon
{
    friend ostream&operator<<(ostream&,polygon &);
    public:
      polygon (point points[],int num);
      ~polygon();
      int getnumber();
      double getperimeter();


      protected:
        line borders[MAX];
        int number;
} ;

polygon::polygon(point points[],int num)
{
    number=num;
    for(int i=0;i<num;i++)//对于每一条边初始化
      borders[i].setline(points[i],points[(i+1)%num]);

}

polygon ::~polygon(){}

//计算周长
double polygon::getperimeter()
{
    double perimeter=0.0;
    for(int i=0;i<number;i++)
      perimeter+=borders[i].getlength();

      return perimeter;
}

//得到边数
int polygon::getnumber()
{
    return number;
}


ostream &operator<<(ostream &output,polygon &polygon)
{
    output<<"The ploygon has"<<polygon.number<<"borders:";
    for(int i=0;i<polygon.number;i++)
    output<<endl<<"\tborder["<<i<<"]:"<<polygon.borders[i];
    output<<endl<<"\tIts perimeter is:"<<polygon.getperimeter()<<endl;

    return output;
}
#endif // POIYGON_H_INCLUDED


main函数:

输出信息:

#include <iostream>
#include"rectangle.h"
using namespace std;

int main()
{
    point p1(3.0,3.5);
    point p2(6.5,5.5);

    cout<<"P1:"<<p1<<endl;
    cout<<"P2:"<<p2<<endl;
    cout<<endl;

    line l1(p1,p2);
    line l2(1.0,2.0,4.5,5.0);

    cout<<"L1:"<<l1<<endl;
    cout<<"L2:"<<l2<<endl;
    cout<<endl;

    //声明并输出一个5边行
    point points5[5];
    points5[0].setpoint(0,0);
    points5[1].setpoint(0,1);
    points5[2].setpoint(1,2);
    points5[3].setpoint(2.5,2);
    points5[4].setpoint(2,0.5);

    polygon ply(points5,5);
    cout<<"Ply:"<<ply<<endl;
     cout<<endl;

     //声明并输出一个矩形

     point points4[4];
     points4[0].setpoint(1,1);
     points4[1].setpoint(3,1);
     points4[2].setpoint(3,3);
	 points4[3].setpoint(1,3);

    rectangle rect(points4);

     cout<<"rectangle:"<<rect<<endl;


    return 0;
}


结果:

 

14---7rectangle类-----类复合和继承的综合实例_第1张图片

 

 

 

你可能感兴趣的:(14---7rectangle类-----类复合和继承的综合实例)