面向对象程序设计实验报告-实验三

北京理工大学珠海学院实验报告

ZHUHAI CAMPAUS OF BEIJING INSTITUTE OF TECHNOLOGY

班级:13级计算机3班 学号:130201031037 姓名:郑永雄 指导教师:谭忠兵 实验题目:多态性 实验时间:2014-06-09 成绩:

面向对象程序设计实验报告实验三

一、实验内容

程序1. 编写一个程序,用成员函数重载运算符“+”和“-”将两个二维数组相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。

程序2. 编写一个程序,要求:

(1)声明一个类complex,定义类complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;

(2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时,能返回两个复数对象相加操作;

(3)定义成员函数print,调用该函数时,以格式“(real,imag)”输出当前对象的实部和虚部,例如:对象的实部和虚部分别为4.2和6.5,则调用print函数输出格式为:(4.2,6.5);

(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。

程序3. 写一个程序,定义抽象基类Container,由它派生出3个派生类:Sphere(球体)、Cylinder(圆柱体)、Cube(正方体)。用虚函数分别计算几种图形的表面积和体积。

程序4.编写程序:定义抽象基类Shape,area( )为求图形面积的虚成员函数。由它派生出三个派生类:Circle(圆形)、Rectangle(长方形)、和Triangle (三角形),用虚函数area分别计算各种图形的面积。在主函数中,分别创建派生类的对象并计算其面积,求出它们的面积的和。要求用基类指针数组,使它的每一个元素指向一个派生类的对象,以体现多态性。

程序5.计算正方体、球体、圆柱体的体积。

要求:定义基类shape,在其中定义求体积的纯虚函数volume,并定义一个数据成员r,它可作为球体的半径、正方体的边长以及圆柱体的底面圆半径。由shape类派生出3个类,分别是正方体类、球体类和圆柱体类,在这3个类中都具有求体积的重定义版本。在主函数中统一求3种对象的体积,实现多态性。

二、问题分析(类的数据成员、成员函数等的确定,对象的创建等)

程序1:首先定义一个数组类,数据成员是数组c[2][3],成员函数有运算符重载函数shuzu operator+(int b[2][3])和shuzu operator-(int b[2][3]),当然还有输出函数show。在主函数中创建对象x,y,z。当然也要按要求输入数组。

程序2:首先定义类complex,数据成员有real和imag,成员函数有友元运算符重载函数friend complex operator+(complex &a,complex &b)和输出函数print。在主函数中创建对象c1和c2,最后运算结果。

1

程序3:首先定义抽象基类Container,数据成员有r,成员函数有友元虚函数virtual void s_v() 由它公有派生出3个派生类:Sphere(球体)、Cylinder(圆柱体)、Cube(正方体)。在主函数中分别创建各派生类的对象s1,c1和,c2。用虚函数virtual void s_v()分别计算3种图形的表面积和体积。

程序4:首先定义抽象基类Shape,数据成员有a和b,area( )为求图形面积的虚成员函数。由它公有派生出三个派生类:Circle(圆形)、Rectangle(长方形)、和Triangle (三角形),用虚函数area分别计算各种图形的面积。在主函数中,创建基类指针ptr,分别创建派生类的对象c1, r1 和t1。Ptr指针指向派生类的对象,用area()计算其面积。

程序5:首先定义基类shape,在其中定义求体积的纯虚函数volume,并定义一个数据成员r,它是球体的半径、正方体的边长以及圆柱体的底面圆半径。由shape类派生出3个类,分别是正方体类、球体类和圆柱体类,圆柱体增加数据成员h,在这3个类中都具有求体积的重定义版本。在主函数中创建对象s1 ,c1 和c2,并调用各自的纯虚函数volume计算体积。

三、程序实现(完整程序) 程序1:

#include<iostream>

#include<cmath>

using namespace std;

class shuzu{

public:

shuzu() { } shuzu(int a[2][3]) { } shuzu operator+(int b[2][3]) { } shuzu operator-(int b[2][3]) { shuzu e; for(int i=0;i<2;i++) for(int j=0;j<3;j++) 2 for(int i=0;i<2;i++) for(int j=0;j<3;j++) c[i][j]=0; for(int i=0;i<2;i++) for(int j=0;j<3;j++) c[i][j]=a[i][j]; shuzu d; for(int i=0;i<2;i++) for(int j=0;j<3;j++) d.c[i][j]=c[i][j]+b[i][j]; return d;

e.c[i][j]=c[i][j]-b[i][j]; return e;

}

void show()

{

cout<<"运算结果为:"<<endl; for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

cout<<c[i][j]<<"\t"; cout<<endl;

}

private:

int c[2][3];

};

int main()

{

int m[2][3]={{3,7,5},{8,3,2}}; cout<<"原数组为:"<<endl; for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

cout<<m[i][j]<<"\t"; cout<<endl;

int n[2][3],k;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++) {

cin>>k;

n[i][j]=k;

}

cout<<"输入数组为:"<<endl; for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

cout<<n[i][j]<<"\t"; cout<<endl;

shuzu x(m);

shuzu y;

y=x+n;

y.show();

shuzu z;

z=x-n;

z.show();

getchar();

getchar();

return 0;

3

}

程序2: #include<iostream>

#include<stdlib.h>

using namespace std;

class complex{

public:

complex(double r=0,double i=0);

friend complex operator+(complex &a,complex &b); void print()

{

cout<<"("<<real<<","<<imag<<")"<<endl;

}

private:

double real;

double imag;

};

complex operator+(complex &a,complex &b)

{

complex c;

c.real=a.real+b.real;

c.imag=a.imag+b.imag;

return c;

}

complex::complex(double r,double i)

{

real=r;

imag=i;

}

int main()

{

complex c1(2.5,3.7);

system("color 2C");

cout<<"第一个复数:"<<endl;

c1.print();

complex c2(4.2,6.5);

cout<<"第二个复数:"<<endl;

c2.print();

complex d;

d=c1+c2;

cout<<"相加后结果:"<<endl;

d.print();

getchar();

getchar();

return 0;

}

4

程序3:

#include<iostream>

#include<stdlib.h>

using namespace std;

class Container{

public:

Container(double r1)

{r=r1;}

virtual void s_v()

{

cout<<"计算图形表面积和体积:"<<endl; }

protected:

double r;

};

class Sphere:public Container{

public:

Sphere(double r1):Container(r1)

{}

void s_v()

{

const double pi=3.14159262;

cout<<"球体的半径是:"<<r<<endl;

cout<<"表面积是"<<4*pi*r*r<<endl;

cout<<"体积是:"<<(4/3)*pi*r*r*r<<endl; }

};

class Cylinder:public Container{

public:

Cylinder(double r1,double h1):Container(r1) {

h=h1;

}

void s_v()

{

const double pi=3.14159262;

cout<<"圆柱体的半径是:"<<r<<"高是:"<<h<<endl; cout<<"表面积是"<<(2*pi*r*r+2*pi*r*h)<<endl; cout<<"体积是:"<<h*pi*r*r<<endl;

}

private:

double h;

};

class Cube:public Container{

public:

Cube(double r1):Container(r1)

5

{}

void s_v()

{

cout<<"正方体的边长是:"<<r<<endl; cout<<"表面积是"<<6*r*r<<endl; cout<<"体积是:"<<r*r*r<<endl; }

};

int main()

{

system("color 3D");

Sphere s1(2);

s1.s_v();

Cylinder c1(3,4);

c1.s_v();

Cube c2(5);

c2.s_v();

getchar();

getchar();

return 0;

}

程序4: #include<iostream>

#include<stdlib.h>

using namespace std;

class shape{

public:

shape(double x,double y)

{

a=x;

b=y;

}

virtual void area()=0;

protected:

double a;

double b;

};

class Circle:public shape{

public:

Circle(double x):shape(x,x)

{}

void area()

{

const double pi=3.14159262; cout<<"圆的半径是:"<<a<<endl; 6

cout<<"面积是:"<<pi*a*a<<endl;

}

};

class Rectangle:public shape{

public:

Rectangle(double x,double y):shape(x,y) {

}

void area()

{

const double pi=3.14159262;

cout<<"矩形的长是:"<<a<<"宽是:"<<b<<endl; cout<<"面积是:"<<a*b<<endl;

}

};

class Triangle:public shape{

public:

Triangle(double x,double y):shape(x,y) {}

void area()

{

cout<<"三角形的底是:"<<a<<"高是"<<b<<endl; cout<<"面积是:"<<0.5*a*b<<endl;

}

};

int main()

{

system("color 3D");

shape *ptr;

Circle c1(2);

Rectangle r1(7,8);

Triangle t1(6,8);

ptr=&c1;

ptr->area();

ptr=&r1;

ptr->area();

ptr=&t1;

ptr->area();

getchar();

getchar();

return 0;

}

程序5:

#include<iostream>

7

#include<stdlib.h>

using namespace std;

class shape{

public:

shape(double r1)

{r=r1;}

virtual void volume()=0;

protected:

double r;

};

class Sphere:public shape{

public:

Sphere(double r1):shape(r1)

{}

void volume()

{

const double pi=3.14159262;

cout<<"球体的半径是:"<<r<<endl;

cout<<"体积是:"<<(4/3)*pi*r*r*r<<endl; }

};

class Cylinder:public shape{

public:

Cylinder(double r1,double h1):shape(r1)

{

h=h1;

}

void volume()

{

const double pi=3.14159262;

cout<<"圆柱体的半径是:"<<r<<"高是:"<<h<<endl; cout<<"体积是:"<<h*pi*r*r<<endl;

}

private:

double h;

};

class Cube:public shape{

public:

Cube(double r1):shape(r1)

{}

void volume()

{

cout<<"正方体的边长是:"<<r<<endl;

cout<<"体积是:"<<r*r*r<<endl;

}

};

8

int main() {

面向对象程序设计实验报告实验三

}

system("color 3D"

面向对象程序设计实验报告实验三

); Sphere s1(2); s1.volume(); Cylinder c1(3,4); c1.volume(); Cube c2(5); c2.volume(); getchar(); getchar(); return 0;

四、调试与运行 程序1的运行结果:

程序2的运行结果:

程序3的运行结果: 9

程序4的运行结果:

面向对象程序设计实验报告实验三

程序5的运行结果:

面向对象程序设计实验报告实验三

五、实验总结:

1、学会了面向对象程序的多态性。掌握了运算符的重载和运用虚函数,当然对类的继承更加熟悉了,同时还学会了运用system函数调整运行界面的颜色,使运行结果更好看。

2、遇到了什么问题,如何解决的。

这次在写程序时,一时大意忘了写public,结果共有成员都变成私有的,在写主函数时,发现公有成员都无法访问。最后细心检查才发现问题。在写程序1时发现自己对运算符的重

面向对象程序设计实验报告实验三

10

载还不太熟悉,只能翻翻书熟悉在写了。

3、实验中需要特别注意什么,有哪些细节和技巧。

在写程序前先不要急着写,应该先弄清楚所写的程序要运用哪些基本知识,自己如果对哪些知识不熟悉就得巩固一下,在按照题目要求弄清这个程序的思路,在写这样会减少很多不必要的麻烦。

11

相关推荐