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

下载本文档

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

文档简介

1.声明一个Circle类,有1)数据成员Radius(半径)2)成员函数GetValue()用来给半径赋值3)成员函数GetArea()计算圆的面积在主函数中创建一个Circle类的对象进行测试,显示出面积。#includeusing namespace std;class circle private: int radius; public: void getvalue() coutradius; double getarea() return 3.14*radius*radius; void output() cout半径是:radius,面积是:getarea(); ;int main() circle circle1; double radius; circle1.getvalue(); circle1.getarea(); circle1.output();输入半径:2半径是:2,面积是:12.562.声明一个tree类,有1)数据成员ages(树龄)2)成员函数grow(int years)对ages加上years3)成员函数age()显示对象的数据成员ages的值4)构造函数tree(int n=0)进行初始化在主函数中创建一个tree类的对象进行测试(创建一个树龄为18的对象,调用age()显示树龄,之后调用grow(4),生长4年,再显示树龄)。2:#includeusing namespace std;class treepublic: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();t1.grow(4);t1.age();system(pause);return 0;该树年龄为:5请输入年份:5该树年龄为:103. 声明一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两点的坐标,并有成员函数计算矩形的周长及面积。编程实现求左下角与右上角坐标分别为(2.1,3.2),(5.2,6.3)的矩形周长及面积。#include#includeclass Rectangleprivate: double left; double bottom; double right; double top;public: void SetPoint(double l,double b,double r,double t) left=l; bottom=b; right=r; top=t; void cal(double left,double bottom,double right,double top) double s; s=abs(left-right)*abs(bottom-top); cout该长方形的面积为:sendl; void zhou(double left,double bottom,double right,double top) double z; z=2*(abs(left-right)+abs(bottom-top); cout该长方形的周长为:zendl; void Getp1(double left,double bottom) cout长方形的左下角坐标是:left,bottomendl; void Getp2(double right,double top) cout长方形的右上角坐标是:right,topendl; ;void main() Rectangle r1; double l,b,r,t; coutlb; coutrt; r1.Getp1(l,b); r1.Getp2(r,t); r1.cal(l,b,r,t); r1.zhou(l,b,r,t);请输入长方形的左下角的坐标:2.1 3.2请输入长方形的右上角的坐标:5.2 6.3长方形的左下角的坐标:2.1 3.2长方形的右上角的坐标:5.2 6.31. 编写一个简单复数类Scomplex,要求用友元函数重载“+”、“-”运算符,用成员函数重载“=”运算符,使之能够实现整数或浮点数和复数的加法和减法,并且进行测试。1.#include using namespace std;class Scomplexprivate:double real;double image;public:Scomplex( double x = 0, double y = 0 );friend Scomplex operator+( int a, Scomplex b );friend Scomplex operator-( int a, Scomplex b );friend Scomplex operator+( double a, Scomplex b );friend Scomplex operator-( double a, Scomplex b );Scomplex &operator=( const Scomplex &b );void print();Scomplex:Scomplex( double 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.real;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 &Scomplex:operator=( const Scomplex &b )if( this = &b )return *this;real = b.real;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-4i1+2i2. 空间一点p的坐标为(x,y,z),其中x,y,z为整数。编写点类Point3D,定义空间两点之间的加”+”,减”-”运算为相应三个坐标值分别进行加、减运算,要求实现空间两点之间的加”+”减”-”赋值”=”运算,空间两点间的比较”= =”运算。要求编写Point3D类的声明定义和测试程序。#include using namespace std;class Point3Dprivate:int x, y, z;public:Point3D( int a = 0, int b = 0, int c = 0 );Point3D operator+( Point3D b );Point3D operator-( Point3D b );Point3D &operator=( const Point3D &b );bool operator=( Point3D b );void print();Point3D:Point3D( int a, int b, int c )x = a;y = b;z = c;Point3D Point3D:operator+( Point3D b )Point3D temp;temp.x = x + b.x;temp.y = y + b.y;temp.z = z + b.z;return temp;Point3D Point3D:operator-( Point3D b )Point3D temp;temp.x = x - b.x;temp.y = y - b.y;temp.z = z - b.z;return temp;Point3D &Point3D:operator=( const Point3D &b )if( this = &b )return *this;x = b.x;y = b.y;z = b.z;return *this;bool Point3D:operator=( Point3D b )int t1, t2, t3;t1 = x - b.x;t2 = y - b.y;t3 = z - b.z;if( t1 = 0 & t2 = 0 & t3 = 0 )return true;elsereturn false;void Point3D:print()cout ( x , y , z ) endl; int main()Point3D p1(1, 1, 1), p2(2, 2, 2), p3;p3 = p2 + p1;p3.print();p3 = p2 - p1;p3.print();p3 = p1;p3.print();if( p1 = p2 )cout p1 = p2 endl;elsecout p1 != p2 endl;return 0;(3,3,3)(1,1,1)(1,1,1)p1!=p23. 设计一个时间类Time,包括时、分、秒等私有数据成员。重载“+”和“-”运算符以实现时间的加法和减法运算,并进行测试。 #include using namespace std;class Timeprivate: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 = minute + 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;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;void Time:print()couthour:minute:secondabcdef;Time A1(a, b, c), A2(d, e, f), A3, A4;A3 = A1 + A2;A3.print();A4 = A1 - A2;A4.print();return 0;1 20 92 10 13:30:1023:10:81. 设计一个单基继承的类层次程序,利用Person类派生出Student类,增加属性xh(学号),Person类中至少有姓名、年龄等数据成员,成员函数中构造函数对其初始化,析构函数释放相应存储单元,输出函数输出其数据成员的值,其它成员函数根据需要添加,在主函数中进行测试。#include#includeusing namespace std;class Personprotected:string name;int age;string address;public:Person(string nam,int a,string add)name=nam;age=a;address=add;void display()coutname:nameendl;coutage:ageendl;coutaddress:addressendl;class Student:public Personprivate:int num;public:Student(string nam,int a,string add,int xh):Person(nam,a,add)num=xh;void display_1()display();coutnum:numendl;int main()Student stud(li_hua,18,Nan_yang,1001);stud.display_1();return 0;name:li_huaage:18address:Nan_yangnum:10012. 先定义“高度“类和”圆“类C ircle, 再由Height类和Circle类多重派生出“圆柱体”类Cylinder。在主函数中定义一个圆柱体对象,调用成员函数求出圆柱体的体积和表面积。#includeusing namespace std;class Circleprotected:double R,s1;public:Circle(float r)R=r;void display()s1= 3.14*R*R;1. 分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_ Cadre(教师兼干部)。要求:1) 在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员.2) 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_ Cadre类中还包含数据成员wages(工资)。3) 对两个基类中得姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。4) 在类体中声明成员函数,在类外定义成员函数。5) 在派生类Teacher_ Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、电话,然后再用cout语句输出职务和工资。#includeusing namespace std;class Teacher/教师protected: char name20; int age; char sex2; char add20; char tel20; char title20;/职称public:void getdata();void display();class Cadre/干部protected: char name20; int age; char sex2; char add20; char tel20; char post20;/职务public: void getdata(); void disp();class Teacher_Cadre :public Teacher,public Cadre/教师兼干部private: int wages;/工资public:void show();void getdata();void disp();void Teacher:getdata() coutplease input data:endl; coutname;coutendl; coutage;coutendl;coutsex;coutendl;coutadd;coutendl;couttel;coutendl;couttitle;coutendl;void Teacher:display() coutoutput teacher data:endl; cout姓名:nameendl; cout年龄:ageendl; cout性别:sexendl; cout地址:addendl; cout电话:telendl; cout职称:titleendl;void Cadre:getdata() coutplease input Cadre data:endl; coutname;coutendl; coutage;coutendl;coutsex;coutendl;coutadd;coutendl;couttel;coutendl;coutpost;coutendl;void Cadre:disp() 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:getdata(); coutpost; coutwages;void Teacher_Cadre:disp() cout职务:postendl;cout工资:wagesendl; int main() Teacher t; Cadre c; Teacher_Cadre s; t.getdata(); c.getdata(); s.getdata(); t.display(); c.disp(); s.show(); s.disp(); system(pause); return 0;2. 设计一个虚基类Person,包含姓名和年龄私有数据成员以及相关的成员函数;由它派生出领导类Leader,包含职务和部门私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer,包含职务和专业私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。并采用相关数据进行测试。#includeusing namespace std;class personprivate: char name10; int age; public: person() void getdata() cout输入数据:endl; coutname; coutage; void dispdata() cout输出数据:endl; 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; ;class Engineer:virtual public person/工程师类 private: char post10; char major10;public: Engineer() void getdata() person:getdata(); coutpost; coutmajor; void dispdata() person:dispdata(); cout职务:postendl; cout专业:majorendl; ;class Chairman:public Leader,public Engineer/主任工程师类 public: Chairman()coutconstrcuter!endl;int main() 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#includeusing namespace std;const double PI=3.14;class Shape/抽象基类 public: Shape() virtual void getradius(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;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) 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三角形的面积为:s1endl; int main() Circle c(5); Rectangle r(4,6); Triangle t(5,12,13); c.printArea(); r.printArea

温馨提示

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

评论

0/150

提交评论