欢迎来到天天文库
浏览记录
ID:38580605
大小:100.50 KB
页数:6页
时间:2019-06-15
《C++ 实验多态性实验报告》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、贵州大学实验报告学院:电子信息学院专业:通信工程班级:姓名学号实验组5实验时间指导教师成绩实验项目名称多态性实验目的通过让学生进行实验,使其对于动态多态性有一个较为深入的了解和熟悉。最终可以熟练使用。实验要求1.编写4个重载函数Double(x),返回值为输入参数的两倍;参数类型分别为int、long、float、double,返回值类型与参数类型一样。2.请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea()和计算周长函数GetPerim()
2、。3.对类Point重载++(自增)、--(自减)运算符。实验原理VisualC++的编译环境下,独立完成实验要求的内容,独立完成编写、编译以及运行的过程实验仪器安装了VisualC++的PC机器实验步骤按照实验要求的内容逐一完成实验的要求。顺序是编写、编译、运行。实验内容1.编写4个重载函数Double(x),返回值为输入参数的两倍;参数类型分别为int、long、float、double,返回值类型与参数类型一样。2.请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算
3、对象面积的函数GetArea()和计算周长函数GetPerim()。3.对类Point重载++(自增)、--(自减)运算符。实验数据1、代码如下:#includeusingnamespacestd;intDouble(intx);longDouble(longx);floatDouble(floatx);doubleDouble(doublex);intmain(){intmyInt=6500;cout<4、ouble(myLong)<5、stream>#definePI3.1415926;usingnamespacestd;classShape//抽象类的定义{public:virtualdoubleGetArea()=0;//纯虚函数virtualdoubleGetPerim()=0;//纯虚函数};classRectangle:publicShape//矩形类,公有继承{public:Rectangle(doubleaa,doublebb)//带参数的构造函数{a=aa;b=bb;cout<<"长"<6、tualdoubleGetArea(){returna*b;}virtualdoubleGetPerim(){return2*(a+b);}private:doublea;doubleb;};classCircle:publicShape//圆类,公有继承{public:Circle(doublerr)//带参数的构造函数{r=rr;cout<<"半径"<7、*PI;}private:doubler;};voidmain(){doublelength,width;cout<<"输入长和宽:";cin>>length>>width;Rectanglerect(length,width);cout<<"面积是:"<>rr;Circlecir(rr);cout<<"面积是:"<8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
4、ouble(myLong)<5、stream>#definePI3.1415926;usingnamespacestd;classShape//抽象类的定义{public:virtualdoubleGetArea()=0;//纯虚函数virtualdoubleGetPerim()=0;//纯虚函数};classRectangle:publicShape//矩形类,公有继承{public:Rectangle(doubleaa,doublebb)//带参数的构造函数{a=aa;b=bb;cout<<"长"<6、tualdoubleGetArea(){returna*b;}virtualdoubleGetPerim(){return2*(a+b);}private:doublea;doubleb;};classCircle:publicShape//圆类,公有继承{public:Circle(doublerr)//带参数的构造函数{r=rr;cout<<"半径"<7、*PI;}private:doubler;};voidmain(){doublelength,width;cout<<"输入长和宽:";cin>>length>>width;Rectanglerect(length,width);cout<<"面积是:"<>rr;Circlecir(rr);cout<<"面积是:"<8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
5、stream>#definePI3.1415926;usingnamespacestd;classShape//抽象类的定义{public:virtualdoubleGetArea()=0;//纯虚函数virtualdoubleGetPerim()=0;//纯虚函数};classRectangle:publicShape//矩形类,公有继承{public:Rectangle(doubleaa,doublebb)//带参数的构造函数{a=aa;b=bb;cout<<"长"<6、tualdoubleGetArea(){returna*b;}virtualdoubleGetPerim(){return2*(a+b);}private:doublea;doubleb;};classCircle:publicShape//圆类,公有继承{public:Circle(doublerr)//带参数的构造函数{r=rr;cout<<"半径"<7、*PI;}private:doubler;};voidmain(){doublelength,width;cout<<"输入长和宽:";cin>>length>>width;Rectanglerect(length,width);cout<<"面积是:"<>rr;Circlecir(rr);cout<<"面积是:"<8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
6、tualdoubleGetArea(){returna*b;}virtualdoubleGetPerim(){return2*(a+b);}private:doublea;doubleb;};classCircle:publicShape//圆类,公有继承{public:Circle(doublerr)//带参数的构造函数{r=rr;cout<<"半径"<7、*PI;}private:doubler;};voidmain(){doublelength,width;cout<<"输入长和宽:";cin>>length>>width;Rectanglerect(length,width);cout<<"面积是:"<>rr;Circlecir(rr);cout<<"面积是:"<8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
7、*PI;}private:doubler;};voidmain(){doublelength,width;cout<<"输入长和宽:";cin>>length>>width;Rectanglerect(length,width);cout<<"面积是:"<>rr;Circlecir(rr);cout<<"面积是:"<8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
8、<<"周长是:"<classPoint{public:Point(intxx,intyy):x(xx),y(yy){}voiddisplay()const;Point&operator++();Pointoperator++(int);Point&operator
此文档下载收益归作者所有