实验8多态性与虚函数程序代码
- 格式:doc
- 大小:46.00 KB
- 文档页数:6
C++中的封装、继承、多态理解封装(encapsulation):就是将抽象得到的数据和⾏为(或功能)相结合,形成⼀个有机的整体,也就是将数据与操作数据的源代码进⾏有机的结合,形成”类”,其中数据和函数都是类的成员。
封装的⽬的是增强安全性和简化编程,使⽤者不必了解具体的实现细节,⽽只是要通过外部接⼝,特定的访问权限来使⽤类的成员。
封装可以隐藏实现细节,使得代码模块化。
继承(inheritance):C++通过类派⽣机制来⽀持继承。
被继承的类型称为基类或超类,新产⽣的类为派⽣类或⼦类。
保持已有类的特性⽽构造新类的过程称为继承。
在已有类的基础上新增⾃⼰的特性⽽产⽣新类的过程称为派⽣。
继承和派⽣的⽬的是保持已有类的特性并构造新类。
继承的⽬的:实现代码重⽤。
派⽣的⽬的:实现代码扩充。
三种继承⽅式:public、protected、private。
继承时的构造函数:(1)、基类的构造函数不能被继承,派⽣类中需要声明⾃⼰的构造函数;(2)、声明构造函数时,只需要对本类中新增成员进⾏初始化,对继承来的基类成员的初始化,⾃动调⽤基类构造函数完成;(3)、派⽣类的构造函数需要给基类的构造函数传递参数;(4)、单⼀继承时的构造函数:派⽣类名::派⽣类名(基类所需的形参,本类成员所需的形参):基类名(参数表) {本类成员初始化赋值语句;};(5)、当基类中声明有默认形式的构造函数或未声明构造函数时,派⽣类构造函数可以不向基类构造函数传递参数;(6)、若基类中未声明构造函数,派⽣类中也可以不声明,全采⽤缺省形式构造函数;(7)、当基类声明有带形参的构造函数时,派⽣类也应声明带形参的构造函数,并将参数传递给基类构造函数;(8)、构造函数的调⽤次序:A、调⽤基类构造函数,调⽤顺序按照它们被继承时声明的顺序(从左向右);B、调⽤成员对象的构造函数,调⽤顺序按照它们在类中的声明的顺序;C、派⽣类的构造函数体中的内容。
继承时的析构函数:(1)、析构函数也不被继承,派⽣类⾃⾏声明;(2)、声明⽅法与⼀般(⽆继承关系时)类的析构函数相同;(3)、不需要显⽰地调⽤基类的析构函数,系统会⾃动隐式调⽤;(4)、析构函数的调⽤次序与构造函数相反。
实验10 多态性一.实验目的1.掌握用成员函数重载运算符的方法2.掌握用友元函数重载运算符的方法3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法二.实验内容与步骤1.上机实验题一定义一个复数类,通过重载运算符:*,/,直接实现二个复数之间的乘除运算。
编写一个完整的程序,测试重载运算符的正确性。
要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。
⑴分析两复数相乘的计算公式为:(a+b i)*(c+d i)=(ac–bd )+(ad+bc) i两复数相除的计算公式为:(a+b i)/(c+d i)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d) i复数类及运算符重载函数可定义为:class Complex{ float Real, Image;public:Complex(float r=0,float i=0) { Real=r;Image=i;}void Show(){cout <<"Real="<<Real<<'\t'<<"Image="<<Image<<'\n';}friend Complex operator *(Complex &, Complex &);Complex operator /(Complex &); //重载运算符+};Complex operator *( Complex &c1,Complex &c2){ Complex t;t.Real=c1.Real * c2.Real - c1.Image * c2.Image;t.Image = c1.Image*c2.Real +c1.Real* c2.Image;return t;}Complex Complex::operator /(Complex &c){ Complex t;t.Real =(Real *c.Real+ Image * c.Image)/(c.Real*c.Real+ c.Image * c.Image);t.Image = (Image *c.Real - Real * c.Image)/(c.Real*c.Real+ c.Image * c.Image);return t;}⑵上机要求增加重载复数的加法和减法运算符的功能,实现两个复数的加法,一个复数与一个实数的加法;两个复数的减法,一个复数与一个实数的减法。
《面向对象程序设计》实验指导书(新)《面向对象程序设计》实验指导书一、课程教学与实验教学计划学时比:48/16二、适用专业:信息管理与信息系统三、实验目的基本要求能够充分理解面向对象程序设计的思想和应用方法。
能够使用面向对象编程语言进行相应的程序设计和开发。
理解面向对象的基本思想、概念和特性以及面向对象的分析、建模、设计技术与方法。
掌握C++语言基本构成、类与对象、消息传递与函数、函数与运算符重载、继承性、多态性与虚拟函数、数据封装和隐藏及Windows 程序设计基础。
通过实验动手实践,使学生能够掌握面向对象程序设计的特征和基本思想,以及相应的具体实现和Windows程序设计基础知识。
四、实验内容实验一:循环控制(一)实验目的要求:熟悉VC++集成开发环境,学习使用控制台应用程序;创建工程,建立可执行文件并执行,观察结果。
掌握变量的声明和定义方法,掌握循环语句和条件语句的定义、作用和使用方法。
(二)实验设备:1.服务器;2.交换机;3.计算机。
(三)实验内容:1.编程求1!+2!+3!+4!+…+12!。
2.编程求所有的3位数素数,且该数是对称的。
所谓“对称”是指一个数,倒过来还是该数。
例如,375不是对称数,因为倒过来变成了573。
实验二:递归函数(一)实验目的要求:掌握函数的概念和使用方法,掌握递归函数的概念和使用方法。
(二)实验设备:1.服务器;2.交换机;3.计算机。
(三)实验内容:1.用递归函数来求1!+2!+3!+…+12!。
2.用递归的方法建立一个函数int fibonacci(int n),求Fibonacci数列中第n个数的值。
实验三:用气泡法排序(一)实验目的要求:掌握文件读写的方法,掌握递归函数的概念和使用方法。
(二)实验设备:1.服务器;2.交换机;3.计算机。
(三)实验内容:建立一个文件,文件中包含一系列数,用这些数构成一个数组,并按照数值,为这个数组从小到大排序,把排序结果输出到另一个文件中。
《面向对象程序设计》实验指导书C++语言上机操作基本与要求:(1)计算机的硬件配置目前个人电脑的配置一般都很高,P2或者是P3的CPU,内存基本上都是128兆或者以上,符合C++的最低要求。
最低的配置只要286或者386以上的主机,32兆内存即可满足要求。
(2)计算机的软件配置1、操作系统Windows95及以上。
2、Borland C++ 3.1或者5.0;或者是Visual C++5.0获6.0。
建议大家用Borland C++3.1,因为VC的操作很复杂,需要编程者对它有足够的了解,用起来不如BC方便。
编程时只需要打开桌面上BC3.1快捷方式即可进行编程、编译、运行和调试程序了。
实验一练习设计、使用类并熟悉编程环境实验目的:学会定义类学会构造类的方法领会面向对象程序设计的方法熟悉编程环境实验内容:(1)设计一个用来表示直角坐标系上点的位置的Location类,然后在主程序中创建两个对象A和B,要求A在第三象限,B在第二象限,计算给定两点之间的距离并按如下格式输出结果:A(x1,y1),B(x2,y2)Distance=d其中x1,y1,x2,y2为指定值,d为计算结果。
分析:这是一个很简单的类的设计。
首先我们要清楚这个类要有几个成员和成员函数。
一个点起码要有X坐标和Y坐标,所以我们给其添加两个私有成员;接着要有起码的构造函数和析构函数;最后根据题目要求,我们在设计一个成员函数,用来求两点之间的距离。
程序源代码如下:程序头文件放在Location.h中//Location.h//这是输入输出流所要包含的头文件#include <iostream.h>//这是要进行数学计算所要包含的头文件#include <math.h>class Location{//横坐标和纵坐标:private:int X,Y;public:Location( int a , int b);//构造函数double distance (Location &);//求两点距离的函数~Location();//析构函数};//在构造函数中对类的成员进行初始化Location::Location(int a ,int b){X=a;Y=b; }//求两点的距离//这里我们用的公式是,两点之间的//距离=//(x1,y1)和(x2,y2)为两点的坐标double Location::distance(Location & loc1){double length:length=sqrt((loc1.X-Location::X)**2+(loc1.Y-Location::Y)**2));//注意这里的作用域符号::表示X和Y是属//于Location类的return length; }//由于我们在这个程序里没有用//new来开辟内存,所以析构函数为空Location::~Location(){ }再在Location.cpp文件中编写主程序//Location.cpp#include <Location.h>#include <iostream.h>#include <math.h>void main(){//先定义Location类的两个对象A和B//并调用构造函数对其进行初始化Location A(-10,-20),B(-30,40);//输出A(x,y),B(x,y)cout<<"A("<<A.X<<","<<A.Y<<"),"<<"B("<<B.X<<","<<B.Y<<")"<<endl;//输出distance=?cout<<"Distance="<<A.distance(B)<<endl;}注意:.h文件和.cpp文件要放在同一个目录下面,如果不是放在同一个目录下面的话,那么在.cpp文件中要在#include <Location.h>语句中加上Location.h文件所在地的整个目录名,例如:#include<d:\Borland\user\location\Location.h>,这样的话,编译时才会不出错。
实验八多态性与虚函数参考程序代码1.程序8-1文件一:#include<iostream>//如用VC++应改为∶#include <iosttram.h>using namespace std; //如用VC++应取消此行#include"cylinder.h"#include"point.cpp"#include"circle.cpp"#include"cylinder.cpp"int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r="<<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder: \n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}文件二://CYLINDER.H#include"circle.h"class Cylinder:public Circle{public:Cylinder (float x=0,float y=0,float r=0,float h=0);void setHeight(float);float getHeight() const;float area() const;float volume() const;friend ostream& operator<<(ostream&,const Cylinder&);protected:float height;};文件三://POINT.CPPPoint::Point(float a,float b){x=a;y=b;}void Point::setPoint(float a,float b){x=a;y=b;}ostream & operator<<(ostream &output,const Point &p){output<<"["<<p.x<<","<<p.y<<"]"<<endl;return output;}文件四://CIRCLE.CPP//#include <iostream.h>Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}void Circle::setRadius(float r){radius=r;}float Circle::getRadius() const {return radius;}float Circle::area() const{return 3.14159*radius*radius;}ostream &operator<<(ostream &output,const Circle &c){output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<",area="<<c.area()<<endl;return output;}文件五://CYLINDER.CPPCylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}void Cylinder::setHeight(float h){height=h;}float Cylinder::getHeight() const {return height;}float Cylinder::area() const{ return 2*Circle::area()+2*3.14159*radius*height;}float Cylinder::volume() const{return Circle::area()*height;}ostream &operator<<(ostream &output,const Cylinder& cy){output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height<<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;return output;}文件六://CIRCLE.H#include"point.h"class Circle:public Point{public:Circle(float x=0,float y=0,float r=0);void setRadius(float);float getRadius() const;float area () const;friend ostream &operator<<(ostream &,const Circle &);protected:float radius;};2.程序8-2解一:#include<iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;}private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){}~Circle(){cout<<"executing Circle destructor"<<endl;}private:float radus;};int main(){Point *p=new Circle(2.5,1.8,4.5);delete p;return 0;}解二:#include<iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;}private:float x;float y;};class Circle:public Point{public:Circle(int a,int b,int r):Point(a,b),radius(r){}~Circle(){cout<<"executing Circle destructor"<<endl;}private:float radus;};int main(){Point *p=new Circle(2.5,1.8,4.5);Circle *pt=new Circle(2.5,1.8,4.5);delete pt;return 0;}解三:#include<iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}virtual ~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){} virtual ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radus;};{Point *p=new Circle(2.5,1.8,4.5);delete p;}3.程序8-3#include<iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数protected:double radius; //半径};//定义Rectangle类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return width*height;} //定义虚函数protected:double width,height; //宽与高};class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return 0.5*width*height;} //定义虚函数protected:double width,height; //宽与高};//输出面积的函数void printArea(const Shape &s){cout<<s.area()<<endl;} //输出s的面积{Circle circle(12.6); //建立Circle类对象circle cout<<"area of circle =";printArea(circle); //输出circle的面积Rectangle rectangle(4.5,8.4); //建立Rectangle类对象rectanglecout<<"area of rectangle =";printArea(rectangle); //输出rectangle的面积Triangle triangle(4.5,8.4); //建立Triangle类对象cout<<"area of triangle =";printArea(triangle); //输出triangle的面积return 0;}。