Notes will continue to update, there are errors in the place welcome corrections, Thank you! constructors and copy control Virtual destructor
Destructors in the inheritance system should be defined as virtual functions.
As I've said before, defining a destructor is going to define copies and assignments, but the destructor of the base class is the exception. synthetic copy control and inheritance The relationship between copy control defined as deletion in a derived class and the base class
Class B
{public
:
b ();///default constructor declaration
B (const b&) = delete;////defined as Delete copy constructor//
since the copy constructor is defined, Will not synthesize the move Operation
};
Class D:public b
{
//doesn't declare anything, just inherits the B
};
D D; Correct: D's Synthetic default constructor calls the default constructor of B, and I don't have any member
D D2 (d);//error: Because B's copy constructor is delete, so d is also delete and cannot be invoked
move operations and inheritance
Move operation: The base class has virtual destructors, so the move operation is not synthesized and should be explicitly defined in the base class if it is really needed, at the same time you explicitly define the copy operation:
Class Quote
{public
:
Quote () = default;//To force the synthesis of the defaults constructor
Quote (const quote&) = default;//copy Constructor
Quote (quote&&) = default; Move constructor
quote& operator= (const quote&) = default;//Copy Assignment
quote& operator= (quote&&) = Default Move Assignment
virtual ~quote () = default;//virtual destructor
};
Thus, quote derived classes automatically get a synthetic move operation as well. Copy control member of derived class
The copy and move constructors of a derived class, while copying and moving their own members, also copy and move the base class part, and the assignment operators are similar. to define a copy or move constructor for a derived class
Class Base
{
};
Class D:public base
{
//We want to copy or move the base class part, we must explicitly call public in the constructor initializer list of the derived class
:
d (const d& D): base (d) { //Call copy constructor of base class to copy base class part
//Here the special is that base (d) will match the copy constructor of base, although people actually accept B type
D (d&& D): Base (Std::move (D) ){}
};
derived class assignment operator
Which is the copy assignment and the move assignment.
D &d::operator= (const D &RHS)
{
base::operator= (RHS);//explicitly assigning to base class: (Synthetic or custom) the copy assignment operator of the base class will be interpreted
// Drop the old value of the base class portion of the left object and then use RHS to assign a new value to it
//next to the derived class's own partial assignment (omitted) return
*this;
}
derived class destructor
It's simple, don't worry, it's the same as the normal old one.
Note:
The order of object destruction is the reverse of the creation order: the derived class destructor executes first, and then executes the destructor of the base class.
That is, the partial copy of the direct base class, then copy the members of the class itself, destroy itself and then destroy the base class, the base class part will automatically destroy. to prohibit calling virtual functions in a constructor or a destructor
Suppose we call a function of a derived class in the constructor of a base class to access its members, which is how it can be accessed if the derived class object has not been built. As a result, C + + prohibits this behavior. The inherited constructor derived class class cannot inherit the default constructor of the base class, copy constructors, move constructors, but a derived class can reuse its base class custom constructors, and base class members in the derived class invoke the constructor initialization of the base class, and the other members in the derived class are initialized by default.
Class Bulk_quote:public Disc_quote
{public
:
using disc_quote::D isc_quote;//use using inheritance Disc_ Quote Constructor
};
When you put a using on a constructor, the using declaration statement will generate code, and the compiler generates a derived class constructor that corresponds to the base class, and the derived class constructor that is generated for the example above is as follows:
Bulk_quote (const string& book, double Price, size_t qty, double disc): Disc_quote (book, Price, qty, disc) {}
the characteristics of inherited constructorsThe using declaration of the constructor does not change the access level of the constructor if the constructor of the base class is explicit or constexpr, the inherited constructor is also when a base class constructor contains a default argument, the derived class obtains multiple inherited constructors, Each of these constructors ignores a formal parameter that contains a default argument.
In addition, if the derived class itself defines a constructor that has the same argument list as the base class constructor, then this constructor of the base class is not inherited, it is defined by the derived class, and it is covered. containers and inheritance containers and inheritance
We cannot store multiple types of objects that have inherited relationships directly in a container:
Examples of errors:
Vector<quote> Basket;
Basket.push_back (Bulk_quote ("A", 50, 10, 0.25));
Basket elements are quote objects, so when we add bulk_quote objects to them, the part that belongs to the derived class is ignored, so what should we do?
A: The container should place pointers (preferably smart pointers) rather than objects.
Example:
Vector<shared_ptr<quote>> Basket;
Basket.push_back ("A", make_shared<quote>);
Basket.push_back (make_shared<quote> ("B", M, 0.25));
cout << basket.back ()->net_price << Endl; Print the price after the discount, this time the derived class object is complete
Inheritance and Composition
Derived classes should reflect the is a relationship to the base class, and objects of the publicly derived class should be available in any place where a base class object is required; Has a is the meaning of the containing member between classes. Writing Basket Class
For C + + object-oriented programming, it is not a direct object, with pointers and references.
Because Bo main time reason, the concrete realization I do not write, please see the book ~ Inquiry into the text query procedure
Because Bo main time reason, the concrete realization I do not write, please see the book ~