zoukankan      html  css  js  c++  java
  • C++类与对象实验(六)

    题:

    1、 设计描述平面坐标上的点CPoint类,该类满足下述要求:

    •具有x,y坐标信息;

    •具有带默认形参值的构造函数,参数分别用于初始化x和y坐标信息;

    •具有获取x、y信息的GetX和GetY函数,具有设置x、y信息的SetX和SetY函数;

    2、 设计一个矩形类CRectangle,该类满足下述要求:

    •具有矩形的左下角和右上角两个点的坐标信息,这两个点的数据类型是CPoint;

    •具有带参数的构造函数CRectangle(const CPoint &, const CPoint &),参数分别用于设置左下角和右上角两个点的坐标信息;

    •具有设置左下角和设置右上角的两个点坐标的功能SetLPoint(const CPoint &)和SetRPoint(const CPoint &);

    •具有获得周长(GetPerimeter)和获得面积(GetArea)的功能。

    3、 在main函数中,完成以下工作:

    •动态创建一个CRectangle类的对象a_rectagnle,其初始的左下角和右上角坐标分别为(2,5)、(6,8);调用GetPerimeter和GetArea获得矩形周长和面积,并将周长和面积显示在屏幕上;

    ///////////////////////////////////////////////////////

    接下来,我把总的代码发出来:

    我采用了.h和.cpp文件,类在.h文件中声明,在.cpp文件中定义。

    总的工程包括:cpoint.h  cpoint.cpp  rectangle.h  rectangle.cpp  main.cpp

    cpoint.h

    #ifndef _CPOINT_H
    #define _CPOINT_H
    #include <iostream>
    using namespace std;
    class CPoint
    {
    public:
        CPoint(int X,int Y):x(X),y(Y){}
        int GetX();
        int GetY();
        void SetX(int);
        void SetY(int);    
    private:
        int x,y;
    };
    
    #endif

    cpoint.cpp

    #include "cpoint.h"
    int CPoint::GetX()
    {
        return x;
    }
    int CPoint::GetY()
    {
        return y;
    }
    void CPoint::SetX(int X)
    {
        x=X;
    }
    void CPoint::SetY(int Y)
    {
        y=Y;
    }

    rectangle.h

    #ifndef _RECTANGLE_H
    #define _RECTANGLE_H
    #include "cpoint.h"
    class CRectangle
    {
    public:
        CRectangle(const CPoint &a,const CPoint &b):c1(a),c2(b){}
        void SetLPoint(const CPoint &);
        void SetRPoint(const CPoint &);
        void GetPerimeter();
        void GetArea();
    private:
        CPoint c1,c2;
    };
    #endif

    rectangle.cpp

    #include "rectangle.h"
    #include "cpoint.h"
    void CRectangle::SetLPoint(const CPoint &a)
    {
        c1=a;
    }
    void CRectangle::SetRPoint(const CPoint &b)
    {
        c2=b;
    }
    void CRectangle::GetPerimeter()
    {
        int Perimeter=0;
        Perimeter=( c2.GetX()-c1.GetX() )*2 + ( c2.GetY()-c1.GetY() )*2;
        cout << "Perimeter is:" << Perimeter << endl;
    }
    void CRectangle::GetArea()
    {
        int Area=0;
        Area=( c2.GetX()-c1.GetX() )*( c2.GetY()-c1.GetY() );
        cout<<"Area is:"<<Area<<endl;
    }

    main.cpp

    #include "rectangle.h"
    int main()
    {
        const CPoint a(2,5),b(6,8);
        CRectangle *p=new CRectangle(a,b);
        p->SetLPoint(a);
        p->SetRPoint(b);
        p->GetPerimeter();
        p->GetArea();
    
        delete p;
        return 0;
    }

    OK,就是这样啦!

  • 相关阅读:
    null和undefined的区别
    百度小程序组件引用问题
    hbase优化操作与建议
    Hbase Rowkey设计原则
    kafka容器报内存不足异常(failed; error='Cannot allocate memory' (errno=12))
    Hbase安装
    四、hive安装
    一、linux安装mysql
    三、hadoop、yarn安装配置
    linux下磁盘进行分区、文件系统创建、挂载和卸载
  • 原文地址:https://www.cnblogs.com/ht-beyond/p/4138336.html
Copyright © 2011-2022 走看看