第4次上机答案_第1页
第4次上机答案_第2页
第4次上机答案_第3页
第4次上机答案_第4页
第4次上机答案_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上C+第四次上机答案1. 1)建立一个教师类Teacher,其包含3个私有数据成员:num(工号)、name(姓名)、sex(性别),包含2个公有成员函数:带参构造函数(用于对三个数据成员进行初始化),display函数(输出数据成员)。2)建立一个生日类BirthDate,其包含3个私有数据成员:year(年)、month(月)、day(日),包含3个公有成员函数:带参构造函数(用于对三个数据成员进行初始化),display函数(输出数据成员),change函数(修改生日数据,即将3个数据成员的值改为change函数参数指定的值)。3)建立一个教授类Professor

2、,它是Teacher类的公有派生类,在Teacher类的基础上增加了私有数据生日信息birthday(birthday是BirthDate类的对象),其新增成员函数包含:带参构造函数(对新增的和继承的数据成员初始化),display函数(输出所有数据成员),changebirth函数(将修改生日信息修改为函数参数指定日期)。4)main函数中的操作步骤如下:l 定义Professor类的对象prof1l 输出对象prof1的所有数据信息l 修改prof1的生日信息l 输出对象prof1的所有数据信息#include <iostream>#include <string>

3、using namespace std;class Teacherpublic:Teacher(int, string, char);void display(); /输出数据private:int num; /工号string name; /姓名char sex; /性别;Teacher:Teacher(int n, string na, char s): num(n), name(na), sex(s) void Teacher:display() /输出数据cout<<"num:"<<num<<endl;cout<<&q

4、uot;name:"<<name<<endl;cout<<"sex:"<<sex<<endl;class BirthDatepublic:BirthDate(int, int, int);void display(); /输出年、月、日数据void change(int, int, int); /修改年、月、日数据private:int year; /年int month; /月int day; /日;BirthDate:BirthDate(int y, int m, int d): year(y), m

5、onth(m), day(d) void BirthDate:display() /输出年、月、日数据cout<<"birthday:"<<year<<'/'<<month<<'/'<<day<<endl;void BirthDate:change(int y, int m, int d) /修改年、月、日数据year = y;month = m;day = d;class Professor:public Teacherpublic:Professor(in

6、t, string, char, int, int, int);void display(); /输出数据void changebirth(int, int, int); /修改生日信息private:BirthDate birthday; /子对象;Professor:Professor(int n,string na,char s,int y,int m,int d): Teacher(n,na,s), birthday(y,m,d)void Professor:display() /输出数据Teacher:display(); /父子同名函数birthday.display(); /输出

7、生日信息void Professor:changebirth(int y,int m,int d) /修改生日信息birthday.change(y,m,d);void main()Professor prof1(1001,"Wangli",'f',1973,5,10);prof1.display(); /调用派生类自己的displaycout<<endl<<endl;prof1.changebirth(1975,10,12); /修改生日信息prof1.display(); /调用派生类自己的display2. 写一个程序,定义抽象

8、基类Shape,由它派生出3个派生类:Circle(圆形类,含一个数据成员:半径)、Rectangle(矩形类,含两个数据成员:长、宽)、Triangle(三角形类,含三个数据成员:三条边),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。法一:#include <iostream>#include <math.h>using namespace std;/定义抽象基类Shapeclass Shapepublic:virtual double area() =0; /纯虚函数;/定义Circle类class Circle: publi

9、c Shapepublic:Circle(double r): radius(r) /构造函数virtual double area() /virtual可以省略 return 3.14159*radius*radius; ; /定义虚函数private:double radius; /半径;/定义Rectangle类class Rectangle: public Shapepublic:Rectangle(double len, double w): length(len), width(w) /构造函数virtual double area() return length*width; /

10、定义虚函数private:double length, width; /长与宽;/定义Triangle类class Triangle:public Shapepublic:Triangle(double x, double y, double z): a(x), b(y), c(z) /构造函数virtual double area() /定义虚函数double s=(a+b+c)*0.5;return sqrt( s*(s-a)*(s-b)*(s-c) ); private:double a,b,c; /三条边;/输出面积的函数void printArea(Shape &s) /基类

11、的引用对象作为形参,实参可以是子类对象。引用对象比指针变量节约空间cout<<s.area()<<endl; /area是虚函数,调用的是实参对象所属类的areaint main()Circle circle(12.6); /建立Circle类对象circlecout<<"area of circle =" printArea(circle); /输出circle的面积Rectangle rectangle(4.5,8.4); /建立Rectangle类对象rectanglecout<<"area of rectan

12、gle =" printArea(rectangle); /输出rectangle的面积Triangle triangle(4,8,5); /建立Triangle类对象 cout<<"area of triangle ="printArea(triangle); /输出triangle的面积return 0;法二:#include <iostream>#include <math.h>using namespace std;/定义抽象基类Shapeclass Shapepublic:virtual void printArea(

13、) =0; /纯虚函数;/定义Circle类class Circle: public Shapepublic:Circle(double r): radius(r) /构造函数virtual void printArea() /virtual可以省略 cout<<3.14159*radius*radius<<endl; ; /定义虚函数private:double radius; /半径;/定义Rectangle类class Rectangle: public Shapepublic:Rectangle(double len, double w): length(len

14、), width(w) /构造函数virtual void printArea() cout<<length*width<<endl; /定义虚函数private:double length, width; /长与宽;/定义Triangle类class Triangle:public Shapepublic:Triangle(double x, double y, double z): a(x), b(y), c(z) /构造函数virtual void printArea() /定义虚函数double s=(a+b+c)*0.5;cout<<sqrt( s

15、*(s-a)*(s-b)*(s-c) )<<endl; private:double a,b,c; /三条边;int main()Shape *pc; /基类指针变量Circle circle(12.6); /建立Circle类对象circlepc=&circle;cout<<"area of circle =" pc->printArea(); /输出circle的面积Rectangle rectangle(4.5,8.4); /建立Rectangle类对象rectanglepc=&rectangle;cout<<

16、"area of rectangle =" pc->printArea(); /输出rectangle的面积Triangle triangle(4,8,5); /建立Triangle类对象 pc=&triangle;cout<<"area of triangle ="pc->printArea(); /输出triangle的面积return 0;自主上机题目:2015年C+A卷的第五题(编程题)。定义具有继承关系的点类Point和圆类Circle:(1)Point类具有(x, y)坐标对;(2) Circle类为Point

17、类的子类,它本身又增加了半径radius、求周长getPeri ()和求面积getArea()的成员函数;(3)为Point类和Circle类添加构造函数;(4)在主函数中创建圆A,其圆心坐标为(5,7),半径为2,计算并输出圆A的周长和面积。#include <iostream.h>#define PI 3.14class Pointfloat x,y;public:Point(float xp=0,float yp=0):x(xp),y(yp);class Circle: public Pointfloat radius; /半径public:Circle(float x, float y, float r): Point(x,y), radius(r)float getPeri(); /求周长float g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论