11_3CEmployee inherited from CPerson class

Source: Internet
Author: User

Print? # 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 <setw (10) <m_szName <setw (25) <m_szId;
If (m_nSex = 0)
Cout <setw (7) <"women ";
Else
Cout <setw (7) <"man ";
Cout <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 ()
{
Cout <"name" <setw (5) <"id" <setw (5) <"sex" <setw (5) <"age" <setw (15) <"department" <setw (10) <"salary" <endl;
Cout <m_szName <setw (5) <m_szId;
If (m_nSex = 0)
Cout <setw (5) <"women ";
Else
Cout <setw (5) <"man ";
Cout <setw (5) <m_nAge <setw (15) <m_szDepartment <setw (10) <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:

# 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 <setw (10) <m_szName <setw (25) <m_szId;
If (m_nSex = 0)
Cout <setw (7) <"women ";
Else
Cout <setw (7) <"man ";
Cout <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 ()
{
Cout <"name" <setw (5) <"id" <setw (5) <"sex" <setw (5) <"age" <setw (15) <"department" <setw (10) <"salary" <endl;
Cout <m_szName <setw (5) <m_szId;
If (m_nSex = 0)
Cout <setw (5) <"women ";
Else
Cout <setw (5) <"man ";
Cout <setw (5) <m_nAge <setw (15) <m_szDepartment <setw (10) <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:

 


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.