/* *文件名称:Ex1-4.cpp *作者:吴培鸿 *完成日期:2016年5月10日 *对任务及求解方法的描述部分: *输入描述:无 *问题描述:按照要求编写代码 *程序输出:按要求写出程序 *问题分析:略 *算法分析:略 */ /*#include <iostream> #include <string> using namespace std; class Person{ public: Person(string s){ name=s; } void display( ){ cout<<"Name: "<<name<<endl; } private: string name; }; class Student:public Person//(1) 继承基类Person中的公有部分 { public: Student(string s, int g):Person(s) // (2)对基类Person的构造函数的初始化 {grade=g;} void display1( ) { display(); // (3) 调用类Person中的display成员函数 cout<<"Grade: "<<grade<<endl; } private: int grade; }; int main( ) { Student s("龙三",19); s.display1(); // (4)通过类Student的对象s输出 return 0; } #include<iostream> #include<string> using namespace std; class Stu //声明基类 { public: Stu(int n, string nam ); //基类构造函数 void display(); //成员函数,输出基类数据成员 protected: //(*)访问权限为保护型的数据成员 int num; //学生学号 string name; //学生姓名 }; Stu::Stu(int n,string nam) { num=n; name=nam; } void Stu::display() { cout<<"学号:"<<num<<endl; cout<<"姓名:"<<name<<endl; } class StuDetail: public Stu //声明派生类StuDetail { public: //学生nam,学号n,a岁,家住ad,他的班长是nam1,学号n1 StuDetail(int n, string nam,int a, string ad,int n1, string nam1); //派生类构造函数 void show( ); //成员函数,输出学生的信息 void show_monitor( ); //成员函数,输出班长信息 private: Stu monitor; //学生所在班的班长,班长是学生,是Stu类的成员 int age; //学生年龄 string addr; //学生的住址 protected: int num; string name; }; StuDetail::StuDetail(int n,string nam,int a,string ad,int n1,string nam1):Stu(n,nam),monitor(n1,nam1) { age=a; addr=ad; } void StuDetail::show() { cout<<"学生信息:"<<endl; display(); cout<<"年龄:"<<age<<endl; cout<<"住址:"<<addr<<endl; } void StuDetail::show_monitor() { cout<<"\n\n班长信息:"<<endl; monitor.display(); } int main( ) { //学生张三,10010号,19岁,家住江西南昌,他的班长是李四,学号10001 StuDetail s(10010,"张三",19,"江西南昌",10001,"李四"); s.show( ); //输出学生信息 s.show_monitor(); //输出班长信息 return 0; }*/ #include<iostream> #include<string> using namespace std; class CPerson { protected: string m_szName; string m_szId; int m_nSex;//0:女,1:男 int m_nAge; public: CPerson(string name,string id,int sex,int age); void Show1(); }; 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<<"\t"<<m_szName<<"\t"<<m_szId<<"\t"; if(m_nSex) cout<<"男\t"; else cout<<"女\t"; cout<<m_nAge; } 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::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<<"\t姓名\tID\t性别\t年龄\t部门\t薪水\n"; Show1(); cout<<"\t"<<m_szDepartment<<"\t"<<m_Salary<<endl; } int main() { string name,id,department; int sex,age; double salary; cout<<"请输入雇员的姓名,ID,性别(0:女,1:男),年龄,部门,薪水:\n"; cin>>name>>id>>sex>>age>>department>>salary; CEmployee employee1(name,id,sex,age,department,salary); employee1.Show2(); return 0; }