在C++中,面向对象编程(OOP)虽然不如C#或VB那样清晰,但它非常适用于快速创建程序和节省内存。在阅读本文之前,需要了解C++ OOP的基础知识,并理解继承的概念。本文的代码是在Visual Studio 2010中编写并使用默认编译器编译的。
首先,应该启动喜欢的IDE并创建一个新项目。在项目中创建一个新文件夹,该文件夹将包含头文件。之后,创建一个名为"GeometricObject"的新头文件。接下来,创建一个名为"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;
}
这个类非常简单。创建了构造函数、边操作函数和获取面积和周长的函数。接下来,将创建一个名为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,用于计算圆的周长和面积。
#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'中,将创建一个基于v、a、b、c的基本三角形类,这些是数学公式中重要的。
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
#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;
}