面向对象程序设计上机作业_第1页
面向对象程序设计上机作业_第2页
面向对象程序设计上机作业_第3页
面向对象程序设计上机作业_第4页
面向对象程序设计上机作业_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、1编一个程序,程序中包含以下内容:(1) 一个圆类 (Circle) ,包含: 属性:园半径radius;常量:PI。方法:构造方法;求面积方法area();求周长方法:perimeter。;显示半径、面积和周长的方法 show()。(2) 主方法main(),在主方法中创建圆类的对象cl和c2并初始化,cl的半径为100,c1 的半径为 200,然后分别显示两个圆的半径、面积和周长。#includeusing namespace std;class Circle public:Circle(double r);double area();double perimeter();void sho

2、w();private:double radius; const double PI;Circle:Circle(double r):PI(3.14) radius=r;double Circle:area() return PI*radius*radius;double Circle:perimeter() return 2*PI*radius;void Circle:show() coutradius area() perimeter()endl;int main()Circle c1(100),c2(200); c1.show(); c2.show();return 0; 2编一个程序,

3、程序中包含以下内容:(1) 一个学生类(Student),包含:属性:学号 s_No,姓名s_Name,性别s_Sex,年龄s_Age, 成绩s_Scoreo方法:构造方法;显示学号、姓名、性别、年龄及成绩的方法show(),修改成绩的方法 modifyScore() 。( 2)普通方法 int max(Student* pStu) ,求出所有学生的最高分。(3) 主方法main(),在其中创建5个学生的对象数组 stu,然后调用 max()方法求出这5 个学生的最高分并输出其学号和成绩。#include#includeusing namespace std;class Studentpubl

4、ic:Student(string no,string name,string sex,int age,int score);void show();void modifyScore(int newScore);string s_No;string s_Name;string s_Sex;int s_Age;int s_Score;Student:Student(string no,string name,string sex,int age,int score)s_No=no;s_Name=name;s_Sex=sex;s_Age=age;s_Score=score;int max(Stud

5、ent* pStu,int n)int ms=pStu0.s_Score;for(int i=1;in;i+)if(mspStui.s_Score)ms=pStui.s_Score;return ms;void Student:show()couts_No s_Name s_Sex s_Age s_Scoreendl;void Student:modifyScore(int newScore)s_Score=newScore;int main()Student stu5=Student(100A,Zhangsan,Male,18,85),Student(100A,Zhangsan,Male,1

6、8,85),Student(100A,Zhangsan,Male,18,95),Student(100A,Zhangsan,Male,18,85), Student(100A,Zhangsan,Male,18,85);coutmax(stu,5)endl;return 0;1编写一个人类Person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。再编写一个学生类Student,它继承Person类,其中包含学号属性,包含构造方法以及显示学号的方法。最后编写main()函数,在 main()函数中定义两个学生si和s2并给他们赋值,最后显示他们的学号、姓名、性别以及

7、年龄。#include#includeusing namespace std;class Personpublic:Person(string n,string s,int a)name=n;sex=s;age=a;void show()coutnametsextageendl;private:string name;string sex;int age;class Student:public Personpublic:Student(string n,string s,int a,string id);void show();private:string stuID;Student:Stu

8、dent(string n,string s,int a,string id):Person(n,s,a)stuID=id;void Student:show()Person:show();coutstuIDendl;int main()Student s1(zhangsan,male,21,123456);s1.show(); return 0; 2编一个程序,包含以下内容:(1) Point类,该类放在两个文件中(Point.h, Point.cpp ),该类包含x和y两个坐标, 构造方法。(2) Circle类,该类放在两个文件中(Circle.h, Circle.cpp )该类继承 P

9、oint类,其 中包含:属性:圆半径 radius。方法:构造方法;求面积方法 area();求周长方法perimeter。(3) 圆柱体类 (Cylinder) ,继承圆类,其中包含: 属性:园柱体高度 height。方法:构造方法;求表面积方法 area();求体积方法volume。;显示圆心点坐标,底 圆面积和周长以及圆柱体表面积和体积的方法 show() 。(4) 主方法main(),在主方法中创建两个圆柱体类的对象cy1和cy2,具体尺寸自 己确定,然后分别显示圆柱体 cy1 和 cy2 圆心点坐标,底圆面积和周长以及圆柱体表面 积和体积。#include using namespa

10、ce std;class Point public:Point(int xx,int yy); void show();private:int x;int y;Point:Point(int xx,int yy) x=xx; y=yy;void Point:show()coutx yendl;class Circle:public Pointpublic:Circle(int xx,int yy,int r); double area(); double perimeter(); void show();private:int radius;Circle:Circle(int xx,int y

11、y,int r):Point(xx,yy) radius=r;double Circle:area() return 3.14*radius*radius;double Circle:perimeter() return 2*3.14*radius;void Circle:show() Point:show(); coutradius area() perimeter()endl;class Cylinder:public Circlepublic:Cylinder(int xx,int yy,int r,int h); double area(); double volume(); void

12、 show();private:int height;Cylinder:Cylinder(int xx,int yy,int r,int h):Circle(xx,yy,r) height=h;double Cylinder:area()return 2*Circle:area()+Circle:perimeter()*height; double Cylinder:volume()return Circle:area()*height;void Cylinder:show()Circle:show(); coutarea() volume()endl;int main()Cylinder c

13、y1(10,20,100,50),cy2(100,200,1000,500); cy1.show();cy2.show();return 0;1.编写一个复数类 Complex,其中包含私有整形变量real和imag ,构造函数以及显示属性的函数show(),实现两个复数进行+、-、*、/的运算符重载友元函数,实现前缀+和一的成员函数。#includeclass complex friend complex operator+(complex&,complex&);friend complex operator-(complex&,complex&);public:complex(double

14、 =0,double =0);void show();complex operator+();complex operator-();private:double real;double image;complex:complex(double r,double i)real=r;image=i;void complex:show()if(image0)coutreal + imageiendl;else if(image0)coutrealimageiendl;elsecoutrealendl;complex complex:operator+()+real;+image;return *t

15、his;complex complex:operator-()-real;-image;return *this;complex operator+(complex&temp1,complex&temp2)complex temp;temp.real=temp1.real+temp2.real; temp.image=temp1.image+temp2.image;return temp;complex operator-(complex&temp1,complex&temp2)complex temp;temp.real=temp1.real-temp2.real;temp.image=te

16、mp1.image-temp2.image;return temp;int main()complex cc1(178.26,59.65),cc2(23.6,10.25),cc3,cc4,cc5(21.456,0);cc1.show();cc2.show();cc3=cc1+cc2;cc4=cc1-cc2;+cc3;+cc4;cc3.show();cc4.show();cc5.show();return 0;1定义基类Base,其数据成员为高h,定义成员函数 disp()为虚函数。然后,再由基类派生出长方体类Cuboid与圆柱体类Cylinder。并在两个派生类中定义成员函数disp()。在主

17、函数中,用基类Base定义指针变量pc,然后用指针pc动态调用基类与派生类中的虚函数disp(),显示长方体与圆柱体的体积#includeusing namespace std;/class Basepublic:Base(int height)h=height;virtual void disp()couthendl;protected:int h;/class Cuboid:public Basepublic:Cuboid(int length,int width,int height):Base(height)l=length;w=width;int volume()return l*w

18、*h;void disp() coutCuboid volume: volume()endl;private:int l;int w;/class Cylinder:public Basepublic:Cylinder(int radius,int height):Base(height) r=radius;double volume()return 3.14*r*r*h;void disp() coutCylinder volume: volume()disp();pc=new Cuboid(20,30,40);pc-disp();pc=new Cylinder(50,60);pc-disp();return 0;2 编写一个函数模板,求数组中的最大值,并写出调用此函数模板的完整程序

温馨提示

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

评论

0/150

提交评论