C++第11周(春)项目2 - 职员有薪水了
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接
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博客专栏================= |
C++第11周(春)项目2 - 职员有薪水了的更多相关文章
- C++第11周(春)项目1 - 存储班长信息的学生类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...
- C++第11周(春)项目3 - 点类派生直线类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3 - 点类派生直线类]定义点类Poin ...
- C++第11周(春)项目4 - 类族的设计
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目4 - 类族的设计]按下面的提示,由基类 ...
- 2013级C++第15周(春)项目——输入输出流及文件文件操作
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...
- C++第12周(春)项目2 - "双肩挑"教师
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 教师兼干部类](第11章习题9) ...
- 2013级C++第12周(春)项目——成员的訪问属性、多重继承
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读 1.阅读程序.分析类中成员 ...
- C++第15周(春)项目3 - OOP版电子词典(一)
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...
- C++第13周(春)项目1 - 点、圆的关系
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目1 - 点.圆的关系](1)先建立一个P ...
- 2013级C++第13周(春)项目——继承的进一步话题与GUI应用开发
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读:阅读以下类的定义,请说出在 ...
随机推荐
- 省市区(县)三级联动代码(js 数据源)
ylbtech-JavaScript-Utility:省市区(县)三级联动代码(js 数据源) 省市区(县)三级联动代码(js 数据源) 1.A,源代码(Source Code)返回顶部 1.A.1, ...
- TextView字体和背景图片 设置透明度
背景图片透明度设置 viewHolder.relative_layout.getBackground().setAlpha(225); 0 --- 225 ((TextView)tv). ...
- iframe 透明
iframe是迫不得已才使用的,因为使用iframe会带来较多的问题,而有的浏览器可以设置将iframe当作广告屏蔽. 在最近的一个工作内容中使用了iframe,开始遇到的问题是iframe高度自适应 ...
- acdream 1044
题意:有你一个草坪,草的初始高度都是100,让你用割草机割,割草机只能横着或竖着割,每次割的高度一定,问你能不能割出给定的草坪出来. 考虑任意一个草被割要么是横着要么竖着,所以任意一个草必然是它所在行 ...
- wuzhicms内的全局函数--load_class()
load_class() 可以加载并实例化/coreframe/app/模块名/libs/class/$class.class.php类文件里的对象,如果有扩展类文件EXT_$class.class. ...
- Android JNI之JAVA调用C/C++层
转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5929996.html 一.java调用本地函数的开发步骤: 1.编写本地方法的类(可以说是用来叙述本地方法的类 ...
- 用Gitolite 构建 Git 服务器
转载 Gitolite 构建 Git 服务器 作者: 北京群英汇信息技术有限公司 网址: http://www.ossxp.com/ 版本: 0.1-1 日期: 2010-10-07 14:52:19 ...
- 转载-清除Linux中MySQL的使用痕迹~/.mysql_history
原文地址:清除Linux中MySQL的使用痕迹~/.mysql_history 作者:RogerZhuo 原贴:http://bbs.chinaunix.net/thread-3676498-1-1. ...
- 第二百六十天 how can I 坚持
晚上去看了个电影,<万万没想到>,挺好看的,豆瓣评分不高.网络啊. 互联网会让聪明的人越来越聪明. 明天去看寻龙诀了. 懒惰会生根发芽,哈哈. 睡觉了.
- mysql查询数据库大小和表
每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 ,),'GB') as da ...