C++ Primer Plus Notes (Chapter 11)

1. operator+(): overloaded + operator operator* (): overloaded * operator makes it possible to directly perform + or * operations on class objects

Assuming that district, sid, sara are objects of class Salaperson, you can write:

district=sid+sara;// is equivalent to the next sentence, both of which call the operator+() method, sid is the calling object, and sara is passed as a parameter.

district=sid.operator+(sara);

2. The overloaded operator does not have to be a member function, but at least one operand must be a user-defined type. (Some operators can only be overloaded through member functions =, () [] ->)

假设Time类 class Time{ privates: int hours; int minutes;  public: ...........};

3. Friends : friend function, friend class, friend member function. Can access the private data of the class

(To realize the commutative law of addition or multiplication, you must use friend functions to overload addition or multiplication.)

4. (1) Create a friend function : the prototype is placed in the class declaration , but it is not a member function , and the keyword friend 
friend Time operator* (double m,const Time & t); //The return value is Time class

//Although it is placed in the class declaration, it is not a member function and cannot be called using the member operator period , but it has the same permissions as member functions

(2) Define the friend function: do not use the class qualifier :: , do not add the keyword friend when defining

Time operator* (double m,const Time & t){...........}

5. If you want to overload the operator for the class, and use the non-class item as its first operand, you can use the friend function to reverse the order of the operands

6. Overload the << operator:

(1) Use the first kind of friend function:

void operator<<(ostream &os, const Time &t){

os<<t,hours<<"hours, "<<t.minutes<<" minutes.";} //cout is an object of the ostream class, and the cout object is referenced through os. The cout itself should be referenced, and // is not a copy of it. The function passes parameters by reference (rather than by value). When a new object created is returned, it should not be returned by reference.

When trip is a Time object, you can use cout<<trip; to output 4 hours, 25 minutes

(2) Use the second kind of friend function, which can also be used to write output to a file

ostream & operator<<(ostream &os, const Time &t){ //Compared with the first, there is a return type ostream &

os<<t,hours<<"hours, "<<t.minutes<<" minutes.";

return os; }

可以实现:cout<<"Trip time: "<<trip<<" (Tuesday)\n";

7. When the code of a member function or friend function is very short, the declaration and definition can be carried out at the same time and written in the class declaration, so that the function becomes an inline function.

8 For many operators, you can choose to use member functions or non-member functions to implement overloaded operations. Such as:

Time operator+(const Time &t) const; //Member function

friend Time operator+( const Time &t1, const Time &t2 ); //Non-member function 

8. When the member function and friend function of the class vector are defined in the name space VECTOR, when the friend function uses the constant REC in the class, vector::REC

When declaring an object externally, vector folly(20,30, VECTOR::vector::REC);

9. Automatic conversion of classes: only constructors that accept one parameter can be used as conversion functions.

class Stonewt
{ 
private: 
  enum {Lbs_per_stn=14};
  int stone;
  double pds_left;
  double pounds;
public:
  Stonewt(double lbs);
  Stonewt(int stn,double lbs);
  Stonewt();
  ~Stonewt();
};

(1) Convert the int type to the class Stonewt type: ( the class constructor with only one parameter is used to convert the value of the same type as the parameter to the class type )

Stonewt wall=325.25; //Use Stonewt(double) to set all 3 members, assign the double type to the class object, and convert it to the class type

Stonewt taft=325; //int is converted to double first, and then Stonewt(double) is used to set all 3 members.

(2) Convert the class type to other types: a conversion function is required , that is, a user-defined mandatory type conversion. The conversion function is a class member, has no return type, no parameters, and is named operator typeName(), where typeName is the type that the object will be converted into.

Stonewt wolfe(285.7);

double host=double (wolfe); //#1  or    double host=(double)  wolfe; //#2

To implement #1 and #2, you need to declare the function prototype converted to double type in the method of the class: operator double() const; //Conversion function

Stonewt walls(20,3);

double star=walls; // The right side is the class type, the left side is the double, the compiler will first check whether there is a matching conversion function, if there is an implicit call, the compilation is successful, if there is no error

There is no need for the compiler to automatically find a matching compiled function for automatic conversion. You can use the keyword explicit to force conversion:

  explicit operator int() const; //Only called explicitly

10. The conversion function can be replaced with a non-conversion function:

Stonewt::operator int() {return int (pounds+0.5);} Call statement: int plb=walls;

Replace with:

int  Stonewt::Stone_to_Int()  {return int (pounds+0.5); }

The calling statement is: int plb=walls.Stone_to_Int();

 

 

 

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/86139452