11th Week Project 2 inherited employee information salary category

Source: Internet
Author: User

/* *copyright (c) 2016, College of Computer Science, Yantai University * Author: Liu Jinshi * Completion date: May 10, 2016 * Problem description: (1) defines a class named CPerson, with the following private There are members: name, social Security number, gender and age, member functions: constructors, destructors, output information functions. Based on this, the CEmployee class is derived, and the derived class CEmployee adds two new data members, respectively, to represent departments and salaries. 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.    */#include <iostream> #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 ();}; Cperson::~cperson () {}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;} 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 ();}; CEmployee:: CEmployee (String name,string id,int sex,int age,string department,double salary): CPerson (Name,id,sex,age) , M_szdepartment (department), M_salary (Salary) {}void cemployee::show2 () {COUT&LT;&LT;SETW (<< "name" < &LT;SETW << "id" &LT;&LT;SETW (7) << "Sex" &LT;&LT;SETW (5) << "Age" &LT;&LT;SETW (+) << "    Department "&LT;&LT;SETW (Ten) <<" 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 takes the protected attribute, it can be implemented with the above code, otherwise, if the member variable of the//base class CPerson takes the privated attribute, only Cperson::show1 () 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;}

Operation Result:


/* (2) strings in addition to the string types that are expanded with C + +, the traditional C language can also be represented 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.) ) */#include <iostream> #include <iomanip> #include <string.h>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 ();};    Cperson::~cperson () {}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 (+) <<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;}    Class Cemployee:public Cperson{private:char * m_szdepartment; Double M_salary;public:cemployee (char * Name,char * id,int sex,int Age,char * department,double salary);    void Show2 (); ~cemployee ();}; CEmployee:: CEmployee (char * name,char * id,int sex,int Age,char * department,double salary): CPerson (Name,id,sex,age),    M_salary (Salary) {m_szdepartment=new Char[strlen (department) +1]; strcpy (m_szdepartment,department);} 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 takes the protected attribute, it can be implemented with the above code, otherwise, if the member variable of the//base class CPerson takes the privated attribute, only Cperson::show1 () can be used; implement cout &LT;&LT;SETW (n) <<m_szdepartment<<setw (Ten) <<m_salary<<endl;} Cemployee::~cemplOyee () {}int main () {char * name,*id,*department;    int sex,age;    Double salary;    Name=new CHAR[10];    Id=new char[19];    Department=new CHAR[10];    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;}
Operation Result:

Ditto

Learning experience:

For a deep copy of a subclass, if a deep copy of a variable is already in the constructor of the base class, deep copying of the inherited variable is not required in the constructor of the subclass, just a deep copy of the added pointer variable.

11th Week Project 2 inherited employee information salary category

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.