65.9K
CodeProject 正在变化。 阅读更多。
Home

制作 C++ 几何标头

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.53/5 (7投票s)

2012年4月2日

CPOL

2分钟阅读

viewsIcon

37664

downloadIcon

295

一篇关于制作 C++ 头文件,用于简单的 2D 几何对象的文章。

介绍 

在本教程中,您将学习如何制作自己的头文件,用于在 C++ 中处理几何图形。头文件中将包含用于操作的类。

背景 

C++ 类的开发不如 C# 或 VB 的面向对象编程那么简洁,但它非常适用于快速程序创建、节省内存以及快速操作。 在阅读本文之前,您必须了解 C++ 面向对象编程的基础知识,并且应该理解继承的概念。

注意

本文的代码是在 Visual Studio 2010 中编写的,并使用默认编译器编译的。

使用代码 

好的,我们开始吧。

您应该启动您最喜欢的 IDE 并创建一个新项目。 在项目中创建一个新文件夹,该文件夹将包含我们的头文件。 然后,创建一个名为“GeometricObject”(扩展名 .h.hpp,具体取决于编译器)的新头文件。

现在,创建名为“Square”的新文件。 在此头文件中,我们将创建一个名为 Square 的简单类,用于根据边长计算面积和周长数据。

//
// Square.h
//
class Square
{
    public:
        Square(){this->side = 0;};
        Square(double sideSize){this->side = sideSize;};
        double GetSide() const{return this->side;};
        void SetSide(double size) {this->side = size;};
        double Area();
        double Circuit();
    private:
        double side;
};
double Square::Area()
{
    return this->side * this->side;
}
double Square::Circuit()
{
    return this->side * 4;
}

这个类非常简单。 我们正在创建构造函数、边操作函数以及用于获取面积周长的函数。 主要(私有)边数据存储变量是 side。 接下来,我们将创建一个名为 Rectangle 的类,它与 Square 类类似,但稍微复杂一些。

//
// Rectangle.h
//

class Rectangle
{
    public:
        Rectangle() {this->width = 0;this->height = 0;}
        Rectangle(double Width, double Height){this->height = Height;this->width = Width;}
        void SetSides(double Width, double Height){this->height = Height;this->width = Width;}
        void GetSides(double *Width, double *Height)const;
        double GetWidth() const {return width;}
        double GetHeight() const {return height;}
        double Area() const;
        double Circuit()const;
    private:
        double width, height;
};
void Rectangle::GetSides(double *Width, double *Height) const
{
    *Width = this->GetWidth();
    *Height = this->GetHeight();
}
double Rectangle::Area() const
{
    return width * height;
}
double Rectangle::Circuit() const
{
    return width * 2 + 2 * height;
}

这个类也包含基本的边操作方法。 它可用于计算普通数学/几何正方形的简单参数。

在下一个头文件(“Circle”)中,我们将创建一个圆类,并定义一个新的常量 PI,它将用于计算圆的周长面积

//
// Circle.h
//

#ifndef PI
    #define PI 3.142857
#endif
class Circle
{
    public:
        Circle(){this->r = 0;};
        Circle(double r){this->r = r;};
        double Getr() const{return this->r;};
        void Setr(double r){this->r = r;};
        double Area() const;
        double Circuit() const;
    private:
        double r;
};
double Circle::Area() const
{
    return PI * (this->r * this->r);
}
double Circle::Circuit() const
{
    return 2 * (PI * this->r);
}

与之前一样,这里我们有类似的函数来获取用户重要的数据。 我们使用 pi*r*r 来获取面积,使用 2pi*r 来获取周长

在最后一个头文件“Triangle”中,我们基于 vabc 创建一个基本的三角形类,这些是数学公式中重要的变量。

//
// Triangle.h
//
class Triangle
{
    public:
        Triangle(){this->v = this->a = this->b = this->c = 0;}
        Triangle(double v, double a, double b, double c){this->v = v;this->a = a;this->b = b;this->c = c;}
        void Setvabce(double v, double a, double b, double c){this->v = v;this->a = a;this->b = b;this->c = c;}
        void Getvabce(double *v, double *a, double *b, double *c) 
             const {*v = this->v;*a = this->a;*b = this->b;*c = this->c;}
        double Area();
        double Circuit();
    private:
        double v, a, b, c;
};
double Triangle::Area()
{
    return v * a / 2;
}
double Triangle::Circuit()
{
    return a + b + c;
}

示例代码

//
// main.cpp
//

#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include "Triangle.h"
using namespace std;

int main(int argc, char **argv)
{
    double a = 0;
    Circle c(10.5);
    cout << "Circuit of circle 'c': " << c.Circuit() << endl;
    Rectangle r(10, 50);
    cout << "Area of rectangle 'r': " << r.Area() << endl;
    Square s(5.758);
    cout << "Area of square 's': " << s.Area() << endl;
    Triangle t(5, 7, 7, 7);
    cout << "Circuit of triangle 't': " << t.Circuit() << endl;
    cin >> a;
    return 0;
}

值得关注的点  

如所有代码所示,我们都有数据存储在私有变量中,例如 widthrv。 为了访问这些数据,我们使用诸如 GetvabcSetr 之类的方法。 我们可以使用 C# 中的 Properties 等新元素在其他语言中完成此操作,这些元素可以将 get 和 set 操作设置为方法。 在 C++ 中可以通过一些技巧实现这一点,但它是可行的。 有很多免费的未扩展和未修补的空间,您可以利用这些空间并创建扩展。 扩展和库可以简化大型应用程序的编程。

历史 

文章没有更新。

© . All rights reserved.