c++程序设计实验报告七/c++实验报告

一、实验内容

1. 定义一个基类MyArray,基类中可以存放一组整数。

class MyArray

{

public:

MyArray(int leng);

~MyArray( );

void Input( );

void Display( );

protected:

int *alist;

int length;

};

基类中有构造函数、析构函数、输入数据和输出数据的函数。

2. 定义一个类AverArray继承自MyArray,在该类中定义函数Aver求解整数的平均值。

3、首先设计一个人员类person作为基类,其数据成员为姓名和身份号,成员函数有人数据和显示数据;再设计一个学生地址类add,数据成员为地址和年龄,成员函数有入数据和显示数据;生成person的派生类学生student, student的数据成员为电话号和C++成绩,成员函数也是有输入数据和显示数据;设计学生成绩类score,它是stud(类和add类的派生类,继承2个类的所有属性,score类本身有数据成员数学成绩和英语成绩,当然成员函数也有输人数据和显示数据,职员类employee继承person类,类中有任何成员。编写主程序,观察运行结果。

要求:

建成人员类persona。

建成地址类add。

由person类派生出student类。

由person类派生出employee类。

由student类和add类共同派生出学生成绩类score。

编制主程序。

运行主程序,观察运行结果

二、实验目的

1. 理解一种称为“继承”的软件重用形式。在继承中,新类是通过吸收现有类的属性和行为,从现有类创建的。

2. 能够通过继承已有的类来创建新的类。

3. 理解基类的派生类的概念。

4. 理解各种不同类型的继承(public、protected和private),并了解各种继承类型的使用时机。

三、主要仪器设备及耗材

硬件:计算机一台

软件:VC++ 6.0,MSDN2003或者以上版本

四、实验步骤

1. 创建空白新工程,向新工程中添加空文件

2. 编写代码

3. 编译、调试并运行

五、实验数据及处理结果

试验内容(1)(2)相关代码:

//myarray。h

#include<iostream>

using namespace std;

class MyArray

{

public:

MyArray(int leng);

void Input( );

void Display( );

protected:

int *alist;

int length;

};

MyArray:: MyArray(int leng)

{

length=leng;

alist=new int;

}

void MyArray:: Input()

{

int j;

for(j=0;j<length;j++)

cin>>*(alist+j);

}

void MyArray:: Display()

{

int i;

for(i=0;i<length;i++)

cout<<*(alist+i);

}

class AverArray: public MyArray

{

public:

AverArray(int L): MyArray(L){}

float Aver();

};

float AverArray:: Aver()

{

int i,sum=0;

for(i=0;i<length;i++)

sum=sum+*(alist+1);

return sum/length;

}

//MainCpp

#include<iostream>

#include”MyArray.h”

using namespace std;

void main()

{

AverArray   a1(10);

float average;

a1.Input();

average=a1.Aver();

cout<<”The average is ”<<average<<endl;

}

试验内容(3)的相关代码:

//add.h

#include<ioStream>

using namespace std;

class add

{

public:

void Input();

void Display();

protected:

char address[10];

int age;

};

void add:: Input()

{

cin>>address;

cin>>age;

}

void add:: Display

{

cout<<”The student’address is ”<<address<<endl;

cout<<”The student is ”<<age<<”years old”<<endl:

}

//person.h

#include<iostream>

using namespace std;

class person

{

public:

void Input();

void Display();

protected:

char Name[5];

unsigned long  ID;

};

void person:: Input()

{

cin>>Name;

cin>>ID;

}

void person:: Display()

{

cout<<”The person’name is ”<<Name<<endl;

cout<<”The persom’id is ”<<ID<<endl;

}

//student.h

#include<iostream>

#include”person.h”

using namespace std;

class student: public person

{

public:

void Input();

void Display();

protected:

unsigned long  phonenumber;

int  Cmark;

};

void student:: Input()

{

cin>>phonenumber;

cin>>Cmark;

}

void student:: Display()

{

cout<<”The student’phonenumber is ”<<phonenumber<<endl;

cout<<”the student c++ mark is ”<<Cmark<<endl;

}

//employee.h

include<iostream>

#include”person.h”

using namespace std;

enum gender{ MALE , FEMALE};

class employee: public person

{

public:

void Input();

void Display();

private:

int wage;

int age;

gender G;

};

void employee:: Input()

{

cin>>wage;

cin>>age;

cin>>G;

}

void employee:: Display()

{

cout<<”wage:”<<wage<<”age:”<<age<<”gender:”<<G<<endl;

}

//score.h

#include<iostream>

#include”student.h”

#include”add.h”

using namespace std;

class score: public add,public student

{

public:

void Input();

void Display();

private:

int Mmark;

int Emark;

}

void score:: Input()

{

cin>>Mark;

cin>>Emark;

}

void score:: Display()

{

cout<<”The student’s maths score is ”<<Mmark<<endl;

cout<<”The student’s  score is ”<<Emark<<endl;

}

//Main.cpp

#include<iostream>

#include”student.h”

#include”person.h”

#include”employee.h”

#include”add.h”

#include”score.h”

using namespace std;

void main()

{

score s1;

employee employee1;

s1.add:: Input();

s1.student:: Input();

s1.student.person:: Input();

s1.Input();

employee1.person:: Input();

employee1.Input();

s1.add:: Display();

s1.student:: Display();

s1.student.person:: Display();

s1.Display();

employee1.person:: Display();

employee1.Display();

}

六、思考讨论题或体会或对改进实验的建议

Leave a Reply

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:


正在读取数据……