c++实验答案1_第1页
c++实验答案1_第2页
c++实验答案1_第3页
c++实验答案1_第4页
c++实验答案1_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

1、1 1.声明一个 Circle 类,有 1)数据成员 Radius(半径) 2)成员函数 GetValue()用来给 半径赋值 3)成员函数 GetArea()计算圆的 面积 在主函数中创建一个 Circle 类的 对象进行测试,显示出面积。 #include using namespace std; class circle private: int radius; public: void getvalue() coutradius; double getarea() return 3.14*radius*radius; void output() cout半径 是:radius,面积是:

2、 getarea(); ; int main() circle circle1; double radius; circle1.getvalue(); circle1.getarea(); circle1.output(); 输入半径:2 半径是:2,面积是:12.56 2.声明一个 tree 类,有 1)数据成员 ages(树龄) 2)成员函数 grow(int years)对 ages 加上 years 3)成员函数 age()显示对象的数 据成员 ages 的值 4)构造函数 tree(int n=0)进行初 始化 在主函数中创建一个 tree 类的对 象进行测试(创建一个树龄为 18

3、的对象,调用 age()显示树龄,之 后调用 grow(4) ,生长 4 年,再 显示树龄) 。 2: #include using namespace std; class tree public: tree(int a) ages=a; void grow(int years); void age(); private: int ages; ; void tree:grow(int years) coutyears; ages+=years; void tree:age() cout 该 树 年 龄 为 : agesendl; int main() tree t1(5); t1.age()

4、; t1.grow(4); t1.age(); system(pause); return 0; 该树年龄为:5 请输入年份:5 该树年龄为:10 3. 声明一个名为 Rectangle 的矩 形类,其属性为矩形的左下角和 右上角两点的坐标,并有成员函 数计算矩形的周长及面积。编程 实现求左下角与右上角坐标分别 为(2.1,3.2) , (5.2,6.3)的 矩形周长及面积。 #include#include #include#include classclass RectangleRectangle private:private: doubledouble left;left; doubl

5、edouble bottom;bottom; doubledouble right;right; doubledouble top;top; public:public: voidvoidSetPoint(doubleSetPoint(double l,doublel,double b,doubleb,double r,doubler,double t)t) left=l;left=l; bottom=b;bottom=b; right=r;right=r; top=t;top=t; voidvoid cal(doublecal(double left,doubleleft,double bo

6、ttom,doublebottom,doubleright,doubleright,double top)top) doubledouble s;s; s=abs(left-right)*abs(bottoms=abs(left-right)*abs(bottom -top);-top); coutcout该长方形的面积为:该长方形的面积为: sendl;sendl; voidvoidzhou(doublezhou(double left,doubleleft,doublebottom,doublebottom,double right,doubleright,double top)top)

7、doubledouble z;z; z=2*(abs(left-right)+abs(botz=2*(abs(left-right)+abs(bot tom-top);tom-top); coutcout该长方形的周长为:该长方形的周长为: 2 zendl;zendl; voidvoidGetp1(doubleGetp1(double left,doubleleft,double bottom)bottom) coutcout长方形的左下角坐标长方形的左下角坐标 是:是: left,bottomendl;left,bottomendl; voidvoidGetp2(doubleGetp2(do

8、uble right,doubleright,double top)top) coutcout长方形的右上角坐标长方形的右上角坐标 是是: right,topendl;right,topendl; ; voidvoid main()main() RectangleRectangle r1;r1; doubledouble l,b,r,t;l,b,r,t; coutcoutlb;cinlb; coutcoutrt;cinrt; r1.Getp1(l,b);r1.Getp1(l,b); r1.Getp2(r,t);r1.Getp2(r,t); r1.cal(l,b,r,t);r1.cal(l,b,

9、r,t); r1.zhou(l,b,r,t);r1.zhou(l,b,r,t); 请输入长方形的左下角的坐标:请输入长方形的左下角的坐标: 2.12.13.23.2 请输入长方形的右上角的坐标:请输入长方形的右上角的坐标: 5.25.26.36.3 长方形的左下角的坐标:长方形的左下角的坐标:2.12.1 3.23.2 长方形的右上角的坐标:长方形的右上角的坐标:5.25.2 6.36.3 1.编写一个简单复数类 Scomplex,要求用友元 函数重载“+”、“-”运算 符, 用成员函数重载“=” 运算符,使之能够实现 整数或浮点数和复数的 加法和减法,并且进行 测试。 1. #include

10、 using namespace std; class Scomplex private: double real; double image; public: Scomplex(double x = 0, double y = 0 ); friendScomplex operator+(inta, Scomplex b ); friendScomplex operator-(inta, Scomplex b ); friendScomplex operator+( double a, Scomplex b ); friendScomplex operator-( double a, Scom

11、plex b ); Scomplex void print(); ; Scomplex:Scomplex( d ouble x, double y ) real = x; image = y; Scomplex operator+( int a, const Scomplex b ) Scomplex temp; temp.real = a + b.real; temp.image= b.image; return temp; Scomplex operator+( double a, const Scomplex b ) Scomplex temp; temp.real = a + b.re

12、al; temp.image= b.image; return temp; Scomplex operator-( double a, const Scomplex b ) Scomplex temp; temp.real = a - b.real; temp.image= -b.image; return temp; Scomplex operator-( int a, const Scomplex b ) Scomplex temp; temp.real = a - b.real; temp.image= -b.image; return temp; Scomplex real = b.r

13、eal; image = b.image; return *this; void Scomplex:print() cout = 0 ) cout +; cout image i endl; int main() Scomplex t1(1, 2), t2(3, 4), t3; t3 = 1 + t2; t3.print(); t3 = 2 - t2; t3.print(); t3 = t1; t3.print(); return 0; 4+4i -1-4i 1+2i 2.空间一点 p 的坐标为 (x,y,z),其中 x,y,z 为 整 数 。 编 写 点 类 Point3D,定义空间两点 之

14、间的加”+”,减”-”运 算为相应三个坐标值分 别进行加、减运算,要 求实现空间两点之间的 加”+”减”-”赋值”=”运 算,空间两点间的比 较”= =”运算。要求编写 Point3D 类的声明定义 和测试程序。 #include using namespace std; class Point3D private: int x, y, z; public: Point3D( int a = 0, int b = 0, int c = 0 ); Point3D operator+( Point3D b ); Point3D operator-( Point3D b ); Point3D boo

15、l operator=( Point3D b ); void print(); ; Point3D:Point3D( int a, int b, int c ) x = a; y = b; z = c; Point3D Point3D:operator+( Poin t3D b ) Point3D temp; temp.x = x + b.x; temp.y = y + b.y; temp.z = z + b.z; return temp; Point3D Point3D:operator-( Poin t3D b ) Point3D temp; temp.x = x - b.x; temp.

16、y = y - b.y; temp.z = z - b.z; return temp; Point3D x = b.x; y = b.y; z = b.z; return *this; bool Point3D:operator=( Poi nt3D b ) int t1, t2, t3; t1 = x - b.x; t2 = y - b.y; t3 = z - b.z; if( t1 = 0 else return false; void Point3D:print() cout ( x , y , z ) endl; int main() Point3D p1(1, 1, 1), p2(2

17、, 2, 2), p3; 4 p3 = p2 + p1; p3.print(); p3 = p2 - p1; p3.print(); p3 = p1; p3.print(); if( p1 = p2 ) cout p1 = p2 endl; else cout p1 != p2 endl; return 0; (3,3,3) (1,1,1) (1,1,1) p1!=p2 3.设计一个时间类 Time, 包括时、分、秒等私有 数据成员。重载“+”和 “-”运算符以实现时间 的加法和减法运算,并 进行测试。 #include using namespace std; class Time priv

18、ate: int hour, minute, second; public: Time(int a = 0, int b = 0, int c = 0); Time operator+(Time x); Time operator-(Time x); void print(); Time() ; Time:Time(int a, int b, int c) hour = a; minute = b; second = c; Time Time:operator+(Time x) Time temp; temp.second= second + x.second; temp.minute= mi

19、nute + x.minute; temp.hour = hour + x.hour; if(temp.second 59) temp.minute += 1; temp.second -= 60; if(temp.minute 59) temp.hour+= 1; temp.minute -= 60; if(temp.hour 23) temp.hour-= 24; return temp; Time Time:operator-(Time x) Time temp; temp.second= second - x.second; temp.minute= minute - x.minute

20、; temp.hour = hour - x.hour; if(temp.second 0) temp.minute -= 1; temp.second += 60; if(temp.minute 0) temp.hour-= 1; temp.minute += 60; if(temp.hour 0) temp.hour+= 24; return temp; 5 void Time:print() couthour: minute:second abcd ef; Time A1(a, b, c), A2(d, e, f), A3, A4; A3 = A1 + A2; A3.print(); A

21、4 = A1 - A2; A4.print(); return 0; 1 20 9 2 10 1 3:30:10 23:10:8 1.设计一个单基继承的类层次 程序,利用 Person 类派生出 Student 类, 增加属性 xh (学 号) ,Person 类中至少有姓 名、年龄等数据成员,成员 函数中构造函数对其初始 化,析构函数释放相应存储 单元,输出函数输出其数据 成员的值,其它成员函数根 据需要添加,在主函数中进 行测试。 #include #include using namespace std; class Person protected: string name; int

22、age; string address; public: Person(string nam,int a,string add) name=nam; age=a; address=add; void display() coutname:name endl; coutage:agee ndl; coutaddress:ad dressendl; ; classStudent:public Person private: int num; public: Student(string nam,int a,string add,int xh):Person(nam,a,add) num=xh; v

23、oid display_1() display(); coutnum:nume ndl; ; int main() Student stud(li_hua,18,Nan_ya ng,1001); stud.display_1(); return 0; name:li_hua age:18 address:Nan_yang num:1001 2.先定义“高度“类和”圆“类 C ircle, 再由 Height 类和 Circle 类多重派生出“圆柱 体”类 Cylinder。在主函数 中定义一个圆柱体对象,调 用成员函数求出圆柱体的体 积和表面积。 #include using namespac

24、e std; class Circle protected: double R,s1; public: Circle(float r) R=r; void display() s1= 3.14*R*R; ; 1.分别定义 Teacher(教 师)类和 Cadre(干部) 类,采用多重继承方式 由这两个类派生出新类 Teacher_ Cadre(教师 兼干部) 。要求: 1)在两个基类中都包 含姓名、年龄、性 别、地址、电话等 数据成员. 2)在 Teacher 类中还 包 含 数 据 成 员 6 title(职称) ,在 Cadre 类中还包含 数据成员 post(职 务) ,在 Teache

25、r_ Cadre 类中还包含 数据成员 wages(工 资)。 3)对两个基类中得姓 名、年龄、性别、 地址、电话等数据 成 员 用 相 同 的 名 字,在引用这些数 据成员时,指定作 用域。 4)在类体中声明成员 函数,在类外定义 成员函数。 5)在派生类 Teacher_ Cadre 的成员函数 show中调用 Teacher 类 中 的 display 函数, 输出 姓名、年龄、性别、 职称、电话,然后 再用cout语句输出 职务和工资。 #include using namespace std; class Teacher/教师 protected: char name20; int

26、age; char sex2; char add20; char tel20; char title20;/ 职称 public: void getdata(); void display(); ; classCadre/ 干部 protected: char name20; int age; char sex2; char add20; char tel20; char post20;/ 职 务 public: void getdata(); void disp(); ; class Teacher_Cadre :public Teacher,publi c Cadre/教师 兼干部 pri

27、vate: int wages;/工资 public: void show(); void getdata(); void disp(); ; void Teacher:getd ata() coutplease input data:endl; coutname;c outendl; coutage;co utendl; coutsex;co utendl; coutadd;co utendl; couttel;co utendl; couttitle; coutendl; void Teacher:disp lay() coutoutput teacher data:endl; cout姓

28、 名: nameendl ; cout年 龄: ageendl; cout 性 别 : sexendl; cout地 址: addendl; cout电 话: telendl; 7 cout 职 称 : titleendl; void Cadre:getdat a() coutplease inputCadre data:endl; coutname;c outendl; coutage;co utendl; coutsex;co utendl; coutadd;co utendl; couttel;co utendl; coutpost;c outendl; void Cadre:disp(

29、) coutoutput Cadre data:endl; cout 姓 名 : nameendl; cout年 龄: ageendl; cout性 别: sexendl; cout地 址: addendl; cout电 话: telendl; cout职 务: postendl ; void Teacher_Cadre: :show() display(); void Teacher_Cadre: :getdata() coutplease input Teacher_Cadre data:endl; Teacher:getd ata(); coutpos t; coutwages; voi

30、d Teacher_Cadre: :disp() cout 职 务 :poste ndl; cout工 资 :wagesendl ; int main() Teacher t; Cadrec; Teacher_Cadre s; t.getdata(); c.getdata(); s.getdata(); t.display(); c.disp(); s.show(); s.disp(); system(pause ); return 0; 2.设 计 一 个 虚 基 类 Person,包含姓名和年 龄私有数据成员以及相 关的成员函数;由它派 生出领导类 Leader, 包 含职务和部门私有数据

31、 成员以及相关的成员函 数; 再由 Person 派生出 工程师类 Engineer, 包 含职务和专业私有数据 成员以及相关的成员函 数 ; 再 由 Leader 和 Engineer 类派生出主 任工程师类 Chairman。 并采用相关数据进行测 试。 #include using namespace std; class person private: char name10; int age; 8 public: person() void getdata() cout输入数据:endl; coutname; coutage; void dispdata() cout输出数据:end

32、l; cout姓名: nameendl; cout年龄: ageendl; ; class Leader:virtual public person/ 领导类 private: char post10; /职务 char depart10;/部门 public: Leader() void getdata() person:getdata(); coutpost; coutdepart; void dispdata() person:dispdata(); cout职务: postendl; cout部门: departendl; ; classEngineer:virtualpublic p

33、erson/工程师类 private: char post10; char major10; public: Engineer() void getdata() person:getdata(); coutpost; coutmajor; void dispdata() person:dispdata(); cout职务: postendl; cout专业: majorendl; ; classChairman:public Leader,public Engineer/主任工程 师类 public: Chairman()coutconstrcuter! endl; ; int main()

34、Leader l; Engineer e; l.getdata(); e.getdata(); Chairman t; l.dispdata(); e.dispdata(); system(pause); return 0; 1、声明抽象基类 Shape,由它派 生出 3 个派生类:Cirle(圆形) 、 Rectangle(矩形) 、Triangle(三角 形) ,用一个函数 printArea 分别 输出以上三者的面积, 3 个图形的 数据在定义对象是给定。 #include #include using namespace std; const double PI=3.14; class

35、 Shape/抽象基类 public: Shape() virtualvoidgetradius(double r=0); virtual void printArea()=0; protected: int a; int b; int c; double radius; ; void Shape:getradius(double r) radius=r; class Circle:public Shape/圆形类 public: Circle(double r)radius=r; void printArea(); protected: double radius; double s; ;

36、void Circle:printArea() s=PI*radius*radius; cout圆的面积为:sendl; class Rectangle:public Shape/矩形 类 public: Rectangle(int i=0,int j=0); void printArea(); protected: int a; int b; int s; ; Rectangle:Rectangle(int i,int j) 9 a=i; b=j; void Rectangle:printArea() s=a*b; cout矩形的面积 为:sendl; class Triangle:public Shape/ 三 角 形类 public: Triangle(int i,int j,int k) a=i; b=j; c=k; void printArea(); protected: int a; int b; int c; double s; double s1; ; void Triangle:printArea() s=(a+b+c)/2; s1=sqrt(s*(s-a)*(s-b)*(s-c); cout 三

温馨提示

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

最新文档

评论

0/150

提交评论