1. 程式人生 > >C++第十週【任務2】定義一個名為CPerson的類,有以下私有成員:姓名、身份證號、性別和年齡,成員函式:建構函式、解構函式、輸出資訊的函式。

C++第十週【任務2】定義一個名為CPerson的類,有以下私有成員:姓名、身份證號、性別和年齡,成員函式:建構函式、解構函式、輸出資訊的函式。

/* (程式頭部註釋開始)
* 程式的版權和版本宣告部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 檔名稱:    C++第十週【任務2】                        
* 作    者: 李洪懸                             
* 完成日期:   2012      年   4    月   23    日
* 版 本 號:          
* 對任務及求解方法的描述部分
* 輸入描述: 
* 問題描述:定義一個名為CPerson的類,有以下私有成員:姓名、身份證號、性別和年齡,成員函式:建構函式、解構函式、輸出資訊的函式。
* 程式輸出: 
* 程式頭部的註釋結束
*/

【任務2】定義一個名為CPerson的類,有以下私有成員:姓名、身份證號、性別和年齡,成員函式:建構函式、解構函式、輸出資訊的函式。並在此基礎上派生出CEmployee類,派生類CEmployee增加了兩個新的資料成員,分別用於表示部門和薪水。要求派生類CEmployee的建構函式顯示呼叫基類CPerson的建構函式,併為派生類CEmployee定義解構函式,定義輸出資訊的函式。
#include <iostream>
#include <string.h>
#include <iomanip>//setw:設定輸出資料的寬度,使用時應#include <iomanip.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();  //需要釋放建立物件時動態分配的記憶體
};

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();
	system("pause");
	return 0;
}

#include <iostream>

#include <string.h>

#include <iomanip>//setw:設定輸出資料的寬度,使用時應#include <iomanip.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(char *name,char *id,int sex,int age)
{
	m_szName=name;

	m_szId=id;

	m_nSex=sex;

	m_nAge=age;

}
void CPerson::Show1()
{
	cout<<m_szName<<setw(10)<<m_szId<<setw(10)<<((m_nSex==0)?"woman":"man")<<setw(8)<<m_nAge<<setw(12);
}
CPerson::~CPerson()
{
	delete []m_szName;
	delete []m_szId;
	//delete []m_nSex;
	//delete []m_nAge;

}



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();
};
CEmployee::CEmployee(char *name,char *id,int sex,int age,char *department,float salary):CPerson(name,id,sex,age)
{
	m_szDepartment=department;

	m_Salary=salary;

}
void CEmployee::Show2()
{
	
	cout <<"m_szName"<<setw(10)<<"m_szId"<<setw(12)<<"m_nSex"<<setw(8)<<"m_nAge"<<setw(16)<<"m_szDepartment"<<setw(16)<<"m_Salary"<<endl;
	Show1();
	cout << m_szDepartment<<setw(16)<<m_Salary<<endl;
}
CEmployee::~CEmployee()
{
	delete []m_szDepartment;
//	delete []m_Salary;
}



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();

	system("pause");

	return 0;
}