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++的异常处理,是对知识的拓宽。
专业知识分享