您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页《面向对象程序设计》实验册

《面向对象程序设计》实验册

来源:飒榕旅游知识分享网
 WORD格式整理

学 生 实 验 报 告 册

(理工类)

课程名称:面向对象程序设计 专业班级: _13计统2班_ 学生学号:___1305103038 学生姓名: 曹亚孟 _ __ 所属院部: 计算机工程学院 指导教师: 吴刚

20 15 —20 16 学年 第 一 学期

金陵科技学院教务处制

专业知识分享

WORD格式整理

实验报告书写要求

实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,要遵照以下字体、字号、间距等的具体要求。纸张一律采用A4的纸张。

实验报告书写说明

实验报告中一至四项内容为必填项,包括实验目的和要求;实验仪器和设备;实验内容与过程;实验结果与分析。各院部可根据学科特点和实验具体要求增加项目。

填写注意事项

(1)细致观察,及时、准确、如实记录。 (2)准确说明,层次清晰。

(3)尽量采用专用术语来说明事物。

(4)外文、符号、公式要准确,应使用统一规定的名词和符号。 (5)应完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。

实验报告批改说明

实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用百分制,具体评分标准由各院部自行制定。

实验报告装订要求

实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。

专业知识分享

WORD格式整理

实验项目名称: C++基础程序设计 实验学时: 3 同组学生姓名: 无 实验地点: A107 实验日期: 9.18 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.了解Visual C++6.0或者Visual C++2005以上版本的特点。

2.学会Visual C++6.0或者Visual C++2005以上版本的开发环境,来创建和调试标准的C++控制后台应用程序。

3.学会使用Visual C++6.0或者Visual C++2005以上版本的开发环境中的程序调试功能,例如单步执行、设置断点、观察变量值等。 4.掌握string类型的用法。

5.掌握C++语言编程时输入和输出格式控制。 6.掌握多文件结构的使用。 7.掌握重载函数的使用。

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.给出Visual C++调试一个简单应用程序的步骤,要求程序输出字符串“Hello!Welcome to C++!”。 程序:

#include using namespace std; int main() {

cout<<\"Hello!\"<cout<<\"Welcome to C++!\"<return 0; }

运行结果:

专业知识分享

WORD格式整理

2.调试以下程序,观察运行结果。 #include using namespace std; void main() { }

int a,b=10; int&ra=a; a=20;

cout<cout<专业知识分享

WORD格式整理

运行结果及结果分析:

3.编写一程序,实现九九乘法表的2中格式输出,格式如下: (1) 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 2 2 4 6 8 10 12 14 16 18 3 3 6 · · · · · · · 4 4 8 · · · · · · · 5 5 10 · · · · · · · 6 6 12 · · · · · · · 7 7 14 · · · · · · · 8 8 16 · · · · · · · 9 9 18 · · · · · · · (2) 1 2 3 4 5 6 7 8 9 1 1 2 2 4 3 3 6 9 4 4 8 12 16 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 9 9 18 27 36 45 63 72 81 注:要求每种输出格式均写成函数形式 程序:

专业知识分享

WORD格式整理

#include using namespace std;

int main() {

int i,j;

cout<<\"显示1:\"<cout<for(j=1;j<=i;j++) {

cout<cout<cout<}

专业知识分享

WORD格式整理

}

}

cout<return 0;

运行结果:

4.将第3题改成多文件结构实现。要求该工程中有3个.cpp文件和1个.h文件。 程序: .h:

#include using namespace std; int cfb1(int x,int y); int cfb2(int x,int y); .cpp: (1)

专业知识分享

WORD格式整理

#include \"3head.h\" void main() { } (2)

#include \"3head.h\"

int cfb1(int i,int j) {

cout<<\"显示1:\"<for(j=1;j<10;j++) { }

cout<cout<int i,j;

cout<专业知识分享

WORD格式整理

} (3)

#include \"3head.h\"

int cfb2(int i,int j) { }

5.编程实现比较两个数的大小,求较大值(要求使用重载函数实现)。 程序:

#include using namespace std; int max(int a,int b)

专业知识分享

cout<<\"显示2:\"<for(j=1;j<=i;j++) {

cout<}

cout<WORD格式整理

{ }

int main() { }

运行结果:

6.编写一个程序,判断一个

字符串是否为另一个字符串的子串,若是,返回字串在主串中的位置。要求不适用strstr函数,自己编写一个子函数实现。(建议使用string类型,而非字符数组。) 程序:

#include #include \"string.h\" using namespace std; void main() {

int m,n;

cout<<\"Enter two integer:\"; cin>>m>>n;

cout<<\"The bigger is:\"<b?a:b;

专业知识分享

WORD格式整理

}

char a[]=\"how are you?\"; char b[]=\"are\"; int i=0,j,na,nb,flag; na=strlen(a); nb=strlen(b); flag=1;

for(i=0;i!='\\0';i++) { }

if(a[i]==b[j]) j++;

运行结果:

四、实验体会

熟悉了C++开发环境的使用,对C++编程有了初步的了解,能够编写一些简单的和C类似的程序。

实验项目名称: 类与对象 实验学时: 3

专业知识分享

WORD格式整理

同组学生姓名: 无 实验地点: A107 实验日期: 9.25 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.掌握类和对象的概念、定义方法以及类与对象的简单用法。 2.掌握成员函数的实现与调用方法。 3.深刻领会类与对象的区别。 4.理解类实现数据隐藏和封装的原理。

5.掌握构造函数、拷贝构造函数、分析构造函数的定义和使用,尤其注意组合类中它们的调用顺序。

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.用面向对象的程序设计方法实现栈的操作。栈又叫堆栈,是一个常用的数据结构,它是一种运算受限的线性表,仅允许在表的一端进行插入和删除运算,是一种后进先出表。

提示:栈用一维整型数组来表示,栈的大小定义为10;栈定义为一个类stack;实现栈的创建、进栈和出栈、栈的消亡。进栈函数:void push(int n);出栈函数:int pop(void); 程序:

#include using namespace std; const int maxsize=10; class stack { public:

void init() {

专业知识分享

WORD格式整理

}

top=-1;

void push(int n); int pop(); void dead();

private: };

void stack::push(int n) { }

int stack::pop()

if(top==maxsize) { } else { }

top++; data[top]=n;

cout<<\"栈已满!\"<int data[maxsize];

专业知识分享

WORD格式整理

{ }

void stack::dead() {

if(top<0) { } else {

top--;

cout<<\"栈中还剩\"<<(top+1)<<\"个元素\"<top--;

return data[top+1]; cout<<\"栈下溢!\"<专业知识分享

WORD格式整理

}

}

int main() {

stack seqstack; int i,x,m; seqstack.init();

cout<<\"请输入10个数字:\"; for(i=0;icout<<\"请输入你所需要的操作:\"<>m; switch(m) {

cin>>x;

seqstack.push(x);

case 1:

{

for(i=0;i专业知识分享

WORD格式整理

}

}

}

cout<<\"\"<break;

case 2:

{ } return 0;

for(i=0;iseqstack.dead();

break;

运行结果:

2.将第1题中的实验内容改为多文件结构实现。 程序:

#include using namespace std; #define N 10 class stack {

专业知识分享

WORD格式整理

static int top; int a[N]; public:

void push(int n); void pop(); void die(); };

#include int stack::top=0; void stack::push(int n) {

if(topa[top++]=n;

cout<<\"进栈数据:\"<cout<<\"堆栈已经满了,不能再加数进去了!\"<}

void stack::pop() {

if(top<=0)

专业知识分享

WORD格式整理

cout<<\"堆栈已经空了,没有数据了!\"<cout<<\"出栈数据:\"; cout<void stack::die() {

int i;

for(i=0;icout<<\"消亡堆栈!\"<#include \"head.h\" void main() {

int n,m;

cout<<\"输入两个你要输入的数据N和M:\"; cin>>n>>m; stack s; s.push(n);

专业知识分享

WORD格式整理

s.push(m); s.pop(); s.pop(); s.pop(); s.die(); }

3.设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和折构函数、拷贝构造函数、内联成员函数、组合类等。 程序:

#include #include using namespace std; class Date { private:

int year; int month; int day;

public:

Date(){}

专业知识分享

WORD格式整理

Date(int y,int m,int d) { } void set() { }

void display() { }

cout<>year>>month>>day; year=y; month=m; day=d;

};

class Person { private:

int num; char sex; Date birthday; char ID[18];

public:

Person(){}

专业知识分享

WORD格式整理

Person(int n,int y,int m,int d,char id[18],char

s='m'):birthday(y,m,d)

{ }

Person(Person& p) { }

void input() {

cout<<\"录入数据:\"<>num; cout<<\"性别(m/f):\"; cin>>sex; cout<<\"生日:\"; birthday.set(); cout<<\"身份证号:\"; cin>>ID; ID[18]='\\0'; num=p.num; sex=p.sex;

birthday=p.birthday; strcpy(ID,p.ID);

num=n; sex=s; strcpy(ID,id);

专业知识分享

WORD格式整理

};

}

cout<void output() { } ~Person() { }

cout<<\" \"<cout<<\"身份证号:\"<int main() {

Person p1; p1.input(); p1.output(); return 0;

专业知识分享

WORD格式整理

}

运行结果:

4.设计一个计算薪水的类Payroll,它的数据成员包括:单位小时的工资、已经工作的小时数、本周应付工资数。在主函数定义一个具有10个元素的对象数组(代表10个雇员)(可以定义普通对象数组,也可以定义堆对象数组)。程序询问每个雇员本周已经工作的小时数,然后显示应得的工资。要求:输入有效性检验:每个雇员每周工作的小时数不能大于60,同时也不能为负数。 程序:

#include #include class Payroll { private:

double money; double total;

public:

int time; int GetTime()

专业知识分享

WORD格式整理

{ }

int GetTotalMoney() {

return (total =time * money); do { }

while(time< 0 && time >60 ); money = 100; return 0;

printf(\"input time:\"); scanf(\"%d\

}

void Print() {

printf(\"total:%lf\\n\

} }; main() {

Payroll p[10];

专业知识分享

WORD格式整理

}

p[0].GetTime(); p[0].GetTotalMoney(); p[0].Print();

运行结果:

四、实验体会

对多文件有了更深了解,能够熟练的掌握多文件的使用方法。学习了C++的构造函数,以及拷贝构造函数,接触友元组合类等新知识,需要课后多加练习。

专业知识分享

WORD格式整理

实验项目名称: 静态成员与友元 实验学时: 3 同组学生姓名: 无 实验地点: A107 实验日期: 10.09 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.掌握类中静态成员的定义的方法。 2.掌握静态数据成员初始化的方法。

3.掌握静态数据成员和数据函数成员的访问和使用方法。 4.掌握友元函数的定义和使用方法。 5.了解友元类的使用方法。

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.任意输入10个数,计算器总和及平均值。设计程序测试该功能(要求用类、静态友员实现)。 程序:

#include #include using namespace std; class number {

int No; int figure;

static double sum; static int sumpeople;

public:

number(int n,int ifigure) {

专业知识分享

WORD格式整理

};

}

No=n;

figure=ifigure; sum+=figure; sumpeople++;

static int Sum() { }

static double Average() { }

return sum/sumpeople; return sum;

int number::sumpeople=0; double number::sum=0; void main() {

int i,j;

cout<<\"The ten number is:\"<专业知识分享

WORD格式整理

}

}

cin>>j; number(i,j);

cout<<\"The sum is:\"<运行结果:

2.求两点之间的距离(要求定义点Point类,并用友员函数实现)。 程序:

#include #include using namespace std; class Point {

double x,y;

public: };

double Distance(Point& px,Point& py)

Point(double m,double n):x(m),y(n){} friend double Distance(Point& px,Point& py);

专业知识分享

WORD格式整理

{ }

void main() { }

运行结果:

Point p1,p2; int a1,b1,a2,b2; cin>>a1>>b1>>a2>>b2; p1=Point(a1,b1); p2=Point(a2,b2);

cout<<\"the distance is\"<d=sqrt((px.x-py.x)*(px.x-py.x)+(px.y-py.y)*(px.y-py.y)); return d;

3.定义一个经理类Manager,其成员数据包括编号id,姓名name和年龄age,均声明为private访问属性。再定义一个员工类Employee,其成员数据及访问属性与经理类相同。将Manager类声明为Employee类的友员函数,并在Manager类中定义一个函数访问Employee类的私有数据成员并进行输出。 程序:

#include #include

专业知识分享

WORD格式整理

using namespace std; class Employee {

friend class Manager;

private: int id; char *n; int age; public: Employee() { id = 1; n = \"abc\"; age = 20;

}

};

class Manager { private: int id;

char name[10]; 专业知识分享

WORD格式整理

int age;

public:

Manager() { }

Employee e;

void Print(Employee &e) { } };

void main() { } 运行结果:

专业知识分享

cout<Employee e; Manager m; m.Print(e); system(\"pause\");

WORD格式整理

四、实验体会

使用。

懂得了静态成员的使用、静态成员的作用域和使用方法。理解并掌握友元的

专业知识分享

WORD格式整理

实验项目名称: 继承与派生 实验学时: 3 同组学生姓名: 无 实验地点: A107 实验日期: 10.16 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.掌握利用单继承和多重继承的方式定义派生类的方法。 2.理解在各种继承方式下构造函数和析构函数的执行顺序。

3.理解和掌握public、protected、private对基类成员的访问机制。 4.理解虚基类的概念,引入虚基类的目的和作用。 5. 理解在虚基类时的构造函数和析构函数的执行顺序。

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.分析以下程序,写出运行结果。 #include using namespace std; class Base { public: };

class Derive:public Base { public:

Derive() {cout<<\"执行派生类构造函数\"<专业知识分享

WORD格式整理

};

void main() { }

运行结果:

2. 分析以下程序,写出运行结果。 #include calss Base { public:

Base(){cout<<”基类构造函数”<~Base(){cout<<”基类析构函数”<class Derive:public Base { public:

Derive(){cout<<”派生类构造函数”<专业知识分享

WORD格式整理

}; void main() {

Derive *p=new Derive; Delete p; }

运行结果:

3.求一个三角形物体的面积,同时求一个圆形物体的面积(要求使用继承)。 #include #include using namespace std; class Object { public:

void getData() { }

cout<<\"Input x,y,z:\"; cin>>x>>y>>z;

protected:

专业知识分享

WORD格式整理

};

int x,y,z;

class Triangle:public Object { public: };

class Circle:public Object { public:

void getData() {

void getData() { }

double Area() { }

double d=(x+y+z)/2;

double s=sqrt(d*(d-x)*(d-y)*(d-z)); return s;

cout<<\"triangle:\"; Object::getData();

专业知识分享

WORD格式整理

};

}

cout<<\"circle:(only radius=x)\"<double Area() { }

double s=x*x*3.1415; return s;

void main() { }

运行结果:

Triangle t; t.getData();

cout<<\"The results:\"<cout<<\"The results:\"<专业知识分享

WORD格式整理

4.一个三口之家,大家知道父亲会开车,母亲会唱歌。但其父亲还会修电视机,只有家里人知道。小孩既会开车又会唱歌,甚至也会修电视机。母亲瞒着任何人在外面做小工以补贴家用。此外小孩还会打乒乓球。

编写程序输出者三口之家一天从事的活动:先是父亲出去开车,然后母亲出去工作(唱歌),母亲下班后去做两个小时的小工。小孩在俱乐部打球,在父亲回家后,开车玩,后又高兴地唱歌。晚上,小孩和父亲一起修电视机。 程序:

#include using namespace std; class father { protected:

void repairTV(){cout<<\"Repairing TV.\"<public: };

class mother {

void dootherJob(){cout<<\"Mother is doing other job!\";} void drive (){cout<<\"Father is driving.\"<public: };

virtual void sing(){cout<<\"Mother is singing songs.\"<专业知识分享

WORD格式整理

class child:public father,public mother { public: }; int main() {

cout<<\"main: \"<m.dootherJob(); c.playingPingPang(); c.drive(); c.sing(); f.repairTV(); c.repair();

void drive(){ cout<<\"Child is driving .\"<专业知识分享

WORD格式整理

}

return 0;

运行结果:

5.设计定义一个哺乳动物类Mammal,再由此派生出狗类Dog和猪类Pig,从狗类Dog和猪类Pig又派生出PigDog类。定义一个PigDog类的对象,观察基类与各派生类的构造函数和析构函数的调用顺序。 程序:

#include using namespace std; class Mammal { public: };

class Dog:public Mammal { public:

Dog(){cout<<\"调用dog构造函数\"<专业知识分享

WORD格式整理

};

~Dog(){cout<<\"调用dog析构函数\"<class Pig:public Mammal { public: };

class PigDog:public Dog,public Pig { public: };

void main() {

/* Mammal m;*/ Dog d; Pig p; PigDog pd;

PigDog(){cout<<\"调用pigdog构造函数\"<专业知识分享

WORD格式整理

}

运行结果:

四、实验体会

本次实验学习了继承和派生,对于其使用以及理解有点困难。

专业知识分享

WORD格式整理

实验项目名称: 多态性 实验学时: 3 同组学生姓名: 无 实验地点: A107 实验日期: 10.23 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.加深理解继承和多继承的概念、应用等。

2.掌握虚函数的定义方法,以及在实现多态性中的作用;理解掌握实现动态多态性的前提条件,理解静态多态性和动态多态性的区别。

3.理解运算符重载的概念和实质;掌握预案算重载函数的定义方法;掌握运算符重载为类的成员函数和友元函数的方法;掌握几种常用运算符的重载,用来解决函数。

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.利用虚函数实现的多态性来求四种几何图形的面积。这四种几何图形是:三角形、矩形、正方形和圆。 程序:

#include #define PI 3.1415926 using namespace std; class Area {

double x,y; public:

Area(double x,double y,int z):x(x),y(y) {

if(z==0)

cout<<\"三角形的面积为:\"<专业知识分享

WORD格式整理

else if(z==1)

cout<<\"矩形面积为:\"<Area(double x,int z):x(x) {

if(z==0)

cout<<\"正方形的面积为:\"<cout<<\"圆的面积为:\"<void main() {

Area a(3,4,0); Area b(3,4,1); Area c(5,0); Area d(5,1); }

运行结果:

专业知识分享

WORD格式整理

2.声明Point类,有坐标-x,-y两个成员变量;对Point类重载“++”(自增)、“——”(自减)运算符,实现对坐标值的改变。 程序:

#include using namespace std; class Point {

double _x,_y; public:

Point(Point &point) {

_x=point._x; _y=point._y; }

Point(double x,double y):_x(x),_y(y){} void operator ++() {

cout<<\"原来的值为:\"<<_x<cout<<\"改过后的值为:\"<<_x<double getx(){return _x;}

专业知识分享

WORD格式整理

double gety(){return _y;} };

void main() {

double x,y; Point p(1,2); p.operator ++(); }

运行结果:

3.定义一个复数类,通过重载运算符:*,/,直接实现两个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。 程序:

#include using namespace std; class Complex {

double r,v; public:

Complex(Complex &c)

专业知识分享

WORD格式整理

{

r=c.r; v=c.v; }

Complex(double r=0,double v=0):r(r),v(v){} void operator =(Complex c); void show() {

cout<=0?\"+\":\"-\")<friend Complex operator +(Complex a,Complex b); friend Complex operator *(Complex a,double r); Complex operator /(Complex a); double getr(){return r;} double fetv(){return v;} };

void Complex::operator =(Complex c) {

r=c.r; v=c.v; }

Complex operator *(Complex &a,Complex &b)

专业知识分享

WORD格式整理

{

double ar=a.getr(); double av=a.fetv(); double br=b.getr(); double bv=b.fetv();

Complex c(ar*br-av*bv,av+bv); return c; }

Complex operator *(Complex a,double r) {

double ar=a.getr(); double av=a.fetv(); Complex c(ar+r,av); cout<<\"*的结果为:\"; return c; }

Complex Complex::operator /(Complex a) { Complex

c(a.r*r-a.v*(-v))/(a.r*a.r+a.v*(-a.v)),(a.v)/(a.r*a.r+a.v*(-a.v)); cout<<\"/的结果为:\"; return c;

专业知识分享

WORD格式整理

}

void main() {

Complex a(2,2),b(3,4),test; test=a*b; test.show(); test=a/b; test.show(); }

运行结果:

4.在第3题基础上,增加重载函数的加法和减法运算符的功能,实现两个复数的加法,一个复数和一个实数的加法;两个复数的减法,一个复数与一个实数的减法。用成员函数实现加法运算符的重载,用友元函数实现减法运算符的重载。 要求:自己设计主函数,完成程序的调试工作。 程序:

#include using namespace std; class Complex {

double r,v;

专业知识分享

WORD格式整理

public:

Complex(Complex &c) {

r=c.r; v=c.v; }

Complex(double r=0,double v=0):r(r),v(v){} void operator =(Complex c); void show() {

cout<=0?\"+\":\"\")<friend Complex operator *(Complex a,Complex b); friend Complex operator -(Complex a,Complex b); friend Complex operator -(Complex a,double r); Complex operator/(Complex a); Complex operator+(Complex a); Complex operator+(double a); double getr(){return r;} double fetv(){return v;} };

void Complex::operator =(Complex c)

专业知识分享

WORD格式整理

{

r=c.r; v=c.v; }

Complex operator *(Complex a,Complex b) {

double ar=a.getr(); double av=a.fetv(); double br=b.getr(); double bv=b.fetv();

Complex c(ar*br-av*bv,av+bv); cout<<\"友元函数实现与复数的乘法:\"; return c; }

Complex Complex::operator /(Complex a) { Complex

c((a.r*r-a.v*(-v))/(a.r*a.r+a.v*(-a.v)),(a.v+v)/(a.r*a.r+a.v*(-a.v)); cout<<\"成员函数实现与复数的除法:\"; return c; }

Complex Complex::operator +(Complex a)

专业知识分享

WORD格式整理

{

Complex c(a.r+r,a.v+v);

cout<<\"成员函数实现与复数的加法:\"; return c; }

Complex Complex::operator +(double a) {

Complex c(r+a,v);

cout<<\"成员函数实现与实数的加法:\"; return c; }

Complex operator -(Complex a,Complex b) {

Complex c(a.r-b.r,a.v-b.v);

cout<<\"友元函数实现与复数的减法:\"; return c; }

Complex operator -(Complex a,double r) {

Complex c(a.r-r,a.v);

cout<<\"友元函数实现与实数的减法:\"; return c;

专业知识分享

WORD格式整理

}

void main() {

Complex a(2,2),b(3,4),test; test=a*b; test.show(); test=a/b; test.show(); test=a + b; test.show(); test=a + 5; test.show(); test=a-b; test.show(); test=a-5; test.show(); }

运行结果:

专业知识分享

WORD格式整理

四、实验体会

本次学习了多态性,增加了函数的复用性,很大程度的减少了代码的重复性。

专业知识分享

WORD格式整理

实验项目名称: 异常处理 实验学时: 3 同组学生姓名: 无 实验地点: A107 实验日期: 11.20 实验成绩: 批改教师: 吴刚 批改时间:

专业知识分享

WORD格式整理

一、实验目的和要求

1.正确理解C++的异常处理机制。 2.学习异常处理的声明和执行过程

二、实验设备和环境

1.计算机每人一台。

2.安装WindowsXP或者以上版本操作系统。

3.安装Visual C++6.0或者Visual C++2005以上版本。

三、实验内容及步骤

1.声明一个异常类Cexception,有成员函数Reason(),用来显示异常的类型,在子函数中触发异常,在主程序中处理异常,观察程序的执行流程。 程序:

#include using namespace std; int Div(int x,int y); void main() {

Try {

cout<<\"5/2=\"<catch(int) {

cout<<\"except of deviding zero.\\n\";

专业知识分享

WORD格式整理

cout<<\"this is ok.\\n\"; } }

int Div(int x,int y) {

if(y==0) throw y; return x/y; }

运行结果:

2.设计一个异常抽象类Exception,在此基础上派生一个OutOfMemory类响应内存不足,一个RangeError类响应输入的数不在指定范围内,实现并测试这几个类。 程序“

#include using namespace std; void MyFunc(void); class Expt { public:

Expt(){}; ~Expt(){};

专业知识分享

WORD格式整理

const char * ShowReason() const {

return \"Expt 类异常。\"; } }; class Demo { public:

Demo(); ~Demo(); };

Demo::~Demo() {

cout<<\"析构 Demo.\"<Demo::Demo() {

cout<<\"构造 Demo.\"<void MyFunc() {

Demo d;

专业知识分享

WORD格式整理

cout<<\"在MyFunc()中抛掷Expt类异常。\"<int main() {

cout<<\"在main函数中。\"<cout<<\"在try块中,调用MyFunc()。\"<catch(Expt e) {

cout<<\"在catch中 异常处理程序中。\"<catch(char *str) {

cout<<\"捕获到其他异常:\"<cout<<\"回到main函数。从这里恢复执行。\"<专业知识分享

WORD格式整理

return 0; }

运行结果:

四、实验体会

本次的异常学习熟悉了C++的异常处理,是对知识的拓宽。

专业知识分享

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务