The well-designed object-oriented system encapsulates the object internally, leaving only two functions responsible for copying the object, that is, the copy constructor and the copy assignment operator. When necessary, the compiler creates a coping function for the class and describes the "compiler generated version" Action: Copies all member variables of the Copied object.
At any time, as long as you implement the copying function of the derived class, you must carefully copy its base class components. These components are often private, so they cannot be directly accessed. Therefore, the coping function of the dispatch class should call the corresponding basic functions:
void logCall(const string& funcName);
class Customer { public:
... Customer(const Customer& rhs); Customer& operator=(const Customer& rhs);
... private: string name; Date lastTranscation; };
Class PriorityCustomer: public Customer {public :... priorityCustomer (const PriorityCustomer & rhs); PriorityCustomer & operator = (const PriorityCustomer & rhs );... private: int priority;}; PriorityCustomer: PriorityCustomer (const PriorityCustomer & rhs): Customer (rhs), // call the base class's copy constructor priority (rhs. priority) {logCall ("PriorityCustomer copy constructor");} PriorityCustomer & PriorityCustomer: operator = (const PriorityCustomer & rhs) {logCall ("PriorityCustomer copy extends constructor"); Customer :: operator = (rhs); // copy the base class Customer components. priority = rhs. priority; return * this ;}