1)括号运算符()重载
2)【面试题】&&, || 能不能做 操作符重载?
3)运算符极致练习:
【提高】运算符重载
括号运算符()重载
chunli@Linux:~/c++$ cat main.cpp #include <iostream> using namespace std; class A { public: A(int a,int b) { this->a = a; this->b = b; } int operator()(int a,int b) { return a + b + this->a + this->b; } int a; int b; }; int main() { A a1(1,2); cout << a1(2,1) << endl; return 0; } chunli@Linux:~/c++$ g++ -Wall -g -o run main.cpp && ./run 6
【面试题】逻辑与 逻辑或能不能做 操作符重载?
答案:
可以。但是无论怎么样也无法实现短路功能!达不到逻辑与,或的运算效果
所以一般情况下不建议使用这个运算符重载
【注意】&& || 结合性:自左向右
实现代码:chunli@Linux:~/c++$ cat main.cpp #include <iostream> using namespace std; class A { public: A(int a = 0) { this->a = a; } bool operator&&(A a) { cout << "&& 运算符重载 被调用\n"; if(this->a && a.a) { return true; } return false; } bool operator||(A a) { cout << "|| 运算符重载 被调用\n"; if(this->a || a.a ) { return true; } return false; } A& operator+(A a) { cout << "+ 运算符重载 被调用\n"; this->a += a.a; return *this; } int a; }; int main() { A a1(1); A a2(0); if(a1 && (a1 + a2) ) { cout << "-----------------运算结果:与运算 true "<< endl; } else { cout << "-----------------运算结果:与运算 false "<< endl; } A a3(0); A a4(0); if(a3 || (a3 + a4) ) { cout << "-----------------运算结果:或运算 true "<< endl; } else { cout << "-----------------运算结果:或运算 false "<< endl; } return 0; } chunli@Linux:~/c++$ g++ -Wall -g -o run main.cpp && ./run + 运算符重载 被调用 && 运算符重载 被调用 -----------------运算结果:与运算 true + 运算符重载 被调用 || 运算符重载 被调用 -----------------运算结果:或运算 false chunli@Linux:~/c++$
运算符极致练习:
= 运算符重载,对象链式赋值
<< 运算符重载,对象字符内容链式输出
>>运算符重载,成员变量链式输入
==运算符重载,判断两个Str对象是否相同
!=运算符重载,判断两个Str对象是否不同
>= > 运算符重载,判断两个Str对象大小
<= < 运算符重载,判断两个Str对象大小
要满足的测试程序:
Str s1; s1.print();
Str s2("我的树莓派"); s2.print();
Str s3 = NULL; s3.print();
Str s4 = s2; s4.print();
Str s5 = "数据结构 + 算法 = 程序"; s5.print();
Str s6("2016 年的夏天不太热");
s6[4] = ‘?‘; s6.print();
cout << s6[4] << "\n";
cout << s6 << s6;
cin >> s3;
chunli@Linux:~$ cat main.cpp #include <iostream> #include <string.h> using namespace std; class Str { friend ostream& operator<<(ostream &put,Str &from); friend istream& operator>>(istream &in,Str &from); public: Str(const char *p = NULL) { if(p == NULL) { this->p = NULL; this->len = 0; } else { this->len = strlen(p); this->p = new char[this->len +1 ]; strncpy(this->p,p,this->len); this->p[this->len] = ‘\0‘; } } Str(const Str &from) { //cout << from.len << endl; this->len = from.len; this->p = new char[this->len +1]; strncpy(this->p,from.p,this->len); this->p[this->len] = ‘\0‘; } ~Str() { if(this->p != NULL) { delete [] this->p; this->p = NULL; this->len = 0; } } void print() { if(this->p != NULL) { cout << this->get_str() << endl; } } int get_len() { int n = this->len; return n; } char* get_str() { return this->p; } Str& operator=(char *p) { if(this->p != NULL) { delete []this->p; } if(p != NULL) { this->len = strlen(p); this->p = new char[this->len + 1]; strncpy(this->p,p,this->len); this->p[this->len] = ‘\0‘; } else { this->p = NULL; this->len = 0; } return *this; } char& operator[](int i) { return this->p[i]; } private: int len; char *p; }; istream& operator>>(istream &in,Str &from) { in>>from.len;//如果这里是指针,运行会出错,建议完善一下 return in; } ostream& operator<<(ostream &put,Str &from) { put<< from.p << endl; return put; } int main() { Str s1; s1.print(); Str s2("我的树莓派"); s2.print(); Str s3 = NULL; s3.print(); Str s4 = s2; s4.print(); Str s5 = "数据结构 + 算法 = 程序"; s5.print(); Str s6("2016 年的夏天不太热"); s6[4] = ‘?‘; s6.print(); cout << s6[4] << "\n"; cout << s6 << s6; cin >> s3; // ==运算符重载,判断两个Str对象是否相同 // !=运算符重载,判断两个Str对象是否不同 // >= > 运算符重载,判断两个Str对象大小 // <= < 运算符重载,判断两个Str对象大小 return 0; } chunli@Linux:~$ g++ -g -o run main.cpp && ./run 我的树莓派 我的树莓派 数据结构 + 算法 = 程序 2016? 年的夏天不太热 ? 2016? 年的夏天不太热 2016? 年的夏天不太热 3 chunli@Linux:~$
本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1795649
原文地址:http://990487026.blog.51cto.com/10133282/1795649