首先设计Vector与Point的表示方法,再依次完善两个类中的构造函数,复制构造函数等。

向量由两个点表示,当进行运算的时候,转化起点坐标为(0,0);

第14行:由于Vector需要用到Point中的x,y ,故设计为友元较为方便;

第13行:因为无法修改ostream与istream类,所以将"<<" ">>"重载为全局函数或者申明为友元;返回os能够实现"<<"  ">>"的连续使用;

第49行:设计第二个构造函数,以便实现Vector中运算符重载时的返回值的情况;

第59行:防止自复制;

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Point // Point类
  5. {
  6. private :
  7. double x,y;
  8. public :
  9. Point(double x1=,double y1=):x(x1),y(y1) {} // Point构造函数
  10. Point(const Point &rhs); // Point复制构造函数
  11. Point operator + (const Point & a); // Point +
  12. Point operator - (const Point & a); // Point -
  13. friend ostream & operator << (ostream & os , const Point & a); // Point输出重载
  14. friend class Vector; // 友元
  15. };
  16.  
  17. Point::Point(const Point & rhs) // Point复制构造函数
  18. {
  19. if(this != &rhs)
  20. {
  21. x = rhs.x;
  22. y = rhs.y;
  23. }
  24. }
  25.  
  26. Point Point::operator + (const Point & a) // Point +
  27. {
  28. return Point(x + a.x , y + a.y);
  29. }
  30.  
  31. Point Point::operator - (const Point & a) // Point -
  32. {
  33. return Point(x - a.x , y - a.y);
  34. }
  35.  
  36. ostream & operator << (ostream & os , const Point & a) // Point <<
  37. {
  38. os <<a.x<<","<<a.y;
  39. return os;
  40. }
  41.  
  42. class Vector // Vector 类
  43. {
  44. private :
  45. Point beg,end,_00;
  46. public :
  47. Vector(double x1=,double y1=,double x2=,double y2=) // 构造函数1
  48. :beg(x1,y1),end(x2,y2) {}
  49. Vector(Point beg1,Point end1):beg(beg1),end(end1) {} // 构造函数2(重载)
  50. Vector(const Vector & b); // 复制构造函数
  51. Vector operator + (const Vector & b); // Vector +
  52. Vector operator - (const Vector & b); // Vector -
  53. friend ostream & operator << (ostream & os , const Vector & b); // Vector 输出重载 <<
  54. // void print(); // 输出函数
  55. };
  56.  
  57. Vector::Vector(const Vector & rhs1) // 复制构造函数
  58. {
  59. if(this != &rhs1)
  60. {
  61. beg = rhs1.beg;
  62. end = rhs1.end;
  63. }
  64. }
  65.  
  66. Vector Vector::operator + (const Vector & b) // Vector +
  67. {
  68. return Vector(_00 , end -beg + b.end - b.beg); // 利用构造函数重载构造
  69. }
  70.  
  71. Vector Vector::operator - (const Vector & b) // Vector -
  72. {
  73. return Vector(_00 , end - beg - b.end + b.beg);
  74. }
  75. /*
  76. void Vector::print() // 输出函数
  77. {
  78. cout <<"("<<beg.x<<","<<beg.y<<")"<<"\t";
  79. cout <<"("<<end.x<<","<<end.y<<")"<<endl<<endl;
  80. }
  81. */
  82.  
  83. ostream & operator << (ostream & os , const Vector & b)
  84. {
  85. os <<"("<<b.beg<<")"<<"\t";
  86. os <<"("<<b.end<<")"<<endl<<endl;
  87. return os;
  88. }
  89.  
  90. int main()
  91. {
  92. Vector ww(,,,),mm(,,,);
  93. // ww.print();
  94. // mm.print();
  95. // (ww+mm).print();
  96. // (ww-mm).print();
  97. cout<<ww<<mm<<ww-mm<<mm+ww;
  98. return ;
  99. }

2019-10-17

C++中的Point类与vector类的简单处理的更多相关文章

  1. 【java】学习路径25-ArrayList类,Vector类,LinkedList类的使用和区别,Iterator迭代器的使用

    ArrayList的使用 ArrayList类:可变化长度的数组. 与一般的数组不同的是,其长度不固定,可以添加任意类型的数据. 也可以添加不同类型的数据,但是一般不这么做. ArrayList类位于 ...

  2. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  3. java中Vector类的常用方法

    Vector类是实现List接口,所以继承的方法就不在这里讲了 https://www.cnblogs.com/xiaostudy/p/9503199.html public void add(int ...

  4. vector 类中的 push_back( ) 函数

    函数名 push_back,算法语言里面的一个函数名,如:   1) c++中的vector头文件里面就有这个push_back函数:   2) 在vector类中作用为在vector尾部加入一个数据 ...

  5. Java API —— ArrayList类 & Vector类 & LinkList类

    1.ArrayList类     1)ArrayList类概述         · 底层数据结构是数组,查询快,增删慢         · 线程不安全,效率高     2)ArrayList案例   ...

  6. 转载:C++ vector 类学习笔记

    声明:本文转载自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机 ...

  7. Java Vector 类

    Vector类实现了一个动态数组.和ArrayList和相似,但是两者是不同的: Vector是同步访问的. Vector包含了许多传统的方法,这些方法不属于集合框架. Vector主要用在事先不知道 ...

  8. 五:Java之Vector类专题

    据说期末考试要考到Vector 这个类,出于复习须要在这里就要好好整理下这个类了. 一.基本概念 Vector 是可实现自己主动增长的对象数组. java.util.vector提供了向量类(vect ...

  9. List接口的实现类(Vector)(与ArrayList相似,区别是Vector是重量级的组件,使用使消耗的资源比较多。)

      LinkedList提供以下方法:(ArrayList无此类方法) addFirst();    removeFirst();   addLast();   removeLast(); 在堆栈中, ...

随机推荐

  1. CentOS 7下简答搭建DNS服务器

    一.DNS相关介绍 DNS(Domain Name System)域名系统协议,作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用挨个记住IP地址.通过主机名,最终得到 ...

  2. flask flask_session,WTForms

    一.Flask_session 本质上,就是是cookie 下的session存储在redis中,方便快速取得session from flask import Flask,session from ...

  3. 快速安装Rainbond——开源企业级Paas平台

    快速安装Rainbond--开源企业级Paas平台 参考:https://www.rainbond.com/docs/user-operations/install/online_install/ R ...

  4. 京东框架jd_frame

    #!/user/bin/python# -*- coding:utf-8 -*-#1.定义京东首页def index(): pass#2.定义加目录def home(): pass#3.定义购物车功能 ...

  5. nginx发布静态资源

    nginx发布静态资源 参考 ngx_http_index_module index指令 ngx_http_core_module http指令 location指令 listen指令 root指令 ...

  6. (day47)jQuery

    目录 一.初识jQuery (一)jQuery介绍 (二)版本介绍 (三)jQuery对象 (四)相关网站 (五)基础语法 二.查找标签 (一)基本选择器 (1)id选择器 (2)标签选择器 (3)c ...

  7. Shell:

    Bash Notes for professionals ebook https://blog.csdn.net/simple_the_best/article/details/52821136 27 ...

  8. B1001 害死人不偿命的(3n+1)猜想 (15 分)

    一.参考代码: #include<iostream> using namespace std; int main(){ int n; int step = 0; cin >> ...

  9. Bootstrap-table 单元格合并 、表头合并

    1.页面引入js/css @*1.Jquery组件引用*@ <script src="~/Scripts/jquery-1.10.2.js"></script&g ...

  10. ES方法使用注意

    matchQuery:会将搜索词分词,再与目标查询字段进行匹配,若分词中的任意一个词与目标字段匹配上,则可查询到.   termQuery:不会对搜索词进行分词处理,而是作为一个整体与目标字段进行匹配 ...