C ++ learning ---- virtual base class MI

Source: Internet
Author: User
Tags class manager
/*************************************** * *********************************** Note, this type of hierarchy uses the MI with a virtual base class, so remember that this type of hierarchy is used to construct the special rules of the list. Note that some methods are declared as protected. This can simplify the code of some highfink methods (for example, if highfink: ShowAll () only calls fink: ShowAll () and manager: ShowAll (), it will call pai_emp:: ShowAll () twice ). Provide Implementation of class methods and test these class methods in a program. The following is a small test program: **************************************** ***********************************//*** **************************************** * ******************************* useemp. cpp function: Provides test program remarks: no *************************************** * ***********************************/# include <iostream> using namespace std; # include "emp. h "int main (void) {employee em (" Trip "," Harris "," Thumper "); cout <em <endl; em. show All (); cout <endl; manager ma ("Amorphia", "Spindragon", "Nuancer", 5); cout <ma <endl; ma. showAll (); cout <endl; fink fi ("Matt", "Oggs", "Oiler", "Juno Barr"); cout <fi <endl; fi. showAll (); cout <endl; highfink hf (ma, "Curly Kew"); hf. showAll (); cout <"Press a key for nex phase: \ n"; cin. get (); while (cin. get ())! = '\ N') continue; highfink hf2; hf2.SetAll (); cout <"Using an empty _emp * pointer: \ n"; pai_emp * tri [4] ={ & em, & fi, & hf, & hf2}; cout <endl; for (int I = 0; I <4; I ++) {tri [I]-> ShowAll (); cout <endl ;}return 0 ;} /*************************************** * ****************** emp. h function: provides some class declaration remarks: **************************************** * *****************/# ifndef EMP_H _ # define EMP_H _ # include <iost Ream> # include <string> // class pai_empclass pai_emp {private: std: string fname; std: string lname; std: string job; public: pai_emp (); pai_emp (const std: string & fn, const std: string & ln, const std: string & j); virtual void ShowAll () const; virtual void SetAll (); friend std: ostream & operator <(std: ostream & OS, const pai_emp & e); virtual ~ Pai_emp () = 0 {}}; // class employeeclass employee: public pai_emp {public: employee (); employee (const std: string & fn, const std :: string & ln, const std: string & j); virtual void ShowAll () const; virtual void SetAll () ;}; // class managerclass manager: virtual public pai_emp {private: int inchargeof; protected: int InChargeOf () const {return inchargeof;} int & InChargeOf () {return inchargeof ;} Public: manager (); manager (const std: string & fn, const std: string & ln, const std: string & j, int ico = 0 ); manager (const pai_emp & e, int ico); manager (const manager & m); virtual void ShowAll () const; virtual void SetAll () ;}; // class finkclass fink: virtual public pai_emp {private: std: string reportsto; protected: const std: string ReportsTo () const {return reportsto;} std: string & Repor TsTo () {return reportsto;} public: fink (); fink (const std: string & fn, const std: string & ln, const std: string & j, const std: string & rpo); fink (const pai_emp & e, const std: string & rpo); fink (const fink & e); virtual void ShowAll () const; virtual void SetAll () ;}; // class highfinkclass highfink: public manager, public fink {public: highfink (); highfink (const std: string & fn, const std :: string & ln, Const std: string & j, const std: string & rpo, int ico); highfink (const 1__emp & e, const std: string & rpo, int ico ); highfink (const fink & f, int ico); highfink (const manager & m, const std: string & rpo); highfink (const highfink & h); virtual void ShowAll () const; virtual void SetAll ();}; # endif /************************************* * ********************* emp. cpp function: provides implementation of class methods. Remarks: MI with virtual base classes. The constructor rules are changed (including copying Constructor) and in the Show () method, the idea should be changed, otherwise it will be called twice. **************************************** * *****************/# Include "emp. h "using namespace std; // class pai_emp methods ostream & operator <(ostream & OS, const pai_emp & e) {e. showAll (); return OS;} void pai_emp: SetAll () {cout <"Enter the first name:"; getline (cin, fname ); cout <"Enter the last name:"; getline (cin, lname); cout <"Enter the job:"; getline (cin, job); while (cin. get ())! = '\ N') continue;} void pai_emp: ShowAll () const {cout <fname <"," <lname <endl; cout <"Job: "<job <endl;} pai_emp: pai_emp (const std: string & fn, const std: string & ln, const std: string & j ): fname (fn), lname (ln), job (j) {} pai_emp: pai_emp () {}// class employee methodsvoid employee: SetAll () {pai_emp :: setAll ();} void employee: ShowAll () const {pai_emp: ShowAll ();} employee: employee (const std: string & fn, const std :: string & ln, const std: string & j): performance_emp (fn, ln, j) {} employee: employee (): employee _emp () {}// class manager methods void manager: SetAll () {pai_emp: SetAll (); cout <"Enter the inchargeof:"; cin >>> inchargeof ;} void manager: ShowAll () const {pai_emp: ShowAll (); cout <"Inchargeof:" <inchargeof <endl;} manager :: manager (const manager & m): pai_emp (m) {inchargeof = m. inchargeof;} manager: manager (const pai_emp & e, int ico): pai_emp (e) {inchargeof = ico;} manager: manager (const std: string & fn, const std: string & ln, const std: string & j, int ico): pai_emp (fn, ln, j) {inchargeof = ico;} manager: manager (): pai_emp () {}// class fink methodvoid fink: SetAll () {pai_emp: SetAll (); cout <"Enter the reportsto :"; cin> reportsto;} void fink: ShowAll () const {pai_emp: ShowAll (); cout <"reportsto:" <reportsto <endl;} fink :: fink (const fink & e): pai_emp (e) {reportsto = e. reportsto;} fink: fink (const pai_emp & e, const std: string & rpo): pai_emp (e), reportsto (rpo) {} fink :: fink (const std: string & fn, const std: string & ln, const std: string & j, const std: string & rpo): pai_emp (fn, ln, j), reportsto (rpo) {} fink: fink (): pai_emp () {}// class highfink methodsvoid highfink: SetAll () {pai_emp :: setAll (); cout <"Enter the inchargeof:"; cin> InChargeOf (); cout <"Enter the reportsto:"; cin> ReportsTo ();} void highfink: ShowAll () const {pai_emp: ShowAll (); cout <"InChargeOf:" <InChargeOf () <endl; cout <"ReportsTo: "<ReportsTo () <endl;} highfink: highfink (const highfink & h): pai_emp (h), manager (h), fink (h) {} highfink:: highfink (const manager & m, const std: string & rpo): pai_emp (m), manager (m), fink (m, rpo) {} highfink :: highfink (const fink & f, int ico): pai_emp (f), fink (f), manager (f, ico) {} highfink: highfink (const pai_emp & e, const std: string & rpo, int ico): pai_emp (e), manager (e, ico), fink (e, rpo) {} highfink: highfink (const std:: string & fn, const std: string & ln, const std: string & j, const std: string & rpo, int ico): pai_emp (fn, ln, j), manager (fn, ln, j, ico), fink (fn, ln, j, rpo) {} highfink: highfink (): 1__emp (), manager (), fink (){}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.