函数重载顺序

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 函数重载顺序
  5. * */
  6. void f(int i) {
  7. cout<<i<<endl;
  8. }
  9. void f(const char *s) {
  10. cout<<s<<endl;
  11. }
  12. int main() {
  13. char c = 'A';
  14. int i = 1;
  15. short s = 2;
  16. double ff = 3.4;
  17. char a[10] = "123456789";
  18. f(c); // char->int 提升匹配
  19. f(i); // 精确匹配
  20. f(s); // short->int 提升匹配
  21. f(ff); // double->int 标准转换匹配
  22. f('a'); // char->int 提升匹配
  23. f(3); // 精确匹配
  24. f("string"); // 精确匹配
  25. f(a); // 精确匹配
  26. return 0;
  27. }

重载基类的成员函数,会影响基类成员函数在派生类中的可见性

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 重载基类的成员函数,会影响基类成员函数在派生类中的可见性
  5. * */
  6. class Base {
  7. int x;
  8. public:
  9. void setx(int i) {
  10. x = i;
  11. }
  12. void set(int n) {
  13. x = n;
  14. }
  15. void print() {
  16. printf("Base class x=%d\n", x);
  17. }
  18. };
  19. class D: public Base {
  20. int m, n;
  21. public:
  22. void set(int p, int k) {
  23. m = p;
  24. n = k;
  25. }
  26. void set(int i, int j, int k) {
  27. Base::set(i);
  28. m = j;
  29. n = k;
  30. }
  31. void print() {
  32. Base::print();
  33. printf("D class m=%d n=%d\n", m, n);
  34. }
  35. };
  36. int main() {
  37. D d;
  38. d.set(1, 3);
  39. d.print(); // 由于基类的 x 未初始化,因此是一个不确定的值
  40. d.set(5, 6, 7);
  41. d.print();
  42. // d.set(10); // 错误,set 被重载了,影响了基类成员函数在派生类中的可见性
  43. d.Base::print(); // 正确,由于 print 被派生类重写了,如果要调用基类的函数,需要加上“类名::”
  44. d.setx(8); // 正确,setx 没有被重载或者覆写,因此在派生类中是可见的
  45. d.Base::print();
  46. return 0;
  47. }

运算符重载

类 重载二元运算符

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 类 重载二元运算符
  5. * 成员函数重载加减,友元函数重载乘除
  6. * */
  7. class Complex {
  8. double r, i;
  9. public:
  10. Complex (double R=0, double I=0):r(R), i(I) {};
  11. Complex operator+(Complex b); // 非静态函数重载加减
  12. Complex operator-(Complex b);
  13. friend Complex operator*(Complex a, Complex b); // 友元函数重载乘除
  14. friend Complex operator/(Complex a, Complex b);
  15. void display();
  16. };
  17. Complex Complex::operator+(Complex b) {
  18. return Complex(r+b.r, i+b.i);
  19. }
  20. Complex Complex::operator-(Complex b) {
  21. return Complex(r-b.r, i-b.i);
  22. }
  23. Complex operator*(Complex a, Complex b) {
  24. Complex t;
  25. t.r = a.r*b.r - a.i*b.i;
  26. t.i = a.r*b.i + b.r*a.i;
  27. return t;
  28. }
  29. Complex operator/(Complex a, Complex b) {
  30. Complex t;
  31. double x;
  32. x = 1/(b.r*b.r + b.i*b.i);
  33. t.r = x * (a.r * b.r + a.i * b.i);
  34. t.i = x * (a.i * b.r - a.r * b.i);
  35. return t;
  36. }
  37. void Complex::display() {
  38. cout<<r;
  39. if(i > 0) cout<<"+";
  40. if(i != 0) cout<<i<<"i"<<endl;
  41. }
  42. int main() {
  43. Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6;
  44. c1.display();
  45. c2.display();
  46. c3 = c1+c2;
  47. c3.display();
  48. c4 = c1-c2;
  49. c4.display();
  50. c5 = c1 * c2;
  51. c5.display();
  52. c6 = c1/c2;
  53. c6.display();
  54. c1.operator+(c2); // 成员函数显示调用
  55. operator*(c1, c2); // 友元函数显示调用
  56. return 0;
  57. }

友元函数重载二元运算符

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 友元函数重载二元运算符
  5. * 因为友元函数第一个参数默认是 this 指针,不会进行隐式类型转换
  6. * */
  7. class Complex {
  8. double r, i;
  9. public:
  10. Complex(double R=0, double I=0): r(R), i(I) {};
  11. // friend Complex operator+(Complex a, double b) {
  12. // return Complex(a.r+b, a.i);
  13. // }
  14. // friend Complex operator+(double a, Complex b) {
  15. // return Complex(a+b.r, b.i);
  16. // }
  17. friend Complex operator+(Complex a, Complex b) { // 包含了隐式类型转换
  18. return Complex(a.r+b.r, a.i+b.i);
  19. }
  20. void display() {
  21. cout<<r;
  22. if(i > 0) cout<<"+";
  23. if(i != 0) cout<<i<<"i"<<endl;
  24. }
  25. };
  26. int main() {
  27. Complex c1(1, 2), c2;
  28. c2 = c1 + 5;
  29. c2.display();
  30. c2 = 5 + c1;
  31. c2.display();
  32. return 0;
  33. }

成员函数重载一元运算符

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 一元运算符重载
  5. * 成员函数
  6. * */
  7. class Time {
  8. int h, m, s;
  9. public:
  10. Time(int hour, int minute, int second);
  11. Time operator++();
  12. void display();
  13. };
  14. Time::Time(int hour, int minute, int second) {
  15. h = hour;
  16. m = minute;
  17. s = second;
  18. if(hour >= 24) h = 0;
  19. if(minute >= 60) m = 0;
  20. if(second >= 60) s = 0;
  21. }
  22. Time Time::operator++() {
  23. ++s;
  24. if(s >= 60) {
  25. s = 0;
  26. ++m;
  27. if(m >= 60) {
  28. m = 0;
  29. if(h >= 24) {
  30. h = 0;
  31. }
  32. }
  33. }
  34. }
  35. void Time::display() {
  36. printf("hour:%d minute:%d second:%d\n", h, m, s);
  37. }
  38. int main() {
  39. Time t1(23, 59, 59);
  40. t1.display();
  41. ++t1; // 隐式调用
  42. t1.display();
  43. t1.operator++(); // 显式调用
  44. t1.display();
  45. return 0;
  46. }

友元函数重载一元运算符

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 一元运算符重载
  5. * 友元函数
  6. * */
  7. class Time {
  8. int h, m, s;
  9. public:
  10. Time(int hour, int minute, int second);
  11. friend Time operator++(Time &t);
  12. void display();
  13. };
  14. Time::Time(int hour, int minute, int second) {
  15. h = hour;
  16. m = minute;
  17. s = second;
  18. if(hour >= 24) h = 0;
  19. if(minute >= 60) m = 0;
  20. if(second >= 60) s = 0;
  21. }
  22. Time operator++(Time &t) {
  23. ++t.s;
  24. if(t.s >= 60) {
  25. t.s = 0;
  26. ++t.m;
  27. if(t.m >= 60) {
  28. t.m = 0;
  29. if(t.h >= 24) {
  30. t.h = 0;
  31. }
  32. }
  33. }
  34. return t;
  35. }
  36. void Time::display() {
  37. printf("hour:%d minute:%d second:%d\n", h, m, s);
  38. }
  39. int main() {
  40. Time t1(23, 59, 59);
  41. t1.display();
  42. ++t1; // 隐式调用
  43. t1.display();
  44. operator++(t1); // 显式调用
  45. t1.display();
  46. return 0;
  47. }

自增增减运算符重载

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 自增自减运算符重载
  5. * */
  6. class Counter {
  7. int n;
  8. public:
  9. Counter(int i = 0) {
  10. n = i;
  11. }
  12. Counter operator++(); // 成员函数,前缀
  13. Counter operator++(int); // 成员函数,后缀
  14. friend Counter operator--(Counter &c); // 友元函数,前缀
  15. friend Counter operator--(Counter &c, int); // 友元函数,后缀
  16. void display();
  17. };
  18. Counter Counter::operator++() {
  19. ++n;
  20. return *this;
  21. }
  22. Counter Counter::operator++(int) {
  23. n++;
  24. return *this;
  25. }
  26. Counter operator--(Counter &c) {
  27. --c.n;
  28. return c;
  29. }
  30. Counter operator--(Counter &c, int) {
  31. c.n--;
  32. return c;
  33. }
  34. void Counter::display() {
  35. printf("Counter number=%d\n", n);
  36. }
  37. int main() {
  38. Counter a;
  39. ++a;
  40. a.display();
  41. a++;
  42. a.display();
  43. --a;
  44. a.display();
  45. a--;
  46. a.display();
  47. return 0;
  48. }

赋值运算符重载

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 赋值运算符重载
  5. * 类的赋值运算函数只能为成员函数,不能为友元
  6. * 类的赋值运算函数不能被继承
  7. * */
  8. class String {
  9. char *ptr;
  10. int n;
  11. public:
  12. String(char *s, int a) {
  13. ptr = new char[strlen(s)+1];
  14. strcpy(ptr, s);
  15. n = a;
  16. }
  17. ~String() {
  18. delete ptr;
  19. }
  20. void print() {
  21. cout<<ptr<<endl;
  22. }
  23. String & operator=(const String &s) ;
  24. };
  25. String & String::operator=(const String &s) {
  26. if(this == &s) return *this;
  27. delete ptr;
  28. ptr = new char[strlen(s.ptr)+1];
  29. strcpy(ptr, s.ptr);
  30. return *this;
  31. }
  32. int main() {
  33. String p1 ("hello world", 12);
  34. {
  35. String p2("chong qing", 10);
  36. p2 = p1;
  37. cout<<"p2:";
  38. p2.print();
  39. }
  40. cout<<"p1:";
  41. p1.print(); // 错误,指针悬挂
  42. // 没有显示赋值,调用默认进行对象赋值,因此 p2 结束调用析构释放了空间,导致 p1 的指针悬挂
  43. return 0;
  44. }

构造函数将标准类型转为类对象

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 构造函数将标准类型转为类对象
  5. * */
  6. class Date {
  7. int year, month, day;
  8. public:
  9. Date(int yy=1900, int mm=1, int dd=1) {
  10. year = yy;
  11. month = mm;
  12. day = dd;
  13. }
  14. void show() {
  15. printf("year=%d month=%d day=%d\n", year, month, day);
  16. }
  17. };
  18. int main() {
  19. Date d(2000, 10, 11);
  20. d.show();
  21. d = 2006; // 调用Date 的构造函数
  22. d.show();
  23. return 0;
  24. }

类转为其他类型

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 类型转换
  5. * 类转为其他类型
  6. * */
  7. class Circle {
  8. double x, y, r;
  9. public:
  10. Circle(double x1, double y1, double r1) {
  11. x = x1;
  12. y = y1;
  13. r = r1;
  14. }
  15. operator int() {
  16. return int(r);
  17. }
  18. operator double() {
  19. return 2*3.14*r;
  20. }
  21. operator float() {
  22. return (float)3.14*r*r;
  23. }
  24. };
  25. int main() {
  26. Circle c(2.3, 3.4, 2.5);
  27. int r = c;
  28. double length = c;
  29. float area = c;
  30. double len = (double)c;
  31. cout<<r<<endl;
  32. cout<<length<<endl;
  33. cout<<len<<endl;
  34. cout<<area<<endl;
  35. return 0;
  36. }

重载输入输出

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 重载输入输出
  5. * */
  6. class Sales {
  7. char name[10];
  8. char id[18];
  9. int age;
  10. public:
  11. Sales(char *Name, char *Id, int Age);
  12. friend ostream &operator<<(ostream &os, const Sales &s);
  13. friend istream &operator>>(istream &is, Sales &s);
  14. };
  15. Sales::Sales(char *Name, char *Id, int Age) {
  16. strcpy(name, Name);
  17. strcpy(id, Id);
  18. age = Age;
  19. }
  20. ostream &operator<<(ostream &os, const Sales &s) {
  21. cout<<s.name<<"\t";
  22. cout<<s.id<<"\t";
  23. cout<<s.age<<endl;
  24. }
  25. istream &operator>>(istream &is, Sales &s) {
  26. cout<<"please input employee name, id and age"<<endl;
  27. is>>s.name>>s.id>>s.age;
  28. }
  29. int main() {
  30. Sales s("console", "45564646456", 19);
  31. cout<<s<<endl;
  32. cin>>s;
  33. cout<<s;
  34. return 0;
  35. }