Chapter 7 operator overloading
1, Operator overload definition
-
Function type operator operator operator name (formal parameter table) {process operator}
-
For example, overload the +: Complex operator + (Complex & C1, Complex & C2) of Complex type
-
c3 = c1+c2 is equivalent to c3 = operator+(c1,c2)
-
Operators that cannot be overloaded
- . - member operator
- *-- member pointer operator
- :: -- field operator
- sizeof -- length operator
- ?:—— Conditional operator
-
When overloading an operator as a member function of a class, you can write one less parameter, but the left side must be the same as the operator function type
-
When the operator function is overloaded as a friend function, the parameters cannot be omitted, and the order of the parameters must be the same as the defined types
-
Operators that can only be overloaded as member functions of a class: =, [], (), - >
-
Operators that can only be overloaded as friend functions: <, > >
The idea of writing the project: first build the framework, gradually expand, from simple to complex, and finally improve. Debugging while programming
2, Overloaded unary operator
1. Overload auto increment + + operator
#include <iostream> using namespace std; class Time {public: Time(){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){} Time operator++(); Time operator++(int); void display(){cout<<minute<<":"<<sec<<endl;} private: int minute; int sec; }; Time Time::operator++() {if(++sec>=60) {sec-=60; ++minute;} return *this; } Time Time::operator++(int) {Time temp(*this); sec++; if(sec>=60) {sec-=60; ++minute;} return temp; } int main() {Time time1(34,59),time2; cout<<" time1 : "; time1.display(); ++time1; cout<<"++time1: "; time1.display(); time2=time1++; cout<<"time1++: "; time1.display(); cout<<" time2 : "; time2.display(); return 0; }
3, Overloaded flow operator
- Istream & operator > > (istream &, user-defined class);
- Ostream & operator < < (ostream &, user-defined class);
1. Overload stream insertion operator
#include <iostream.h> class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator + (Complex &c2); friend ostream& operator << (ostream&,Complex&); private: double real; double imag; }; Complex Complex::operator + (Complex &c2) {return Complex(real+c2.real,imag+c2.imag);} ostream& operator << (ostream& output,Complex& c) {output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl; return output; } int main() {Complex c1(2,4),c2(6,10),c3; c3=c1+c2; cout<<c3; return 0; }
- output is a reference to cout -- that is, an alias -- cout < < C3 is interpreted as operator < < (cout, C3)
Similarly, the same is true for overloaded stream extraction.
Overloaded operators can only use one operator for a specified class
4, Conversion between different data types
1. Conversion constructor
- Implicit and explicit conversion -- type name (data)
- Conversion constructor - if c = c1+2.5, because c1 is the type of Complex and overloads +, it cannot be added directly. First add Complex(2.5) and then add
- The conversion constructor can only have one parameter
2. Type conversion function
- Convert the object of one class to another data type -- and only as a member function. The body of the conversion is the object of the class
- operator () {statement implementing conversion} -- type cannot be specified in front of function name, and function has no parameters
#include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r){real=r;imag=0;} Complex(double r,double i){real=r;imag=i;} friend Complex operator + (Complex c1,Complex c2); void display(); private: double real; double imag; }; Complex operator + (Complex c1,Complex c2) {return Complex(c1.real+c2.real, c1.imag+c2.imag);} void Complex::display() {cout<<"("<<real<<"+"<<imag<<"i)"<<endl;} int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+2.5; c3.display(); return 0; }
Chapter VIII inheritance and derivation
1, Concepts of inheritance and derivation
1. Relevant knowledge
-
A subclass has multiple superclasses, and a superclass can have multiple subclasses
-
General form of derived class: class derived class name: [inheritance method] base class name {newly added member of derived class}
-
Private members of a private base class are not accessible in a derived class and can only be referenced through member functions.
2. Inheritance method and attribute summary after inheritance
Compilation mode | public | private | protected |
---|---|---|---|
public | public | X | protected |
private | private | X | private |
protected | protected | X | protected |
2, Constructors and destructors of derived classes
1. Simple destructor
- Derived class constructor name (total parameter table): base class constructor name (parameter table) {new data member initialization statement in derived class}
- Derived class constructor name (total parameter table): base class constructor name (parameter table), sub object name (parameter table) {new data member initialization statement in derived class}
- The following is the constructor of a derived class with a child object of the base class
Student(int n,string nam,int nl,string naml,int a,string ad):Student(n,nam),monitor(nl,naml){ age = a; addr=ad; }
- Execution sequence
- Call the base class constructor first
- Calling the constructor of a child object
- When calling the derived class constructor