Exercise 13.18
1#include <iostream>2#include <string>3#include <memory>4 5 using namespacestd;6 7 Static inti =0;8 9 classEmployee {TenFriend ostream& print (ostream& OS,Constemployee&EMP); One Public: AEmployee (): Empnames ("Jay Chou"), EmpNo (i++) {} -Employee (Const string&name): Empnames (name), EmpNo (i++) {} - Private: the stringEmpnames; - intEmpNo; - }; - +ostream& print (ostream& OS,Constemployee&EMP); - + intMain () A { at Employee E1; - Print (cout, E1); -Employee E2 ("Anna"); - Print (cout, E2); -Employee E3 ("Wilber Cypress"); - Print (cout, E3); inSystem"Pause"); - return 0; to } + -Ostream & Print (Ostream & OS,ConstEmployee &EMP) the { *OS <<"Name:"<< Emp.empnames <<"ID:"<< Emp.empno <<Endl; $ returnos;Panax Notoginseng //TODO: Insert a return statement here -}
Exercise 13.19
Required, if a synthetic copy control is used, then the copy is the value of the copied member, and if there is the same name, it is not possible to identify two people by ID.
1#include <iostream>2#include <string>3#include <memory>4 5 using namespacestd;6 7 Static inti =0;8 9 classEmployee {TenFriend ostream& print (ostream& OS,Constemployee&EMP); One Public: AEmployee (): Empnames ("Jay Chou"), EmpNo (i++) {} -Employee (Const string&name): Empnames (name), EmpNo (i++) {} -Employee (Constemployee& EMP): Empnames (Emp.empnames), EmpNo (i++) {} theEmployee &operator=(ConstEmployee &EMP); - Private: - stringEmpnames; - intEmpNo; + }; - +ostream& print (ostream& OS,Constemployee&EMP); A at intMain () - { - Employee E1; - Print (cout, E1); -Employee E2 ("Anna"); - Print (cout, E2); in Employee E3 (E2); - Print (cout, E3); toEmployee e4 =E1; + print (cout, E4); -System"Pause"); the return 0; * } $ Panax NotoginsengOstream & Print (Ostream & OS,ConstEmployee &EMP) - { theOS <<"Name:"<< Emp.empnames <<"ID:"<< Emp.empno <<Endl; + returnos; A //TODO: Insert a return statement here the } + -Employee & Employee::operator=(ConstEmployee &EMP) $ { $Empnames =Emp.empnames; -EmpNo = i++; - return* This; the //TODO: Insert a return statement here -}
Exercise 13.20
Since we do not define copy control for these two classes, it is generally the compiler's own synthesis of the associated copy control, because we do not define the members of the class that cannot be copied, assigned, or destroyed in these two classes, so the class uses the composition, whereas the copy control member of the composition is defined as deleted.
Exercise 13.21
In my opinion, these two classes are also not necessary to define their copy control members.
These two classes are used in conjunction with the query of the text, in the context of program design, the copy operation is not necessary.
For different text, or for a different stream, you just need to create a new class object, without the need for a copy operation.
C++primer 13.1.6 Sessions