程序设计实习运算符重载内容摘要:
// string length char *sPtr。 // pointer to start of string }。 26 运算符重载为友员函数 实现单目运算: op operand operand是类 A的对象 op应该重载为 A的友员函数,该函数有一个参数 friend return_type operator op(A arg) return_type是 op operand的类型 例如: ! string_s,等价于 operator!(string_s) class String { friend bool operator!(const String amp。 )。 // is String empty? public: String( const char * = )。 // conversion/default ctor ~String()。 // destructor private: int length。 // string length char *sPtr。 // pointer to start of string }。 bool operator!(const String amp。 s) { return == 0。 } 27 运算符重载为友员函数 实现双目运算: operand_1 op operand_2 op被重载为 A的友员函数,该函数有两个参数 friend return_type operator op(argT1 arg1, argT2 arg2 ) return_type是 operand_1 op operand_2的类型 argT1是 operand_1的类型 argT2是 operand_2的类型 operand_1和 operand_2中 至少有一个 是类型为 A的对象 如果 operand_1是类 A的对象,则 argT1为 A 在函数 operator op(argT1 arg1, argT2 arg2 )的函数体中,可以访问类arg1的任何数据成员 如果 operand_2是类 A的对象,则 argT2为 A 在函数 operator op(argT1 arg1, argT2 arg2 )的函数体中,可以访问类arg2的任何数据成员 将双目运算符 op重载为类 A的友员函数时,可以:以非类 A的对象作为 op所在表达式的左操作数 28 流输入输出运算符的重载 cout 5 “this”。 为什么能够成立。 cout是什么。 “ ” 为什么能用在 cout上。 29 cout 是在 iostream中定义的, ostream 类的对象 “ ” 能用在 cout 上是因为 在 “ ” 进行了重载 考虑 ,怎么重载才能使得 cout 5。 和 cout “this”都能成立。 流输入输出运算符的重载 30 void operator( const ostream amp。 o, int n){ Output( n)。 } 假定 Output 是一个能将整数n输出到屏幕上的函数,至于其内部怎么实现,不必深究 void operator( const ostream amp。 o, const char * s){ Output(s)。 } 流输入输出运算符的重载 31 怎么重载才能使得 cout 5 “this”。 能成立。 流输入输出运算符的重载 32 只需要将重载运算符的返回值设为引用 ostream amp。 operator( const ostream amp。 o, int n){ Output( n)。 return o。 } ostream amp。 operator( const ostream amp。 o, const char * s) { Output(s)。 return o。 } 流输入输出运算符的重载 33 引用 某个变量的引用,和这个变量是一回事,相当于该变量的一个别名。 void swap( int amp。 a, int amp。 b) { int tmp。 tmp = a。 a = b。 b = tmp。 } int n1, n2。 swap(n1,n2)。 // n1,n2的值被交换 34 引用 函数的返回值可以是引用 ,如: include iostream using namespace std。 int n = 4。 int amp。 SetValue() { return n。 } int main() { SetValue() = 40。 cout n。 return 0。 } 该程序输出结果是 40 35 const 引用类型参数 class Complex。 Complex Add ( Complex c1, Complex c2)。 Complex Add ( const Complex amp。 c1, const Complex amp。 c2)。 const 代表参数的值在函数内部不能被修改 两种函数定义方式,后者比前者好在哪里呢。 后者的好处是避免了生成参数对象的过程,节省时间,空间开销。 36 对象指针和对象引用作函数的参数 对象作为函数的参数 值传递,但需进行对象副本的拷贝 对象指针作函数的参数 使用对象指针作为函数参数要比使用对象作函数参数更普遍一些,有如下两点好处: (1) 实现传址调用。 可在被调用函数中改变调用函数的参数对象的值,实现函数之间的信息传递。 (2) 使用对象指针实参仅将对象的地址值传给形参,而不进行副本的拷贝,这样可以提高运行效率,减少时空开销。 当形参是指向对象指针时,调用函数的对应实参应该是 某个 对象的地址值 ,一般使用 amp。 后加对象名。 include iostream using namespace std。 class M{ public: M() { x=y=0。 } M(int i, int j) { x=i。 y=j。 } void copy(M *m)。 void setxy(int i, int j) { x=i。 y=j。 } void print() { coutx,yendl。 } private: int x, y。 }。 void M::copy(M *m){ x=mx。 y=my。 } void fun(M m1, M *m2)。 int main() { M p(5, 7), q。 (amp。 p)。 fun(p, amp。 q)。 ()。 ()。 return 0。 } void fun(M m1, M *m2){ (12, 15)。 m2setxy(22,25)。 } 输出结果为: 5,7 22,25 38 对象指针和对象引用作函数的参数 对象引用作函数参数 在实际中,使用对象引用作函数参数要比使用对象指针作函数更普遍 使用对象引用作函数参数具有用对象指针作函数参数的优点 用对象引用作函数参数将更简单,更直接。 include iostream using namespace std。 class M { public: M() { x=y=0。 } M(int i, int j) { x=i。 y=j。 } void copy(M amp。 m)。 void setxy(int i, int j) { x=i。 y=j。 } void print() {coutx,yendl。 } private: int x, y。 }。 void M::copy(M amp。 m){ x=。 x=。 } void fun(M m1, M amp。 m2)。 int main(){ M p(5, 7), q。 (p)。 fun(p, q)。 ()。 ()。 return 0。 } void fun(M m1, M amp。 m2){ (12, 15)。 (22, 25)。 } 40 假定下面程序输出为 5, 请问该补写些什么。 include ios。程序设计实习运算符重载
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。