下例中定义了一个矢量类,并对矢量定义了一系列操作:
文件结构:
vector.h代码:
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
#include <iostream>
class Vector
{
public:
enum Mode {RECT, POLAR}; //定义两种模式,一种为直角坐标,一种为极坐标,状态成员
private:
double x;
double y; //直角坐标系的横纵坐标
double distance;
double angle; //极坐标的角度及距离
Mode mode; //
void setX();
void setY();
void setDis();
void setAngle();
public:
Vector();
Vector(double n1, double n2, Mode mode = RECT);
~Vector();
double xVal() const {return x;}
double yVal() const {return y;}
double distanceVal() const {return distance;}
double angleVal() const {return angle;}
void setRect(); // set mode to RECT
void setPolar(); // set mode to POLAR
Vector operator+(const Vector & v); //reload +
Vector operator-(const Vector & v);
Vector operator*(const double n);
friend Vector operator*(double n, Vector & v);
friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};
#endif // VECTOR_H_INCLUDED
vector.cpp代码:
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
void Vector::setX() //set the value of x
{
x = distance * cos(angle);
}
void Vector::setY()
{
x = distance * sin(angle);
}
void Vector::setDis() //set the value of distance
{
distance = sqrt(x*x + y*y);
}
void Vector::setAngle()
{
angle = atan2(y, x);
}
Vector::Vector() //默认构造函数
{
x = 0;
y = 0;
distance = 0;
angle = 0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form) //构造函数
{
mode = form;
if(form == RECT)
{
x = n1;
y = n2;
setAngle();
setDis();
}
else if(form == POLAR)
{
distance = n1;
angle = n2 / 57.2;
setX();
setY();
}
else
{
cout << "Mode error!\n";
x = 0;
y = 0;
distance = 0;
angle = 0;
mode = RECT;
}
}
Vector::~Vector() //析构函数
{
}
void Vector::setRect() // set mode to RECT
{
mode = RECT;
}
void Vector::setPolar() // set mode to POLAR
{
mode = POLAR;
}
Vector Vector::operator+(const Vector & v) //reload +
{
Vector result;
result.x = x + v.x;
result.y = y + v.y;
result.distance = distance + v.distance;
result.angle = angle + v.angle;
return result;
}
Vector Vector::operator-(const Vector & v) //reload +
{
Vector result;
result.x = x - v.x;
result.y = y - v.y;
result.distance = distance - v.distance;
result.angle = angle - v.angle;
return result;
}
Vector Vector::operator*(const double n)
{
Vector result;
result.x = x * n;
result.y = y * n;
result.distance = distance * n;
result.angle = angle;
return result;
}
Vector operator*(double n, Vector & v)
{
Vector result;
result.x = n * v.x;
result.y = n * v.y;
result.distance = n * v.distance;
result.angle = v.angle;
return result;
}
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if(v.mode == Vector::RECT) //因为这个函数是友元函数,不在类的作用域内,所以使用RECT要加上类名
os << "(x, y) = (" << v.x << ", " << v.y << ")" << endl;
else if(v.mode == Vector::POLAR)
os << "(d, a) = (" << v.distance << ", " << v.angle << ")" << endl;
else
os << "Vector mode is invalid." << endl;
return os;
}
main.cpp代码:
#include <iostream>
#include "vector.h"
using namespace std;
int main()
{
Vector v1(1,2), v2(3, 5), v3, v4, v5, v6;
Vector v7(12, 13, Vector::POLAR), v8;
v3 = v1 * 3;
v4 = v1 + v2;
v5 = v1 - v2;
v6 = 3 * v2;
v8 = 3 * v7;
cout << v2;
cout << v3;
cout << v4;
cout << v5;
cout << v6;
cout << v7;
cout << v8;
return 0;
}