C++上机练习题_第1页
C++上机练习题_第2页
C++上机练习题_第3页
C++上机练习题_第4页
C++上机练习题_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、.1.有一个圆环,其中小院半径为2.5,大圆半径为7。编程定义一个circle类,含有私有变量半径r,能够初始化r,计算圆面积。主函数中通过定义2个对象(大圆和小圆)来计算出圆环的面积。#includeclass circleprivate:float r;public: void SetR(float r0) r=r0; float area() return 3.14*r*r;void main()circle B,S;B.SetR(7);S.SetR(2.5);cout圆环的面积为:B.area()-S.area()endl;2.设计一个学生类stud,除了包括no(学号)、name(姓

2、名)和deg(成绩)数据成员外,有两个静态变量sum和num ,分别存放总分和人数,另有一个构造函数,一个普通成员函数disp()和一个静态成员函数avg(),用于计算平均分。本程序实现,已有若干个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。 #include #include #define N 3 class Stud int no; char name10; int deg; static int num; static int sum; public: void setdata(int n,char na,int d) no=n; deg=d; strcpy(na

3、me,na); sum+=d; num+; static double avg() return sum/num; void disp() printf( %-5d%-8s%3dn,no,name,deg); ; int Stud:sum=0; int Stud:num=0; void main() Stud stN; int i,n,d; char na10; for(i=0;iN;i+) printf(输入学号 姓名 成绩:); scanf(%d%s%d,&n,na,&d); sti.setdata(n,na,d); printf(输出数据n); printf( 学号 姓名 成绩n); f

4、or(i=0;iN;i+) sti.disp(); printf( 平均分=%gnn,Stud:avg(); 3.设有以下关于点point类的定义,请在此基础上派生出一个正方形square类,用以描述正方形左上角的位置、边长、能够计算正方形的面积。 #includeclass Pointpublic: void setxy(int x0,int y0) x=x0; y=y0; int getx() return x; int gety() return y;private: int x,y;class Square:public Pointpublic:void set_Square(int

5、a,int b,int c)setxy(a,b);length=c; int get_area()return (length*length);int get_length()return length;void print();private:int length;void Square:print()coutthe area is: get_area()endl;coutthe length is: get_length()endl;int main()Square b;b.set_Square (1,1,9);b.print();return 0;4.引用作为函数参数,实现在子函数中交换

6、形参的值,同时实现主调函数实参值,并输出交换结果。#include void Swap(int& a ,int& b);void main()int x(5) ,y(10);cout”x=”x y=yendl;Swap(x,y)cout”x=”x y=yendl;void Swap(int& a,int& b)Int t;t=a;a=b;b=t;5.定义一个钟表类,要求定义带参数的构造函数,同时定义时间设置函数和时间显示函数。#include class Clockpublic:void SetTime(int NewH,int NewM,int NewS);void ShowTime();p

7、rivate:int Hour,Minute,Second;void Clock:SetTime(int NewH,int NewM,int NewS)Hour=NewH;Minute=NewM;Second=NewS;void Clock:ShowTime()coutHour:Minute:Secondendl;void main()Clock c;c.SetTime(10,17,15);c.ShowTime();6C+编程实现打印小九九乘法口诀,要求按顺序逐行输出。#includevoid main() int i,j; for(i=1;i=9;i+) for(j=1;j=i;j+) co

8、utjxi=j*i ; coutendl; 7编写一个程序,定义一个职工类,输入3个职工的编号和姓名,然后显示出来。#include #include class Employeeprivate:int id;char name10;int age;public:void SetEmployee(int i,char *s,int g);void ShowEmployee();void Employee:SetEmployee(int i,char *s,int g)id=i;strcpy(name,s);age=g;void Employee:ShowEmployee()cout编号:ide

9、ndl;cout姓名:nameendl;void main()Employee s1;s1.SetEmployee(20091234,张三,18);s1.ShowEmployee();Employee s2;s2.SetEmployee(20094321,李四,19);s2.ShowEmployee();Employee s3;s3.SetEmployee(20092134,刘五,20);s3.ShowEmployee();8设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度,面积和颜色等数据。#include #

10、include class circledouble radius;public:circle(double r) radius=r;double getarea() return radius*radius*3.14;class tabledouble height;public:table(double h) height=h;double getheight()return height;class roundtable:public table,public circlechar *color;public:roundtable(double h,double r,char c):circle(r),table(h)color = new charstrlen(c + 1);strcpy(color,c);ch

温馨提示

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

评论

0/150

提交评论