/* *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<<SETW (+) <<M_SZNAME<<SETW (+) <<m_szId; if (m_nsex==0) COUT<<SETW (7) << "Women"; else COUT<<SETW (7) << "Man"; COUT<<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<<SETW (<< "name" < <SETW << "id" <<SETW (7) << "Sex" <<SETW (5) << "Age" <<SETW (+) << " Department "<<SETW (Ten) <<" salary "<<endl; COUT<<SETW (<<M_SZNAME<<SETW) <<m_szId; if (m_nsex==0) COUT<<SETW (7) << "Women"; else COUT<<SETW (7) << "Man"; COUT<<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 <<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<<SETW () <<M_SZNAME<<SETW (+) <<m_szId; if (m_nsex==0) COUT<<SETW (7) << "Women"; else COUT<<SETW (7) << "Man"; COUT<<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<<SETW (Ten) << "name" <<SETW (+) << "id" <<SETW (7) << " Sex "<<SETW (5) <<" age "<<setw <<" department "<<SETW <<" Salary "<< Endl COUT<<SETW (<<M_SZNAME<<SETW) <<m_szId; if (m_nsex==0) COUT<<SETW (7) << "Women"; else COUT<<SETW (7) << "Man"; COUT<<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 <<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