当前位置:范文大全 > 调查报告 > c语言实验报告怎么写 C++实验报告材料九

c语言实验报告怎么写 C++实验报告材料九

时间:2021-10-15 13:24:18 浏览次数:

 VC++程序设计实验报告

  实验九题目:继承与派生

 专 业

 学 生 姓 名

 班 级 学 号

 指 导 教 师

 指 导 单 位

  日 期

 教师评语

  教师签名:

  年 月 日

 成绩评定

 备 注

 一、实验目的1. 理解类的继承概念,能够定义和使用类的继承关系

 2.掌握派生类的声明与定义方法

 3. 掌握公有、私有和保护派生的访问特性

 二、实验容

 设计一个大学的类系统,学校有学生、老师、职员,每种人员都有自己的特性,他们之间又有相同的地方。利用继承机制定义这个系统中的各个类及类上的必须操作。

 假定车可分为货车、客车又可分为轿车、面包车和公共汽车。请设计相应的类层次结构并加以实现。

 三、实验结果及其结果分析

 1.(1)源程序:

 #include <iostream.h>

 #include <string.h>

 class Person{

 protected:

  char m_strName[10];

  int m_nSex;

  int m_nAge;

 public:

  Person(char *name,int age,char sex){

  strcpy(m_strName, name);

  m_nSex= (sex=='m'?0:1 );

  m_nAge = age;

  }

  void setName(char *name){

  strcpy(m_strName, name);

  }

  void setSex(int sex){

  m_nSex= (sex=='m'?0:1 );

  }

  void setAge(int age){

  m_nAge = age;

  }

  char * getName(){

  return m_strName;

  }

  int getAge(){

  return m_nAge;

  }

  int getSex(){

  return m_nSex;

  }

  void ShowMe(){

  cout<<" 姓 名:"<<m_strName<<endl;

  cout<<" 性 别:"<<(m_nSex==0?"男":"女")<<endl;

  cout<<" 年 龄:"<<m_nAge<<endl;

  }

 };

 class Teacher : public Person{

  char m_strDept[20];

  int m_fSalary;

 public:

  Teacher(char *name,int age,char sex,char *dept,int salary)

  :Person(name,age,sex){

  strcpy(m_strDept, dept);

  m_fSalary = salary;

  }

  void ShowMe() {

  Person::ShowMe();

  cout<<" 工作单位:"<<m_strDept<<endl;

  cout<<" 月 薪:"<<m_fSalary<<endl;

  }

  void setSalary(int salary){

  m_fSalary = salary;

  }

  int getSalary(){

  return m_fSalary;

  }

 };

 class Student : public Person{

  char m_strID[12];

  char m_strClass[12];

 public:

  Student(char *name,int age,char sex,char *ID,char *Class)

  :Person(name,age,sex){

  strcpy(m_strID, ID);

  strcpy(m_strClass, Class);

  }

  void ShowMe(){

  cout<<" 学 号:"<<m_strID<<endl;

  Person::ShowMe();

  cout<<" 班 级:"<<m_strClass<<"\n";

  }

  void setID(char * ID){

  strcpy(m_strID, ID);

  }

  void setClass(char *Class){

  strcpy(m_strClass, Class);

  }

  char* getID(){

  return m_strID;

  }

  char* getClass(){

  return m_strClass;

  }

 };

 class Employee:public Person{

  int m_fSalary;

 public:

  Employee(char *name,int age,char sex,int salary)

  :Person(name,age,sex){

  m_fSalary = salary;

  }

  void setSalary(int salary){

  m_fSalary = salary;

  }

  int getSalary(){

  return m_fSalary;

  }

  void ShowMe(){

  Person::ShowMe();

  cout<<" 工 资:"<<m_fSalary<<"\n";

  }

 };

 void main(){

 Teacher teacher1("周明",38,'m',"计算机系",3800);

  Student std1("王芳",20,'f',,"计算机03");

  Employee emPloyee1("鑫",25,'f',2000);

 teacher1.ShowMe();

  cout<<"--------------------"<<endl;

  std1.ShowMe();

  cout<<"--------------------"<<endl;

  emPloyee1.ShowMe();

 teacher1.setAge(40);

  teacher1.setSalary(4500);

  std1.setAge(21);

  emPloyee1.setAge(26);

  emPloyee1.setSalary(2000);

 cout<<"--------------------"<<endl;

  cout<<"修改各类人员的属性后:"<<endl;

  teacher1.ShowMe();

  cout<<"--------------------"<<endl;

  std1.ShowMe();

 cout<<"--------------------"<<endl;

  emPloyee1.ShowMe();

 }

 (2)实验结果:

 2.(1)源程序:

 #include<iostream.h>

 class vehicle // 定义基类vehicle

 {

 public: // 公有函数成员

  vehicle(int in_wheels,float in_weight); // 给数据成员初始化

  int get_wheels(); // 获取车轮数

  float get_weight(); // 获取汽车重量

  void setWeels(int wls);

  void setWeight(float wt);

  void display(){

  cout<<"车轮数:"<<wheels<<" 汽车重量:"<<weight<<endl;

  }

 private: // 私有数据成员

  int wheels; // 车轮数

  float weight; // 表示汽车承重

 };

 vehicle::vehicle(int in_wheels,float in_weight){

  wheels = in_wheels;

  weight = in_weight;

 }

 float vehicle::get_weight(){

  return weight;

 }

 int vehicle::get_wheels(){

  return wheels;

 }

 void vehicle::setWeels(int wls){

  wheels = wls;

 }

 void vehicle::setWeight(float wt){

  weight = wt;

 }

 class truck:public vehicle // 定义货车类truck

 {

 private: // 新增私有数据成员

  float weight_load; // 承重

 public: // 新增公有成员函数

  truck(int wheel,float wt,float wl):vehicle(wheel,wt){

  weight_load = wl;

  }

  float getLoads(){

  return weight_load;

  }

  void display(){

  vehicle::display();

  cout<<"汽车承重"<<weight_load<<endl;

  }

 };

 //车和客车,客车又可分为轿车、面包车和公共汽车

 class car:public vehicle // 定义客车类car

 {

  int passenger_load; // 新增私有数据成员,表示载客数

 public: // 新增公有成员函数

  car(int in_wheels,float in_weight,int people=4):vehicle(in_wheels,in_weight)

  {

  passenger_load = people;

  }

  int getPassengers(){

  return passenger_load;

  }

  void setPassengers(int people){

  passenger_load = people;

  }

  void display(){

  vehicle::display();

  cout<<"载客数:"<<passenger_load<<endl;

  }

 };

 void main(){

  truck truck1(8,400,100000); // 货车

  car car1(4,20); // 客车

  car saloon_car(4,10,5); // 轿车

  car microbus(6,10,18); // 面包车

  car bus(6,20,30); // 公共汽车

  // 显示相关信息

  truck1.display();

  cout<<"---------------------"<<endl;

  car1.display();

  cout<<"---------------------"<<endl;

  saloon_car.display();

  cout<<"---------------------"<<endl;

  microbus.display();

  cout<<"---------------------"<<endl;

  bus.display();

 }

 (2)实验结果:

 

 四、实验收获与体会

 通过本次试验,我加深了对继承与派生的进一步理解。此次实验编写了有关类的继承与派生的两道程序,我更加了解了类的继承概念。此次试验题目感觉有点难度,我自己编写的时候不是那么顺利,总是出错。后来在网上搜了相关的题目研究了别人的方法,在自己的程序中找出了错误,修改了相关的继承调用,最终正确地调试出了程序。