好文档 - 专业文书写作范文服务资料分享网站

[C++]继承和派生实验报告

天下 分享 时间: 加入收藏 我要投稿 点赞

实 验 报 告 课程名称 程序设计语言C/C++ 实验项目 继承和派生 一、实验目的 1、 掌握继承的概念。 2、 理解派生类与基类的关系。 3、 理解不同的继承类型。 4、 掌握继承下的构造函数和析构函数。 5、 掌握单继承和多继承使用方法。 6、 理解 const 常数据成员和常成员函数的使用。 二、实验内容 1. 上机建立、编译、运行并分析下面程序,并理解继承的概念。 #include #include using namespace std; class Person { private: char m_strName[20]; int m_nAge; int m_nSex; public: Person();//构造函数 Person( char *name, int age, char sex ); //构造函数 Person( const Person &p ); //拷贝构造函数 ~Person() //析构函数 { cout<<\ }; } void SetName( char *name ); void SetAge( int age ); void setSex( char sex ); char* GetName(); int GetAge() ; char GetSex() ; void ShowMe() ; //源程序文件person.cpp的内容: //#include \Person::Person() : m_nAge( 0 ), m_nSex( 0 )//构造函数 { } Person::Person( char *name, int age, char sex ) : m_nAge( age ), m_nSex( sex == 'm'?0:1 )//构造函数 { } Person::Person( const Person &p ) : m_nAge( p.m_nAge ), m_nSex( p. m_nSex )//拷贝构造函数 { } void Person::SetName( char *name ) { } void Person::SetAge( int age ) { strcpy( m_strName, name ); strcpy( m_strName, p.m_strName ); strcpy( m_strName, name ); strcpy( m_strName, \ } m_nAge = age; void Person::setSex( char sex ) { } char* Person::GetName() { } int Person::GetAge() { } char Person::GetSex() { } void Person::ShowMe() { } //头文件 employee.h的内容: //#include \class Employee: public Person //雇员类定义 { char m_strDept[20]; //工作部门 float m_fSalary; //月薪 cout << GetName() << '\\t' << GetAge() << '\\t' << GetSex() << '\\t'; return (m_nSex == 0?'m':'f'); return m_nAge; return m_strName; m_nSex = sex == 'm'?0:1; public: Employee(); Employee( char *name, int age, char sex, char *dept, float salary ); }; Employee( Employee &e ); ~Employee() { cout<<\ } void SetDept( char *dept ); void SetSalary( float salary ); char* GetDept() ; float GetSalary() ; void ShowMe() ; //显示雇员信息 //源程序文件 employee.cpp的内容: //#include \Employee::Employee() : m_fSalary( 0.0 ) { } Employee::Employee( char *name,int age, char sex, char *dept, float salary ) : Person( name, age, sex ), m_fSalary( salary ) { } Employee::Employee( Employee &e ) : Person( e.GetName(), e.GetAge(), e.GetSex() ), m_fSalary( e.m_fSalary ) { } void Employee::SetDept( char *dept ) { strcpy( m_strDept, dept ); strcpy( m_strDept, e.m_strDept ); strcpy( m_strDept, dept ); strcpy( m_strDept, \} void Employee::SetSalary( float salary ) { } char* Employee::GetDept() { } float Employee::GetSalary() { } void Employee::ShowMe() { } //源程序文件 main.cpp的内容: //#include \int main() { } 要求:(1)给出实验结果; Employee emp1; emp1.ShowMe(); Employee emp2(\张莉 \图书馆 \emp2.ShowMe(); cout<<\调用基类 GetName()返回值为: \return 0; Person::ShowMe(); cout << m_strDept << \return m_fSalary; return m_strDept; m_fSalary = salary;

[C++]继承和派生实验报告

实验报告课程名称程序设计语言C/C++实验项目继承和派生
推荐度:
点击下载文档文档为doc格式
5ueoo0q19n0wk4t3v4f03ibqw7s1q700tlw
领取福利

微信扫码领取福利

微信扫码分享