资源预览内容
第1页 / 共15页
第2页 / 共15页
第3页 / 共15页
第4页 / 共15页
第5页 / 共15页
第6页 / 共15页
第7页 / 共15页
第8页 / 共15页
第9页 / 共15页
第10页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
章 多态性与虚函数1.在例 12.1程序基础上作一些修改。定义 Point ./Point.h#ifndef POINT_H#define POINT_H using namespace std;class Point;ostream & operator#includePoint.hPoint:Point(float a,float b)x=a;y=b;void Point:setPoint(float a,float b)x=a;y=b;ostream & operator#includeCircle.hCircle: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() constreturn float(3.14159*radius*radius);ostream &operator#includeCylinder.hCylinder: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 float(2*Circle:area()+2*3.14159*radius*height);float Cylinder:volume() constreturn Circle:area()*height;ostream &operator#includePoint.h#includeCircle.h#includeCylinder.hvoid main()Cylinder cy1(5,4,2,10);cout把构造函数修改为带参数的函数,在建立对象时初始化。#include using namespace std;class Pointpublic:Point(double a,double b):x(a),y(b)Point()cout先不将析构函数声明为 virtual,在 main函数中另设一个指向 Circle类对象的指针变量.#include using namespace std;class Pointpublic:Point(double a,double b):x(a),y(b)Point()cout不作第(2)点的修改而将析构函数声明为 virtual,运行程序,分析结果#include using namespace std;class Pointpublic:Point(double a,double b):x(a),y(b)virtualPoint()coutusing namespace std;class Shapepublic:virtual double area() const =0;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double area() const return 3.14159*radius*radius;protected:double radius; ;class Rectangle:public Shapepublic:Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected:double width,height; ;class Triangle:public Shapepublic: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)coutusing namespace std;class Shapepublic:virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r)virtual double area() const return 3.14159*radius*radius;protected:double radius; ;class Square:public Shapepublic:Square(double s):side(s) virtual double area() const return side*side; protected:double side;class Rectangle:public Shapepublic:Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected:double width,height; ;class Trapezoid:public Shapepublic:Trapezoid(double t,double b,double h):top(t),bottom(t),height(h)virtual double area() const return 0.5*(top+bottom)*height;protected:double top,bottom,height;class Triangle:public Shapepublic:Triangle(double w,double h):width(w),height(h)virtual double area() const return 0.5*width*height;protected:double width,height; ;int main()Circle circle(12.6); Square square(3.5); Rectangle rectangle(4.5,8.4); Trapezoid trapezoid(2.0,4.5,3.2); Triangle triangle(4.5,8.4); Shape *pt5=&circle,&square,&rectangle,&trapezoid,▵ double areas=0.0; for(int i=0;iarea();cout#include using namespace std;int main()double a,b,c,s,area;coutabc;if (a+b#include using namespace std;int main()float a5;coutai;coutusing namespace std;void main() int i,j,k;for(i=0;i#include using namespace std;void fun1()int a10;ofstream outfile1(f1.dat),outfile2(f2.dat);if(!outfile1)cerrai;outfile1ai;outfile2a; outfileai; for(i=0;iaj+1)t=aj;aj=aj+1;aj+1=t;infile.close(); ofstream outfile(f2.dat,ios:out);if(!outfile)cerr#include using namespace std;struct staffint num;char name20;int age;double pay;int main()staff staf7=2101,Li,34,1203,2104,Wang,23,674.5,2108,Fun,54,778,3006,Xue,45,476.5,5101,Ling,39,656.6,staf1;fstream iofile(staff.dat,ios:in|ios:out|ios:binary);if(!iofile)cerrstaf1.numstaf1.namestaf1.agestaf1.pay;iofile.seekp(0,ios:end);iofile.write(char *)&staf1,sizeof(staf1);iofile.seekg(0,ios:beg);for(i=0;inum;while(num)find=false;iofile.seekg(0,ios:beg);for(i=0;inum;iofile.close();return 0;6.在例 13.17的基础上修改程序,将存放在 C数组中的数据读入并显示出来。#include #include using namespace std;struct studentint num;char name20;double score;int main()int i;student stud3=1001,Li,78,1002,Wang,89.5,1004,Fun,90,stud13;char c50;strstream strio(c,50,ios:in|ios:out);for(i=0;istud1i.numstud1i.namestud1i.score;cout#include #include using namespace std;struct studentint num;char name20;double score;int main()student stud3=1001,Li,78,1002,Wang,89.5,1004,Fun,90,stud13;char c50;int i;ostrstream strout(c,50);for(i=0;istud1i.numstud1i.namestud1i.score;coutdata from array c to array stud1:endl;for(i=0;i3;i+)coutstud1i.num stud1i.name stud1i.scoreendl;coutendl;return 0;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号