C + + 11th week (Spring) Project 2-staff is paid.

Source: Internet
Author: User

Course Home: http://blog.csdn.net/sxhelijian/article/details/11890759, complete teaching program and resources link


"Item 2-staff is paid" defines a class named CPerson with the following private members: name, ID, gender, and age, member functions: constructors, destructors, functions that output information. Based on this, the CEmployee class is derived, and the derived class CEmployee adds two new data members, respectively, to represent the department and salary. Constructors that require derived classes CEmployee display constructors that call the base class CPerson, and define destructors for derived classes CEmployee functions that define output information.
Class CPerson  {  protected:      string m_szname;      string M_szid;      int M_nsex;//0:women,1:man      int m_nage;  Public:      CPerson (String name,string id,int sex,int age);      void Show1 ();      ~cperson ();  };      Class Cemployee:public CPerson  {  private:      string m_szdepartment;      Double m_salary;  Public:      cemployee (String name,string id,int sex,int age,string department,double salary);      void Show2 ();      ~cemployee ();  };      int main ()  {      string name,id,department;      int sex,age;      Double salary;      cout<< "input employee ' s name,id,sex (0:women,1:man), age,department,salary:\n";      cin>>name>>id>>sex>>age>>department>>salary;      CEmployee employee1 (name,id,sex,age,department,salary);      Employee1. Show2 ();      return 0;  }  
The following implementation results are available for reference:
   answers to the references:
#include <iostream> #include <string> #include <iomanip>using namespace Std;class cperson{protected:    String M_szname;    String M_szid;    int M_nsex;//0:women,1:man int M_nage;public:cperson (string name,string id,int sex,int age);    void Show1 (); ~cperson ();};    Class Cemployee:public cperson{private:string m_szdepartment;    Double M_salary;public:cemployee (String name,string id,int sex,int age,string department,double Salary);    void Show2 (); ~cemployee ();};    Cperson::cperson (String name,string id,int Sex,int age) {m_szname=name;    M_szid=id;    M_nsex=sex; M_nage=age;}    void Cperson::show1 () {COUT&LT;&LT;SETW () &LT;&LT;M_SZNAME&LT;&LT;SETW (+) <<m_szId;    if (m_nsex==0) COUT&LT;&LT;SETW (7) << "Women";    else COUT&LT;&LT;SETW (7) << "Man"; COUT&LT;&LT;SETW (5) <<m_nage<<endl;} Cperson::~cperson () {}cemployee::cemployee (string name,string id,int sex,int age,string department,double salary): CPe RsOn (name,id,sex,age) {m_szdepartment=department; M_salary=salary;} void Cemployee::show2 () {COUT&LT;&LT;SETW (Ten) << "name" &LT;&LT;SETW (+) << "id" &LT;&LT;SETW (7) << " Sex "&LT;&LT;SETW (5) <<" age "<<setw <<" department "&LT;&LT;SETW <<" Salary "<<    Endl    COUT&LT;&LT;SETW (&LT;&LT;M_SZNAME&LT;&LT;SETW) <<m_szId;    if (m_nsex==0) COUT&LT;&LT;SETW (7) << "Women";    else COUT&LT;&LT;SETW (7) << "Man";    COUT&LT;&LT;SETW (5) <<m_nAge; Because the member variable of the base class CPerson uses the protected attribute, it can be implemented with the code described above, otherwise, if the member variable of the//base class CPerson uses the privated attribute, then only cperson::show () can be used; implement cout &LT;&LT;SETW (n) <<m_szdepartment<<setw (Ten) <<m_salary<<endl;}    Cemployee::~cemployee () {}int main () {string name,id,department;    int sex,age;    Double salary;    cout<< "input employee ' s name,id,sex (0:women,1:man), age,department,salary:\n";    cin>>name>>id>>sex>>age>>department>>salary; CemplOyee employee1 (name,id,sex,age,department,salary); Employee1.    Show2 (); return 0;}

The "Project 2 expansion (optional)" string, in addition to the string type that is expanded with C + +, is represented by the traditional C language, and also by char *. After you have changed the string in the class declaration to char *, write the program again (the difference is that there are pointer members in the class, and the construction and destructor need to consider the problem of deep replication.) )
Class CPerson  {  protected:      char *m_szname;      char *m_szid;      int M_nsex;//0:women,1:man      int m_nage;  Public:      CPerson (char *name,char *id,int sex,int age);      void Show1 ();      ~cperson ();  };  Class Cemployee:public CPerson  {  private:      char *m_szdepartment;      float m_salary;  Public:      CEmployee (char *name,char *id,int sex,int Age,char *department,float salary);      void Show2 ();      ~cemployee ();  };  int main ()  {      char name[10],id[19],department[10];      int sex,age;      float salary;      cout<< "input employee ' s name,id,sex (0:women,1:man), age,department,salary:\n";      cin>>name>>id>>sex>>age>>department>>salary;      CEmployee employee1 (name,id,sex,age,department,salary);      Employee1. Show2 ();      return 0;  }  

Answers to the references:
#include <iostream> #include <string.h> #include <iomanip>using namespace Std;class cperson{    Protected:char *m_szname;    Char *m_szid;    int M_nsex;//0:women,1:man int M_nage;public:cperson (char *name,char *id,int sex,int age);    void Show1 (); ~cperson ();};    Class Cemployee:public Cperson{private:char *m_szdepartment;    Float M_salary;public:cemployee (char *name,char *id,int sex,int age,char *department,float Salary);    void Show2 (); ~cemployee ();};    Cperson::cperson (char *name,char *id,int sex,int age) {m_szname=new Char[strlen (name) +1];    strcpy (M_szname,name);    M_szid=new Char[strlen (ID) +1];    strcpy (M_szid,id);    M_nsex=sex; M_nage=age;} void Cperson::show1 () {COUT&LT;&LT;SETW (&LT;&LT;M_SZNAME&LT;&LT;SETW) &LT;&LT;M_SZID;//SETW: Set the width of the output data, use #    Include <iomanip.h> if (m_nsex==0) COUT&LT;&LT;SETW (7) << "Women";    else COUT&LT;&LT;SETW (7) << "Man"; COUT&LT;&LT;SETW (5) <<m_nAge<<endl;}cperson::~cperson () {delete []m_szname; delete []m_szid;} Cemployee::cemployee (char *name,char *id,int sex,int Age,char *department,float salary): CPerson (name,id,sex,age) {m    _szdepartment=new Char[strlen (department) +1];    strcpy (m_szdepartment,department); M_salary=salary;} void Cemployee::show2 ()//Note derived class output function should output the value of all member variables (member variables with base class inheritance) {COUT&LT;&LT;SETW << "name" &LT;&LT;SETW (25) << "id" &LT;&LT;SETW (7) << "Sex" &LT;&LT;SETW (5) << "age" <<setw << "department" <    &LT;SETW << "Salary" <<endl;    COUT&LT;&LT;SETW (&LT;&LT;M_SZNAME&LT;&LT;SETW) <<m_szId;    if (m_nsex==0) COUT&LT;&LT;SETW (7) << "Women";    else COUT&LT;&LT;SETW (7) << "Man";    COUT&LT;&LT;SETW (5) <<m_nAge; Because the member variable of the base class CPerson uses the protected attribute, it can be implemented with the code described above, otherwise, if the member variable of the//base class CPerson uses the privated attribute, then only cperson::show () can be used; implement cout &LT;&LT;SETW (n) <<m_szdepartment<<setw (Ten) <<m_salary<<endl;}  Cemployee::~cemployee () {  delete []m_szdepartment;}    int main () {char name[10],id[19],department[10];    int sex,age;    float salary;    cout<< "input employee ' s name,id,sex (0:women,1:man), age,department,salary:\n";    cin>>name>>id>>sex>>age>>department>>salary;    CEmployee employee1 (name,id,sex,age,department,salary); Employee1.    Show2 (); return 0;}



=================== Helijian csdn Blog column ================= |== It Student Growth Guide Column column category folder (not regularly updated) ==| |== C + + Classroom Online Column The course teaching link (sub-course grade) ==| |== I wrote the book-"The reverse of the university-to the positive energy of IT students" ==| ===== for it rookie runway, and students enjoy a happy and passionate university =====


C + + 11th week (Spring) Project 2-staff is paid.

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.