c++实验上机作业整理.doc_第1页
c++实验上机作业整理.doc_第2页
c++实验上机作业整理.doc_第3页
c++实验上机作业整理.doc_第4页
c++实验上机作业整理.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

*2编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:(1)定义一个基类圆,至少含有一个数据成员半径;(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。(3)定义主函数,求球、圆柱、圆锥的和体积。#includeusing namespace std;#define pi 3.14159class circleprotected:int banjing;public:circle(int R):banjing(R)void show()cout半径:banjingendl;class qiu:virtual public circleprotected:float s1;float v1;public:qiu(int R):circle(R)s1=4*pi*R*R;v1=4/3*pi*R*R*R;void show()circle:show();cout球的表面积为:s1endl;cout球的体积为:v1endl;coutendl;class yuanzhu:virtual public circleprotected:float s2;float v2;int hight;public:yuanzhu(int R,int h):circle(R),hight(h)s2=pi*R*R+4*pi*R*h;v2=pi*R*R*h;void show()circle:show();cout圆柱的高为:hightendl;cout圆柱的表面积为:s2endl;cout圆柱的体积为:v2endl;coutendl;class yuanzhui:public yuanzhu protected:float s3;float v3;public:yuanzhui(int R,int h):circle(R),yuanzhu(R,h)circle:show();s3=pi*R*R+38.0;v3=1.0/3*pi*R*R*h;void show()cout圆锥的高为:hightendl;cout圆锥的表面积为:s3endl;cout圆锥的体积为:v3endl;coutendl;int main() double R; double h; cout球半径R; qiu qiu(R); qiu.show(); cout圆柱半径R; cout圆柱高度h; yuanzhu yzu(R,h); yzu.show(); cout圆锥半径R; cout圆锥高度h; yuanzhui yzui(R,h); yzui.show(); system(PAUSE); return 0;*1. 编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。#include#includeusing namespace std;class personprotected:int number;char name20;public:person(char nm,int num):number(num)strcpy(name,nm);void show() constcout编号:numberendl;cout姓名:nameendl;class student:virtual public personprotected:int banji;int chengji;public:student(char nm,int num,int bj,int cj):person(nm,num),banji(bj),chengji(cj)void show() constperson:show();cout班级:banjiendl;cout成绩:chengjiendl;coutendl;class teacher:virtual public personprotected:char zhicheng20;char bumen20;public:teacher(char nm,int num,char zc,char bm):person(nm,num)strcpy(zhicheng,zc);strcpy(bumen,bm); void show() constperson:show();cout职称:zhichengendl;cout部门:bumenendl;coutendl;int main(void) char nm20; int num;int bj;int cj;cout请输入学生的姓名,编号,班级,成绩:nmnumbjcj;student s(nm,num,bj,cj);s.show();char zc20,bm20;cout请输入老师的姓名,编号,职称,部门:nmnumzcbm;teacher t(nm,num,zc,bm);t.show();return 0;*3. 定义日期类型Date。要求有以下成员:(1)可以设置日期;(2)日期加一天操作;(3)输入函数,输入格式为:XXXX XX XX,如2010 4 13;(4)输出函数,输出格式为:XXXX年XX月XX日,如2010年4月11日。#includeusingnamespacestd;classdateprivate:intyear;intmonth;intday;public:voidcoutin()inty,m,d;cinymd;year=y;month=m;day=d;voidnextday()if(day=29)&(year%4=0&(year%100!=0|year%400=0)&(month=2)month+;day=1;elseif(day=28)&(year%4!=0&(year%100=0|year%400!=0)&(month=2)month+;day=1;elseif(day=31)&(month=1)|(month=3)|(month=5)|(month=7)|(month=8)|(month=10)month+;day=1;elseif(day=30)&(month=4)|(month=6)|(month=9)|(month=11)month+;day=1;elseif(month12)year+;month=1;day=1;elseday=day+1;coutyear年month月day日endl;intmain()datetext;text.coutin();text.nextday();system(PAUSE);return0;*4. 设计一个double类型的数组类CDBLArray,要求CDBLArray可以进行如下操作:(1)可以重置数组的大小(Resize)(2)可以通过下标返回数组元素,并对下标越界情况进行检查(3)可以利用已知数组对象对整个数组赋值和初始化(4)可以返回当前数组的大小(Size)即,能够使如下程序正常运行(无编译和运行时错误)。void main()double a8 = 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8;CDBLArray ary1(5), ary2(a, 5);CDBLArray ary3 = ary2;Ary2.Resize(8);cout ary3.Size() endl;/输出:8int i;for (i = 5; i 8; i+)ary2i = ai;for (i = 0; i 8; i+)cout ary2i ;cout endl;/输出:1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8ary3 = ary2;for (i = 0; i 8; i+)cout ary3i ;cout endl;/输出:1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8cout ary38 endl;/输出:越界#includeusingnamespacestd;classCDBLArrayprivate:double*arr;intsize;public:intgetSize()returnsize;CDBLArray(ints)size=s;arr=NULL;arr=newdoubles;inti;for(i=0;is;i+)arri=0;CDBLArray(double*a,intn_s)size=n_s;arr=NULL;inti;arr=newdoublesize;for(i=0;in_s;i+)arri=ai;CDBLArray(constCDBLArray©)size=copy.size;arr=NULL;if(arr!=NULL)deletearr; arr=NULL;arr=newdoublesize;for(inti=0;icopy.size;i+)arri=copy.arri; CDBLArray()if(arr!=NULL)deletearr;arr=NULL;voidResize(intReSize)double*tmp=newdoublesize;inttmpsize=size;if(arr!=NULL)for(inti=0;isize)size=ReSize;for(intj=0;jtmpsize;j+)newarrj=tmpj;for(intk=tmpsize;kReSize;k+)newarrk=0;arr=newarr;CDBLArray&operator=(constCDBLArray©)/运算符“=”的重载if(arr!=NULL&this!=©)deletearr;arr=NULL;size=copy.size;arr=newdoublesize;for(inti=0;i=size)cout访问越界endl;exit(1);returnarrindex;intmain()doublea8=1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8;CDBLArrayary1(5),ary2(a,5);CDBLArrayary3=ary2;ary2.Resize(8);coutary2.ge

温馨提示

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

评论

0/150

提交评论