/* Copyright (c) 2012, School of Computer Science, Yantai University * All Rights Reserved. * file name: text. CPP * Author: Hu Ying * Completion Date: July 15, May 11, 2013 * version number: V1.0 ** input Description: none * Problem description: Define a class named cperson with the following private members: name, ID number, gender, and age. member functions: constructor, destructor, and output function. Then, the cemployee class is derived. The derived class cemployee adds two new data members to indicate the Department and salary respectively. The cemployee constructor of the derived class is required to call the constructor of the base class cperson, define the Destructor for the derived class cemployee, and define the output information function. * Program output: Output personnel name, ID card number, gender, age, department, and salary * Problem Analysis: none * Algorithm Design: omitted */# include <iostream> # include <string. h> # include <iomanip> // use # include <iomanip> corresponding to SETW: Set the width of the output data; otherwise, SETW is displayed as the undeclared Symbol Using namespace STD; class cperson {protected: char * m_szname; char * m_szid; int m_nsex; // 0: women, 1: manint m_nage; public: cperson (char * Name, char * ID, int sex, int age); void show1 ();~ Cperson (); // The Memory dynamically allocated when the object is to be released}; 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 <SETW (10) <m_szname <SETW (25) <m_szid; If (m_nsex = 0) cout <SETW (7) <"women"; else cout <SETW (7) <"man"; cout <SETW (7) <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 () {cout <SETW (10) <"name" <SETW (25) <"ID" <SETW (7) <"sex" <SETW (7) <"Age" <SETW (12) <"department" <SETW (8) <"salary" <Endl; cout <SETW (10) <m_szname <SETW (25) <m_szid; If (m_nsex = 0) cout <SETW (7) <"women"; else cout <SETW (7) <"man"; cout <SETW (7) <m_nage <SETW (12) <m_szdepartment <SETW (8) <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 ;}
Running result:
Experience: What I learned from this program is: I know that setw () is used to set the Character spacing and he needs a header file # include <iomanip>;
2: Generally, all characters must contain the destructor to release the memory occupied by the characters.