继续实现Vector类
来源:互联网 发布:人工智能产品有哪些 编辑:程序博客网 时间:2024/06/05 11:30
继续实现Vector类
大纲
1. 尝试实现比较不同Vector对象的成员函数
2. this指针
3. 初涉运算符重载
4. 初涉友元函数
这里呢,我想实现一个这样的成员函数
能够让Vector类的对象和另一个Vector对象进行比较
然后返回长度较长的对象
尝试实现
函数原型 Vector max(Vector tv);
函数定义
Vector Vector::max(Vector tv){ if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y)) return tv; else return ??;}
当我想返回自己的时候,遇到问题了
怎么返回自己呢??
this指针
在类内部,有一个叫做this指针的东西
它表示的是指向被调用对象本身
比如:
我可以这样修改这一个函数
Vector Vector::max(Vector tv){ if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y)) return tv; else return *this;}
通过return*this
,就达到了返回自己的目的
此后,我们便可以这样调用
Vector v1(2,3);Vector v2(3,4);Vector m;m = v1.max(v2);
这样子,便可以找到v1和v2中的较长矢量
并返回对象赋值给max
同时,在类内部
还可以这样子使用this调用类的内部成员 this->x
this->y
实际上,在类的内部定义函数的时候
都会隐式的传递this指针
(就像函数传递参数一样)
通过this指针,可以访问自身的成员
也可以实现返回自身
const成员函数
显式的传递参数,我们可以加上const来防止被改动
但是在类成员函数中
类自身的传递是隐式的(即this指针)
我们有办法给this指针加上const
防止函数修改内部成员吗?
这里拿show()函数做例子
show()仅仅是作为输出而不用于修改
为了防止误操作,我可以给show()函数加上const
防止自己的误操作会修改类的内部成员
// 函数原型void show() const;//函数定义void Vector::show() const{ using namespace std; cout << x << "," << y << endl; return ;}
这里的const,放在了函数的末尾
我的理解是隐式传递的this指针没位置放const
然后不得已在后面找了个没东西放的放const
……
当然了
其实这个函数还可以这样写
// 函数原型void show() const;//函数定义void Vector::show() const{ using namespace std; cout << this->x << "," << this->y << endl; return ; //this是指向当前调用对象的指针 //所以当然可以这样子访问内部成员}
上面的写法是和之前的写法等价的!!
初涉运算符重载
这样子找v1,v2中的最大值
寻找最大值的语句可能有点奇怪 m = v1.max(v2);
我能不能这样子呢?
if (v1 < v2) max = v2;else max = v1;
对于 < 运算符而言,它能够处理很多种数据类型
能够处理int,double,char……
现在我们有了一种新的类型Vector
这种类型,能够这样比较吗?
需要自己定义适用于这个类型的运算符
下面先放一个运算符重载的例子
//函数原型:bool operator< (vector tv);//函数定义bool Vector::operator<(vector tv){ if ( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y) ) return true; else return false;}
这里,相当于定义了名字为operator<的一个函数
然后运算符< 的左边的操作数是被调用对象
< 右边的是括号内的tv对象
也就是说
(v1 < v2)
相当于这样子的函数调用
v1.operator<(v2)
初涉友元函数
原来寻找最大值的语句 m = v1.max(v2);
我是否可以这样子?
看起来比较正常? m = max(v1,v2);
在这里
我们也许会尝试这样
//函数原型Vector max(Vector v1, Vector v2);//函数定义Vector max(Vector v1, Vector v2){ if ( (v1.x*v1.x + v1.y*v1.y) > (v2.x*v2.x + v2.y*v2.y) ) return v1; else return v2;}
可是!
这个函数在类的外部
不能够访问类的私有成员
而x,y,都是这个类的私有成员
怎么办?
这个时候,友元函数就用处大了
友元函数
提供一个非类成员函数访问类的私有成员的机会
关键字:friend
所以有如下函数声明与定义
//函数原型要放到类定义里面//函数原型friend Vector max(Vector v1, Vector v2);//函数定义Vector max(Vector v1, Vector v2){ if ( (v1.x*v1.x + v1.y*v1.y) > (v2.x*v2.x + v2.y*v2.y) ) return v1; else return v2;}
注意:max函数不是类成员函数,
所以函数定义的时候不需要类名+作用域解析运算符
函数的声明放在类定义中,且加上关键词friend
是给予了函数max访问私有成员的权限
最后
可能还不知道代码怎么放
下面还是一个多文件的实例
1. Vector.h
2. Vector.cpp
3. test.cpp
// Vector.h#ifdef VECTOR_H#define VECTOR_Hclass Vector{private: int x; int y;public: Vector() {};//默认构造函数 Vector(int a, int b);//自定义构造函数 ~Vector(){};//析构函数 void show() const; Vector max(Vector tv); bool operator< (vector tv); friend Vector max(Vector v1, Vector v2);};#endif
// Vector.cpp#include "Vector.h"#include <iostream>Vector::Vector(int a, int b){ x = a; y = b;}void Vector::show() const{ using namespace std; cout << x << "," << y << endl; return ;}Vector Vector::max(Vector tv){ if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y)) return tv; else return *this;}bool Vector::operator<(vector tv){ if ( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y) ) return true; else return false;}Vector max(Vector v1, Vector v2){ if ( (v1.x*v1.x + v1.y*v1.y) > (v2.x*v2.x + v2.y*v2.y) ) return v1; else return v2;}
//test.cpp#include "Vector.h"#include <iostream>int main(){ .... return 0;}
这里用了三种方式找到两个类对象的最大值
主要是想让大家了解到三种技术
1. this指针的使用
2. 运算符重载
3. 友元函数
- 继续实现Vector类
- 实现一 --------继续Thread类
- C++实现Vector类
- vector类的实现
- Vector类的实现
- ArrayList和Vector实现类
- C++实现的Vector类
- 集合:List实现类Vector
- 3.vector实现字符串类
- ArrayList和Vector实现类
- C++模板类实现Vector
- vector实现
- vector实现
- vector 实现
- vector实现
- 实现vector
- vector实现
- 自己实现的Vector类 这是Vector.h文件
- SPOJ Make Triangle 卡特兰数,递推 1月25日
- lampp下修改数据库的密码
- MySQL数据导出与导入
- hdu2222
- 在angular中 如何跳到指定的锚点($anchorScroll)
- 继续实现Vector类
- hrbust 1430 矩阵快速幂
- 设计模式-策略模式
- 很有用的 GCC 命令行选项
- 可变参数列表用法及原理
- ue4-定时器
- HackerRank Even Tree(树dp)
- 驱动框架_创建设备_写28个历程_写mfc应用程序进行通信
- The Knowledge of Linux-.-Day05