Google

存档

2008年5月 的存档

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

2008年5月31日

一、实验内容

生成表示学生的类XS,提供成员函数dispXM()、disXB() 和dispNL()分别用来表示姓名、性别、和年龄,并将他们全部定义为纯虚函数;生成CZS类表示初中学生包含数据成员xm、 xb和nl表示学生的姓名、性别和年龄,提供成员函数disXM()disXB()和disNL()分别用来显示姓名、性别和年龄;再生成类GZS表示高中生和DXS类同样包含相同的数据成员和成员函数。编写主程序,观察运行结果。

3、实训要求:

会建造一个类。

会创建类的对象。

会使用类对象调用具体成员函数和数据成员完成相应功能。

会定义和使用派生出类。

编制主程序。

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

二、实验目的

通过多态实训掌握多态特点,培养学生利用多态的手法来解决简单的实际问题。

三、主要仪器设备及耗材

硬件:计算机一台

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

四、实验步骤

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

2. 编写代码

3. 编译、调试并运行

五、实验数据及处理结果

试验内容相关代码:

XS.H文件代码:

#include<iostream>

#include<Cstring>

using namespace std;

class XS

{

public:

virtual void dispXM()=0;

virtual void dispXB()=0;

virtual void dispNL()=0;

};

class CZS: public XS

{

public:

CZS(char *M,char *B,int N){strcpy(xm,M);strcpy(xb,B);nl=N;}

void dispXM();

void dispXB();

void dispNL();

private:

char xm[5];

char xb[6];

int nl;

};

void CZS:: dispXM()

{

cout<<”CZS:xm:”<<xm<<endl;

}

void CZS:: dispXB()

{

cout<<”CZS:xB:”<<xb<<endl;

}

void CZS:: dispNL()

{

cout<<”CZS:nl:”<<nl<<endl;

}

class GZS: public XS

{

public:

GZS(char *M,char *B,int N){strcpy(xm,M);strcpy(xb,B);nl=N;}

void dispXM();

void dispXB();

void dispNL();

private:

char xm[5];

char xb[6];

int nl;

};

void GZS:: dispXM()

{

cout<<”GZS:xm:”<<xm<<endl;

}

void GZS:: dispXB()

{

cout<<”GZS:xB:”<<xb<<endl;

}

void GZS:: dispNL()

{

cout<<”GZS:nl:”<<nl<<endl;

}

class DXS: public XS

{

public:

DXS(char *M,char *B,int N){strcpy(xm,M);strcpy(xb,B);nl=N;}

void dispXM();

void dispXB();

void dispNL();

private:

char xm[5];

char xb[6];

int nl;

};

void DXS:: dispXM()

{

cout<<”DXS:xm:”<<xm<<endl;

}

void DXS:: dispXB()

{

cout<<”DXS:xB:”<<xb<<endl;

}

void DXS:: dispNL()

{

cout<<”DXS:nl:”<<nl<<endl;

}

Main函数代码:

#include<iostream>

#include”XS.h”

using namespace std;

void fun(XS *ptr)

{

ptr->dispXM();

ptr->dispXB();

ptr->dispNL();

}

void main()

{

XS *p;

CZS c1(”Tom”,”male”,12);

p=&c1;

fun(p);

GZS g1(”lucy”,”female”,16);

p=&g1;

fun(p);

DXS d1(”lily”,”female”,20);

p=&d1;

fun(p);

}

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

通过使用纯虚函数使接口统一了,CZS,GZS,DXS都可以使用fun()函数来访问,隐藏了内部的结构。

3.其他文章

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

2008年5月31日

一、实验内容

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();

}

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

2.推荐软件 ,

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

2008年5月31日

一、实验内容

1. 义一个一维数组a[10],从键盘输入元素的值,然后找出所有元素中的最大值并输出。

2. 从键盘输入一行字符,统计其中大写英文字符的个数。要求使用字符数组。

3. 用“Virtue and happiness are mother and daugher.初始化一个字符串,用指针偏移符号遍历字符串的内容,并打印各个字符。用字符“+”来分隔相邻的字符。

二、实验目的

1. 掌握声明数组、初始化数组,以及引用各个数组元素的方法。

2. 学会运用指针。

3. 理解指针、数组和字符串之间的紧密关系。

4. 掌握声明并使用字符串数组。

三、主要仪器设备及耗材

硬件:计算机一台

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

四、实验步骤

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

2. 编写代码

3. 编译、调试并运行

五、实验数据及处理结果

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

#include<iostream>

using namespace std;

void main()

{

int maximum(int a[],int n);

int a[10];

int j;

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

cin>>a[j];

cout<<”The maximum is ”<<maximum(a,10)<<endl;

}

int maximum(int a[],int n)

{

int max,i;

max=*a;

for(i=1;i<n;i++)

if(max<*(a+i)) max=*(a+i);

return max;

}

试验内容(2)的相关代码:
#include<iostream>

#include<cstring>

using namespace std;

void main()

{

int statistics(char a[],int m);

int n;

char str1[34];

cin>>str1;

n=strlen(str1);

cout<<statistics(str1,n)<<endl;

}

int statistics(char a[],int m)

{

int i ,countp=0;

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

if(*(a+i)>=’A'&&*(a+i)<=’Z')countp++;

return countp;

}

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

#include<iostream>

using namespace std;

void main()

{

char str[]=”Virtue and happiness are mother and daugher”;

int i;

for(i=0;str[i]!=’\0′;i++)

cout<<*(str+i)<<+”;

}

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

2.推荐软件

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

2008年5月31日

一、实验内容

1. 定义一个类Student记录学生计算机课程的成绩。要求使用静态成员变量或静态成员函数计算全班学生计算机课程的总成绩和平均成绩。编写一个测试程序来测试该类。

2. 设计一个类CTimeInfo,要求其满足下述要求。

(1) 要求有一个无参数的构造函数,其初始的小时和分钟分别为:0,0.

(2) 要求有一个带参数的构造函数,其参数分别对应小时和分钟。

(3) 要求用一个成员函数实现时间的设置。

(4) 要求用一个友员函数实现以12小时的方式输出时间。

(5) 要求用一个友员函数实现以24小时的方式输出时间。

二、实验目的

1. 掌握静态数据成员和静态成员函数的基本使用方法。

2. 理解友元与友元函数的作用,掌握其使用方法。

三、主要