函数重载顺序
#include<bits/stdc++.h>using namespace std;/** * 函数重载顺序 * */void f(int i) { cout<<i<<endl;}void f(const char *s) { cout<<s<<endl;}int main() { char c = 'A'; int i = 1; short s = 2; double ff = 3.4; char a[10] = "123456789"; f(c); // char->int 提升匹配 f(i); // 精确匹配 f(s); // short->int 提升匹配 f(ff); // double->int 标准转换匹配 f('a'); // char->int 提升匹配 f(3); // 精确匹配 f("string"); // 精确匹配 f(a); // 精确匹配 return 0;}
重载基类的成员函数,会影响基类成员函数在派生类中的可见性
#include<bits/stdc++.h>using namespace std;/** * 重载基类的成员函数,会影响基类成员函数在派生类中的可见性 * */class Base { int x; public: void setx(int i) { x = i; } void set(int n) { x = n; } void print() { printf("Base class x=%d\n", x); }};class D: public Base { int m, n; public: void set(int p, int k) { m = p; n = k; } void set(int i, int j, int k) { Base::set(i); m = j; n = k; } void print() { Base::print(); printf("D class m=%d n=%d\n", m, n); }};int main() { D d; d.set(1, 3); d.print(); // 由于基类的 x 未初始化,因此是一个不确定的值 d.set(5, 6, 7); d.print(); // d.set(10); // 错误,set 被重载了,影响了基类成员函数在派生类中的可见性 d.Base::print(); // 正确,由于 print 被派生类重写了,如果要调用基类的函数,需要加上“类名::” d.setx(8); // 正确,setx 没有被重载或者覆写,因此在派生类中是可见的 d.Base::print(); return 0;}
运算符重载
类 重载二元运算符
#include<bits/stdc++.h>using namespace std;/** * 类 重载二元运算符 * 成员函数重载加减,友元函数重载乘除 * */class Complex { double r, i;public: Complex (double R=0, double I=0):r(R), i(I) {}; Complex operator+(Complex b); // 非静态函数重载加减 Complex operator-(Complex b); friend Complex operator*(Complex a, Complex b); // 友元函数重载乘除 friend Complex operator/(Complex a, Complex b); void display();};Complex Complex::operator+(Complex b) { return Complex(r+b.r, i+b.i);}Complex Complex::operator-(Complex b) { return Complex(r-b.r, i-b.i);}Complex operator*(Complex a, Complex b) { Complex t; t.r = a.r*b.r - a.i*b.i; t.i = a.r*b.i + b.r*a.i; return t;}Complex operator/(Complex a, Complex b) { Complex t; double x; x = 1/(b.r*b.r + b.i*b.i); t.r = x * (a.r * b.r + a.i * b.i); t.i = x * (a.i * b.r - a.r * b.i); return t;}void Complex::display() { cout<<r; if(i > 0) cout<<"+"; if(i != 0) cout<<i<<"i"<<endl;}int main() { Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6; c1.display(); c2.display(); c3 = c1+c2; c3.display(); c4 = c1-c2; c4.display(); c5 = c1 * c2; c5.display(); c6 = c1/c2; c6.display(); c1.operator+(c2); // 成员函数显示调用 operator*(c1, c2); // 友元函数显示调用 return 0;}
友元函数重载二元运算符
#include<bits/stdc++.h>using namespace std;/** * 友元函数重载二元运算符 * 因为友元函数第一个参数默认是 this 指针,不会进行隐式类型转换 * */class Complex { double r, i;public: Complex(double R=0, double I=0): r(R), i(I) {}; // friend Complex operator+(Complex a, double b) { // return Complex(a.r+b, a.i); // } // friend Complex operator+(double a, Complex b) { // return Complex(a+b.r, b.i); // } friend Complex operator+(Complex a, Complex b) { // 包含了隐式类型转换 return Complex(a.r+b.r, a.i+b.i); } void display() { cout<<r; if(i > 0) cout<<"+"; if(i != 0) cout<<i<<"i"<<endl; }};int main() { Complex c1(1, 2), c2; c2 = c1 + 5; c2.display(); c2 = 5 + c1; c2.display(); return 0;}
成员函数重载一元运算符
#include<bits/stdc++.h>using namespace std;/** * 一元运算符重载 * 成员函数 * */class Time { int h, m, s;public: Time(int hour, int minute, int second); Time operator++(); void display();};Time::Time(int hour, int minute, int second) { h = hour; m = minute; s = second; if(hour >= 24) h = 0; if(minute >= 60) m = 0; if(second >= 60) s = 0;}Time Time::operator++() { ++s; if(s >= 60) { s = 0; ++m; if(m >= 60) { m = 0; if(h >= 24) { h = 0; } } }}void Time::display() { printf("hour:%d minute:%d second:%d\n", h, m, s);}int main() { Time t1(23, 59, 59); t1.display(); ++t1; // 隐式调用 t1.display(); t1.operator++(); // 显式调用 t1.display(); return 0;}
友元函数重载一元运算符
#include<bits/stdc++.h>using namespace std;/** * 一元运算符重载 * 友元函数 * */class Time { int h, m, s;public: Time(int hour, int minute, int second); friend Time operator++(Time &t); void display();};Time::Time(int hour, int minute, int second) { h = hour; m = minute; s = second; if(hour >= 24) h = 0; if(minute >= 60) m = 0; if(second >= 60) s = 0;}Time operator++(Time &t) { ++t.s; if(t.s >= 60) { t.s = 0; ++t.m; if(t.m >= 60) { t.m = 0; if(t.h >= 24) { t.h = 0; } } } return t;}void Time::display() { printf("hour:%d minute:%d second:%d\n", h, m, s);}int main() { Time t1(23, 59, 59); t1.display(); ++t1; // 隐式调用 t1.display(); operator++(t1); // 显式调用 t1.display(); return 0;}
自增增减运算符重载
#include<bits/stdc++.h>using namespace std;/** * 自增自减运算符重载 * */class Counter { int n;public: Counter(int i = 0) { n = i; } Counter operator++(); // 成员函数,前缀 Counter operator++(int); // 成员函数,后缀 friend Counter operator--(Counter &c); // 友元函数,前缀 friend Counter operator--(Counter &c, int); // 友元函数,后缀 void display();};Counter Counter::operator++() { ++n; return *this;}Counter Counter::operator++(int) { n++; return *this;}Counter operator--(Counter &c) { --c.n; return c;}Counter operator--(Counter &c, int) { c.n--; return c;}void Counter::display() { printf("Counter number=%d\n", n);}int main() { Counter a; ++a; a.display(); a++; a.display(); --a; a.display(); a--; a.display(); return 0;}
赋值运算符重载
#include<bits/stdc++.h>using namespace std;/** * 赋值运算符重载 * 类的赋值运算函数只能为成员函数,不能为友元 * 类的赋值运算函数不能被继承 * */class String { char *ptr; int n;public: String(char *s, int a) { ptr = new char[strlen(s)+1]; strcpy(ptr, s); n = a; } ~String() { delete ptr; } void print() { cout<<ptr<<endl; } String & operator=(const String &s) ;};String & String::operator=(const String &s) { if(this == &s) return *this; delete ptr; ptr = new char[strlen(s.ptr)+1]; strcpy(ptr, s.ptr); return *this;}int main() { String p1 ("hello world", 12); { String p2("chong qing", 10); p2 = p1; cout<<"p2:"; p2.print(); } cout<<"p1:"; p1.print(); // 错误,指针悬挂 // 没有显示赋值,调用默认进行对象赋值,因此 p2 结束调用析构释放了空间,导致 p1 的指针悬挂 return 0;}
构造函数将标准类型转为类对象
#include<bits/stdc++.h>using namespace std;/** * 构造函数将标准类型转为类对象 * */class Date { int year, month, day;public: Date(int yy=1900, int mm=1, int dd=1) { year = yy; month = mm; day = dd; } void show() { printf("year=%d month=%d day=%d\n", year, month, day); }};int main() { Date d(2000, 10, 11); d.show(); d = 2006; // 调用Date 的构造函数 d.show(); return 0;}
类转为其他类型
#include<bits/stdc++.h>using namespace std;/** * 类型转换 * 类转为其他类型 * */class Circle { double x, y, r;public: Circle(double x1, double y1, double r1) { x = x1; y = y1; r = r1; } operator int() { return int(r); } operator double() { return 2*3.14*r; } operator float() { return (float)3.14*r*r; }};int main() { Circle c(2.3, 3.4, 2.5); int r = c; double length = c; float area = c; double len = (double)c; cout<<r<<endl; cout<<length<<endl; cout<<len<<endl; cout<<area<<endl; return 0;}
重载输入输出
#include<bits/stdc++.h>using namespace std;/** * 重载输入输出 * */class Sales { char name[10]; char id[18]; int age;public: Sales(char *Name, char *Id, int Age); friend ostream &operator<<(ostream &os, const Sales &s); friend istream &operator>>(istream &is, Sales &s); };Sales::Sales(char *Name, char *Id, int Age) { strcpy(name, Name); strcpy(id, Id); age = Age;}ostream &operator<<(ostream &os, const Sales &s) { cout<<s.name<<"\t"; cout<<s.id<<"\t"; cout<<s.age<<endl;}istream &operator>>(istream &is, Sales &s) { cout<<"please input employee name, id and age"<<endl; is>>s.name>>s.id>>s.age;}int main() { Sales s("console", "45564646456", 19); cout<<s<<endl; cin>>s; cout<<s; return 0;}