1. Increment and decrement operators
Unary operators: plus sign (+) minus sign (-) ++ --
Start code:
#include <iostream>
using namespace std;
class point
{
private:
int x;
int y ;
public:
int getX(){ return x;}
void setX(int x) { this->x = x;}
int gety(){ return y;}
void sety(int y) { this->y = y;}
public:
point(int x = 0 ,int y = 0);
point operator=(const point& other);
point operator+=(const point& other);
int operator[](int x);
void operator()(int a,int b);
// 前加加
// point operator++();
//后加加
// point operator++(int);
};
point::point(int x,int y)
:x(x),y(y)
{
}
Global function implementation (former ++): student operator++(const stduent& other);
Member function implementation (former ++): point point::operator++();
//前加加运算符
point operator++(point& other)
{
other.setX(other.getX()+1);
other.sety(other.gety()+1);
return other;
}
//前++(成员函数)
point point::operator++()
{
this->x++;
this->y++;
return *this;
}*
Global function implementation (after ++): student operator++(const stduent& other , int );
Member function implementation (post ++): point point::operator++( int );
Global functions:
//后加加运算符
point operator++(point& other,int)
{
point temp = other;
other.setX(other.getX()+1);
other.sety(other.gety()+1);
return temp;
}
//后加加实现(成员函数)
point point::operator++(int)
{
point temp = *this;
this->x++;
this->y++;
return temp;
}
Note: By default, the addition operator is pre-addition, which is ++danny;
2. Assignment operator
Form 1: void operator=(const person& other);
Form 2: void operator+=(const person& ohter);
Case: Assignment operators can be used consecutively
//赋值运算符
point point::operator=(const point& other)
{
this->x = other.x;
this->y = other.y;
return *this;
}
Description of the main function:
point* px = new point(14,14);
++(*px);
cout << "px->x"<< px->getX()<<endl;
point ptemp = (*px)++; //拷贝构造
point danny(1,2);
ptemp = (*px); //赋值运算符 x=y=z;
cout << "px->x"<< ptemp.getX()<<endl;
danny = ptemp = (*px);
cout << "danny.x" << danny.getX()<<endl;
Note: Assignment operators can only be member function implementations , not global functions and friend functions
If no assignment operator is added manually, the compiler automatically adds a default assignment operator.
3. Subscript operator
Format: int operator[](int x)
case:
//下标运算符
int point::operator[](int x)
{
if(x >= 2 || x < 0) { return y; }
else if( x == 1) { return y; }
else { return x; }
}
Main function call:
cout << "px->x"<< ptemp.getX()<<endl;
danny = ptemp = (*px);
cout << "danny.x" << danny.getX()<<endl;
cout << "danny[1]" << danny[1] <<endl;
Note: it can only be a member function implementation
Four. stream operator
Definition: When using cout or cin, the << and >> operators are used, which are stream operators
Object: cout is the output object, which has been defined and is an ostream type object
cin is the input object, which has been defined and is the type object of istream
Format 1: ostream& operator<<(ostream& out,student& other); output stream
Format 2: istream& operator>>(istream& in,point& other) input stream
Note: Only global functions can implement stream operators, not member functions and friend functions
case:
ostream& operator<<(ostream& out,point& other)
{
out << "x="<<other.getX()<<" y = "<<other.gety()<<endl;
return out;
}
istream& operator>>(istream& in,point& other)
{
int x=0;
int y=0;
in>>x>>y;
other.setX(x);
other.sety(y);
return in;
}
Five, parentheses operator
Definition: The function call operator is overloaded, sometimes it is hoped that the object can be used like a function,
Also known as a functor, there is no fixed way of writing, and it is more flexible;
Format: return value operator()(parameter){}
call: object(parameters);
case:
//函数调用运算符 括号运算符
void point::operator()(int a,int b)
{
cout << "a ="<<a << "b =" << b <<endl;
}
call: danny(1,2); ---- call in this way
6. Inheritance
1. Why inherit
A very important concept in object-oriented design is inheritance, which allows the use of another class to define a new class
Achieved the role of code reuse and improved execution efficiency
The development efficiency is improved, and there is no need to rewrite the member functions and methods of the parent class.
1) Concept
Parent class: also called the base class, for example, the animal class derives the base class, and the animal class is the base class.
Subclass: also called a derived class, for example, the animal class derives from the chicken class, and the chicken class is a subclass;
Relationship: The base class is an existing class, and the derived class is a new class that inherits the parent class
2) Format: class subclass: inherit control parent class { };
3) Inheritance control: public private protected three, the most used is public
public inheritance: common members in the parent class, the child class is still public after inheritance
Private members in the parent class cannot be used after subclass inheritance; equivalent to no inheritance
The protected member in the parent class is still a protected member after the child class inherits;
Private inheritance: Modify the properties of the parent class to be private in the subclass, and the private subclasses of the parent class cannot be accessed;
Protected inheritance: modify the properties of the parent class to be protected in the subclass, and the private subclass of the parent class cannot be accessed;
4) Subclasses cannot inherit
A: The constructor, destructor, and copy construction of the parent class cannot be inherited
B: Friend functions and friend classes of the parent class cannot be inherited
C: Overloaded operators in the parent class cannot be inherited
5) Inherited construction order
A: Construct the parent class first, and then construct the subclass. The construction of the parent class is called in the constructor initialization list of the subclass;
B: Destruct the subclass object first, and then destructure the parent class object;
C: All subclasses will call the constructor of the parent class in the initialization list;
If the subclass construction does not actively add the parent class construction, the compiler automatically calls the default constructor of the parent class;
D: The subclass copies all the member variables and methods of the parent class, and there are different memory spaces between the parent and child classes;
E: If the subclass defines the same method or variable of the parent class, the methods and variables of the parent class are hidden, and the subclass is preferred
Seven, multiple inheritance
1. Definition: A subclass inherits multiple parent classes and has methods and properties of multiple parent classes
2. Format: class subclass: public parent class 1, public parent class 2{};
3) The order of constructors and destructors in multiple inheritance
Construction order: the parent class inherits first and constructs first, then inherits and then constructs, and the subclass is constructed after the parent class is constructed
For example: class pandan: public cat, public bear
In the above example: first construct cat, then construct bear, and then construct pandan
Destruction order: Destruct the subclass first, then call the inherited class, and then destruct the first inherited class
For example: class pandan: public cat, public bear
The above example: first deconstruct pandan, then deconstruct bear and then deconstruct cat (the process of pushing and popping)
3. Diamond inheritance (to be avoided)
A parent class produces two subclasses, and then the two subclasses continue to produce grandchildren, and there are multiple grandpa variables in the grandchildren
(Virtual inheritance solves the problem of diamond inheritance)
8. Inner classes and local classes
1. Internal class: a class defined in a class;
If class A is defined in class B, then A is an inner class of B (class nesting)
2. Features
Internal classes support public private protected attribute restrictions;
Member functions in inner classes can directly access all member functions of outer classes (not vice versa)
Inner classes can access static member variables
Inner classes do not affect the memory of outer classes;
Internal classes can be declared in external classes and implemented externally (declaration and implementation can be separated)
3. Local class: a class defined inside a function;
4. Features of local classes:
The scope is inside the function, it can only be used inside the function, and the object cannot be defined outside
All members must be defined inside the class, static variables are not allowed
Local classes cannot access local variables in functions;
5. Anonymous objects
Definition: There is no variable name, no object pointed to by the pointer, call the destructor and release immediately after use;
The use of anonymous objects will not be associated with references;
Class Box{ public: Box(int x = 0){} } ; Box fun() { return Box(1); }
6. Constructors call each other
Why use: Constructors can have parameters, which makes the amount of code large, and you can call other structures of your own in the construction;
Considering long-term issues, the content of code modification is relatively small, one structure modification, and all other modifications;
Implementation method: call the constructor of the parent class in the constructor initialization list of the subclass;