返回:贺老师课程教学链接
#include
#include
using namespace std;
class Person{
public:
Person(char* s){
strcpy(name,s);
}
void display( ){
cout<<"Name: "<
[ 参考解答]
class Stu //声明基类
{
public:
Stu(int n, string nam ); //基类构造函数
void display( ); //成员函数,输出基类数据成员
protected: //(*)访问权限为保护型的数据成员
int num; //学生学号
string name; //学生姓名
};
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; //学生的住址
};
int main( )
{
//学生王力,10010号,19岁,家住上海的北京路,他的班长是李孙,学号10001
StuDetail s(10010,"Wang-li",19,"115 Beijing Road,Shanghai",10001,"Li-sun");
s.show( ); //输出学生信息
s.show_monitor(); //输出班长信息
return 0;
}
(1)上面是声明好的类及测试函数,请完成类中成员函数的定义,使运行结果如图所示。
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;
}
下面的运行结果供参考:
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
#include
using namespace std;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0):x(x0), y(y0) {};
void PrintPoint(); //输出点的信息
protected:
double x,y; //点的横坐标和纵坐标
};
void Point::PrintPoint()
{
cout<<"Point: ("<
程序运行参考图: