news 2026/6/10 16:02:19

6.再谈重载:一个矢量类

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
6.再谈重载:一个矢量类

6.再谈重载:一个矢量类

位移矢量指的是从何处开始到何处结束,而不是经过的路线。

VECTOR.h

#pragma once #ifndef VECTOR_H__ #define VECTOR_H__ #include <iostream> using namespace std; namespace Vector { class VECTOR1 { public: enum Mode { RECT, POL }; private: double x; // horizontal value double y; // vertical value double mag; // length of vector double ang; // direction of vector in degrees Mode mode; // RECT or POL // private methods for setting values void set_mag(); void set_ang(); void set_x(); void set_y(); public: VECTOR1(); VECTOR1(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); double xval() const { return x; } // report x value double yval() const { return y; } // report y value double magval() const { return mag; } // report magnitude double angval() const { return ang; } // report angle void polar_mode() { mode = RECT; }; // set mode to POL void rect_mode() { mode = POL; }; // set mode to RECT ​ VECTOR1 operator+(const VECTOR1& b) const; VECTOR1 operator-(const VECTOR1& b) const; VECTOR1 operator-() const; VECTOR1 operator*(double n) const; ​ friend VECTOR1 operator*(double n, const VECTOR1& a); friend ostream& operator<<(ostream& os, const VECTOR1& v); }; }; ​ #endif ​

,

VECTOR.cpp

#include "VECTOR.h" #include <cmath> namespace Vector { ​ //double num = 180.0 / 3.14159265358979323846; const double num = 45.0 / atan(1.0); void VECTOR1::set_mag() { mag = sqrt(x * x + y * y); } ​ void VECTOR1::set_ang() { if (x == 0.0 && y == 0.0) { ang = 0.0; } else { ang = atan2(y, x); } } ​ void VECTOR1::set_x() { x = mag * cos(ang); } ​ void VECTOR1::set_y() { y = mag * sin(ang); } ​ VECTOR1::VECTOR1() { x = y = mag = ang = 0.0; mode = RECT; } ​ VECTOR1::VECTOR1(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; set_mag(); set_ang(); } else if (form == POL) { mag = n1; ang = n2 / num; set_x(); set_y(); cout << "ang=" << ang << "mag=" << mag << endl; } else { cout << "Incorrect 3rd argument to VECTOR() -- "; cout << "vector set to 0\n"; x = y = mag = ang = 0.0; mode = RECT; } } ​ void VECTOR1::reset(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; set_mag(); set_ang(); } else if (form == POL) { mag = n1; ang = n2 / num; set_x(); set_y(); } else { cout << "Incorrect 3rd argument to VECTOR() -- "; cout << "vector set to 0\n"; x = y = mag = ang = 0.0; mode = RECT; } } ​ VECTOR1 VECTOR1::operator+(const VECTOR1& b) const { VECTOR1 temp; temp.x = x + b.x; temp.y = y + b.y; temp.set_mag();//类适合在一个对象中表示实体的不同方面(直角坐标和极坐标) temp.set_ang(); return temp; }//按照这种重载方式,不仅要更新直角坐标,还要更新极坐标, //按照下面用构造函数的方式就方便的多 VECTOR1 VECTOR1::operator-(const VECTOR1& b) const { return VECTOR1(x - b.x, y - b.y); } VECTOR1 VECTOR1::operator-() const { return VECTOR1(-x, -y); } VECTOR1 VECTOR1::operator*(double n) const { return VECTOR1(n * x, n * y); } ​ VECTOR1 operator*(double n, const VECTOR1& a) { return a * n; } ​ ostream& operator<<(ostream& os, const VECTOR1& v) { if (v.mode == VECTOR1::RECT) { os << "(x,y) = (" << v.x << "," << v.y << ")"; } else if (v.mode == VECTOR1::POL) { os << "(m,a) = (" << v.mag << "," << v.ang<< ")"; //cout << "d" << v.mag << v.ang * num; } else { os << "Vector object mode is invalid"; } return os; } }

main.cpp

// VECTOR.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // ​ #include <iostream> #include "VECTOR.h" using namespace Vector; int main() { std::cout << "Hello World!\n"; VECTOR1 a = VECTOR1(3.0, 4.0, VECTOR1::POL); VECTOR1 B = VECTOR1(6.0, 4.0, VECTOR1::RECT); VECTOR1 d = VECTOR1(8.0, 6.0, VECTOR1::POL); /*VECTOR1 a = VECTOR1(3.0, 4.0, RECT);*/ cout << "a: " << a << endl; cout << "B: " << B << endl; VECTOR1 c; c = a + B; cout << "c=a+B: " << c << endl; c = a - B; cout << "c=a-B: " << c << endl; c = -d; cout << "c=-d: " << c << endl; return 0; } ​ // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 ​ // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 ​

类适合在一个对象中表示实体的不同方面

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 13:45:47

线上历史馆藏系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】

摘要 随着数字化时代的快速发展&#xff0c;博物馆和文化机构对历史文物和馆藏资源的管理需求日益增长。传统的手工记录和纸质档案管理方式已无法满足现代高效、精准、可追溯的管理需求。线上历史馆藏系统信息管理系统的开发旨在解决这一问题&#xff0c;通过数字化手段实现文物…

作者头像 李华
网站建设 2026/6/10 11:57:14

基于Web的可视化入侵检测系统

文章目录前言一、详细操作演示视频二、具体实现截图三、技术栈1.前端-Vue.js2.后端-SpringBoot3.数据库-MySQL4.系统架构-B/S四、系统测试1.系统测试概述2.系统功能测试3.系统测试结论五、项目代码参考六、数据库代码参考七、项目论文示例结语前言 &#x1f49b;博主介绍&#…

作者头像 李华
网站建设 2026/6/10 18:38:17

Langchain-Chatchat文档去重策略:避免重复索引的有效手段

Langchain-Chatchat文档去重策略&#xff1a;避免重复索引的有效手段 在企业构建私有知识库的过程中&#xff0c;一个看似不起眼却影响深远的问题逐渐浮现&#xff1a;文档重复。无论是多个部门各自保存的《员工手册》&#xff0c;还是技术团队反复迭代的项目方案v1.0、v1.5、f…

作者头像 李华
网站建设 2026/6/10 14:23:31

紫苏小说阅读 V0.1:PC端高TXT小说阅读器

紫苏小说阅读 V0.1 是专为 Windows 用户打造的电脑端小说阅读工具&#xff0c;以高颜值界面和实用功能填补了电脑端优质阅读器的空白。无论是日常休闲阅读&#xff0c;还是办公间隙的碎片化阅读&#xff0c;这款软件都能通过个性化设置和便捷操作&#xff0c;为用户带来充满仪式…

作者头像 李华
网站建设 2026/6/10 18:39:10

救命!网安从业者必须知道的 100 个知识点,你达标了吗?

100条必背网络安全知识点&#xff0c;你都掌握了吗&#xff1f; 1988年&#xff0c;一款名为“莫里斯蠕虫”的程序悄然传播&#xff0c;它最初是康奈尔大学研究员的实验项目&#xff0c;目的是测量互联网规模。可谁也没想到&#xff0c;这个程序失控后感染了数千台电脑&#x…

作者头像 李华
网站建设 2026/6/10 10:55:45

25、Windows Server 网络配置与 DHCP 管理全解析

Windows Server 网络配置与 DHCP 管理全解析 1. TCP/IP 网络安装 要在计算机上安装网络,需要安装 TCP/IP 网络和网络适配器。Windows Server 2012 R2 默认使用 TCP/IP 作为广域网协议。通常,在安装 Windows Server 2012 R2 时会安装网络,也可以通过网络连接属性来安装 TCP…

作者头像 李华