C++运算符重载 Overloading 实例-三维向量vec类

本文介绍了如何在C++中通过自定义vec类实现运算符重载,以支持向量的加减、点乘、叉乘、模长计算和向量之间的夹角计算,以及向量的数乘和张量输出功能。

C++运算符重载 Overloading 实例-三维向量vec类

我们经常会使用到向量这一类型。在学习了运算符重载之后可以通过设计一个向量类,使用运算符重载来方便的对向量进行运算。向量运算的基本功能:

  • 向量的加减
  • 向量的点乘和叉乘
  • 向量的模
  • 向量之间的夹角
  • 单位向量

主函数如下:

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

int main() {
    vec v1(1, 3, 5);
    vec v2(2, 3, 2);
    cout << "v1 = " << v1 << endl;
    cout << "v2 = " << v2 << endl;
    cout << "v1 + v2 = " << v1+v2 << endl;
    cout << "v1 - v2 = " << v1-v2 << endl;
    cout << "v1 * v2 = " << v1*v2 << endl;
    cout << "-v1 = " << -v1 << endl;
    cout << "v1 * 3 = " << v1*3 << endl;
    cout << "cos(v1,v2) = ";
    double a = v1^v2;
    cout << a << endl;
    v1 += v2;
    cout << "v1 += v2 , v1 = " << v1 << endl;
//    v1 -= v2;
//    cout << "v1 -= v2 , v1 = " << v1 << endl;
    cout << "The Length of v1 is " << v1.len << endl;
    cout << "v1(->) = " << (~v1) << endl;

//    cout << v1[3] << endl; // ERROR
    return 0;
}

运行结果:

v1 = (1,3,5)
v2 = (2,3,2)
v1 + v2 = (3,6,7)
v1 - v2 = (-1,0,3)
v1 * v2 = 21
-v1 = (-1,-3,-5)
v1 * 3 = (3,9,15)
cos(v1,v2) = 0.860916
v1 += v2 , v1 = (3,6,7)
The Length of v1 is 9.69536
v1(->) = (0.309426,0.618853,0.721995)

vec.h 头文件

//
// Created by frank on 2024/1/18.
//

#ifndef VECTOR_VEC_H
#define VECTOR_VEC_H

#include <iostream>


class vec
{
    friend vec operator+(const vec &, const vec &);
    friend vec operator-(const vec &, const vec &);
    friend double operator*(const vec &, const vec &);
    friend vec operator*(const vec &, double );
    friend vec operator|(const vec &, const vec &);
    friend double operator^(const vec &, const vec &);
    friend std::ostream &operator<<(std::ostream &, const vec &);

public:
    double x;
    double y;
    double z;
    double len;
    explicit vec(double x, double y, double z);
    [[nodiscard]] double getLen() const;
    // compute functions
    static vec add(const vec &v1, const vec &v2);
    static vec minus(const vec &v1, const vec &v2);
    static vec negative(const vec &v1);
    static double dotProduct(const vec &v1, const vec &v2);
    static vec numProduct(const vec &v1, double num);
    static vec vectorProduct(const vec &, const vec &);
    // overloading operator
    vec operator+=(vec &);
    vec operator-=(vec &);
    vec operator-() const;
    vec operator~() const;
    double operator[](int index) const;
};



#endif //VECTOR_VEC_H

vec.cpp 实现

//
// Created by lenovo on 2024/1/18.
//
#include "vec.h"
#include <cmath>
#include <string>
vec::vec(double x, double y, double z)
    :x(x), y(y), z(z)
{
    this->len = getLen();
}


[[nodiscard]] double vec::getLen() const
{
    return sqrt((this->x*this->x) + (this->y*this->y) + (this->z*this->z));
}

vec vec::operator+=(vec & v1) {
    this->x += v1.x;
    this->y += v1.y;
    this->z += v1.z;
    this->len = this->getLen();
    return *this;
}

vec vec::operator-=(vec & v1) {
    this->x -= v1.x;
    this->y -= v1.y;
    this->z -= v1.z;
    this->len = this->getLen();
    return *this;
}

vec operator+(const vec & v1, const vec & v2) {
    return vec::add(v1, v2);
}

vec operator-(const vec & v1, const vec & v2) {
    return vec::minus(v1, v2);
}

double operator*(const vec &v1, const vec &v2) {
    return vec::dotProduct(v1, v2);
}

vec operator*(const vec &v1, double n) {
    return vec::numProduct(v1, n);
}

vec vec::add(const vec &v1, const vec &v2) {
    return vec(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
}

vec vec::minus(const vec &v1, const vec &v2) {
    return add(v1, negative(v2));
}

vec vec::negative(const vec &v1) {
    return vec(-v1.x, -v1.y, -v1.z);
}

double vec::dotProduct(const vec &v1, const vec &v2) {
    return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}

vec vec::numProduct(const vec &v1, double num) {
    return vec(v1.x*num, v1.y*num, v1.z*num);
}

vec vec::operator-() const {
    return negative(*this);
}

vec vec::operator~() const {
    return vec(this->x / this->len, this->y / this->len, this->z / this->len);
}

double operator^(const vec &v1, const vec &v2) {
    return v1*v2 / (v1.len * v2.len);
}

std::ostream &operator<<(std::ostream &ostream, const vec &vec) {
    ostream << "(" << vec.x <<
                "," << vec.y <<
                "," << vec.z << ")";
    return ostream;
}

vec vec::vectorProduct(const vec &v1, const vec &v2) {
    return vec(v1[1]*v2[2] - v1[2]*v2[1],
            v1[2]*v2[0] - v1[0]*v2[2],
            v1[0]*v2[1] - v1[1]*v2[0]);
}

vec operator|(const vec &v1, const vec &v2) {
    return vec::vectorProduct(v1, v2);
}

double vec::operator[](int index) const {
    if (index > 2 or index < 0) {
        std::string message("The expected range for indices is from 0 to 2, but received index '");
        throw std::out_of_range(message+std::to_string(index)+"'.");
    }
    double res;
    index == 0 ? res = this->x : index == 1 ? res = this->y : res = this->z;
    return res;
}


这样便可以方便的对向量进行运算,ostream输出
后续可升级为张量

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值