课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接

【项目2 - 职员有薪水了】定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、析构函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee添加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义析构函数,定义输出信息的函数。
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();
}; 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();
}; 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;
}

  以下的执行结果供參考:
  

  參考解答:
#include <iostream>
#include <string>
#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();
}; 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();
}; 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(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() { } 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(10)<<"name"<<setw(25)<<"id"<<setw(7)<<"sex"<<setw(5)<<"age"<<setw(12)<<"department"<<setw(10)<<"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(5)<<m_nAge;
//因为基类CPerson的成员变量採用了protected属性,因此可採用上述述代码实现,否则若
//基类CPerson的成员变量採用了privated属性,则仅仅能使用CPerson::Show();实现
cout<<setw(12)<<m_szDepartment<<setw(10)<<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;
}

【项目2拓展(选做)】字符串除了用C++扩充的string类型外,按C语言的传统,还用char *表示。请将类声明中的string所有改为char *后,又一次写一遍程序(此时的差别是,类中有指针成员,构造和析构函数须要考虑深复制的问题了。)

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

參考解答:

#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; //setw:设置输出数据的宽度,使用时应#include <iomanip.h>
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<<setw(10)<<"name"<<setw(25)<<"id"<<setw(7)<<"sex"<<setw(5)<<"age"<<setw(12)<<"department"<<setw(10)<<"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(5)<<m_nAge;
//因为基类CPerson的成员变量採用了protected属性,因此可採用上述述代码实现,否则若
//基类CPerson的成员变量採用了privated属性,则仅仅能使用CPerson::Show();实现
cout<<setw(12)<<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;
}

=================== 迂者 贺利坚 CSDN博客专栏=================
|== IT学子成长指导专栏 专栏文章的分类文件夹(不定期更新) ==|
|== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
|== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====

C++第11周(春)项目2 - 职员有薪水了的更多相关文章

  1. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  2. C++第11周(春)项目3 - 点类派生直线类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3 - 点类派生直线类]定义点类Poin ...

  3. C++第11周(春)项目4 - 类族的设计

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目4 - 类族的设计]按下面的提示,由基类 ...

  4. 2013级C++第15周(春)项目——输入输出流及文件文件操作

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...

  5. C++第12周(春)项目2 - &quot;双肩挑&quot;教师

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 教师兼干部类](第11章习题9) ...

  6. 2013级C++第12周(春)项目——成员的訪问属性、多重继承

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读 1.阅读程序.分析类中成员 ...

  7. C++第15周(春)项目3 - OOP版电子词典(一)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...

  8. C++第13周(春)项目1 - 点、圆的关系

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目1 - 点.圆的关系](1)先建立一个P ...

  9. 2013级C++第13周(春)项目——继承的进一步话题与GUI应用开发

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读:阅读以下类的定义,请说出在 ...

随机推荐

  1. Storm的本地运行模式示例

    以word count为例,本地化运行模式(不需要安装zookeeper.storm集群),maven工程, pom.xml文件如下: <project xmlns="http://m ...

  2. MFC记住上次路径---遇到的一些问题

    今天完成一个需求,就是记住用户选择的文件路径,先是熟悉代码,然后在网上找解决方法,一开始感觉没什么,网上的方法差不多,应该很容易做出来,结果真是卡了一半天,到晚上自己才慢慢的搞清楚了. 遇到的问题真不 ...

  3. MFC工程目录

    如果已经以Debug方式编译链接过程序,则会在解决方案文件夹下和工程子文件夹下各有一个名为“Debug”的文件夹,而如果是Release方式编译则会有名为“Release”的文件夹.这两种编译方式将产 ...

  4. windows7+eclipse-jee-luna+hadoop2.6运行环境及eclipse plugin插件编译

    一.hadoop集群环境配置  参见:<Hadoop2.6集群环境搭建(HDFS HA+YARN)原来4G内存也能任性一次.> Win7环境: 登录用户名:hadoop , 与Hadoop ...

  5. 用原生javascript模拟经典FC游戏公路争霸

    #用原生javascript模拟经典FC游戏公路争霸 前几天看了园子里面的随笔 [原生javascript开发仿微信打飞机小游戏](http://www.cnblogs.com/Mr-Nobody/p ...

  6. thymeleaf中的模板布局

    一.包括模板片段: 1:定义和引用片段,我们经常会想要包含在模板片段来自其他模板.常见的用途是页脚.标题.菜单…; 为了做到这一点,Thymeleaf需要我们定义包含可用的片段,我们可以通过使用th: ...

  7. [MAC OSX - 1] OSX10.10不能安装JKD8,不能使用eclipse

    (1)电脑升级为10.10后,打开eclipse总是提示"您需要安装旧 Java SE 6 运行环境才能打开"Eclipse". 解决:安装JKD   (2)不能安装JK ...

  8. Session,Cookie 和local storage的区别

    以前从没有听说过local storage, 在网上查了一些资料,得到如下结论 从存储位置看,分为服务器端存储和客户端存储两种 服务器端: session 浏览器端: cookie, localSto ...

  9. HDU 5266 pog loves szh III (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...

  10. UVaLive 7361 Immortal Porpoises (矩阵快速幂)

    题意:求Fibonacci的第 n 项. 析:矩阵快速幂,如果不懂请看http://www.cnblogs.com/dwtfukgv/articles/5595078.html 是不是很好懂呢. 代码 ...