第07-09章_习题课_第1页
第07-09章_习题课_第2页
第07-09章_习题课_第3页
第07-09章_习题课_第4页
第07-09章_习题课_第5页
已阅读5页,还剩56页未读 继续免费阅读

下载本文档

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

文档简介

1、2022-5-2900杨杨 琦琦西安交通大学西安交通大学计算机教学实验中心计算机教学实验中心2012.102012.10第7章 习题计算机程序设计(计算机程序设计(C+)2022-5-291 1. 定义并实现定义并实现Dog类,包含类,包含name、age、sex、weight等属性以及初始化和显示属性的方法,要求用一般等属性以及初始化和显示属性的方法,要求用一般成员函数和构造函数两种方法实现初始化操作。成员函数和构造函数两种方法实现初始化操作。样例输入输出样例输入输出Ahuang 3 m 2.5Dogs name:AhuangDogs age:3Dogs sex:mDogs weight:2

2、.52022-5-292class Dogchar name20;intage;char sex;float weight;public:Dog(char *Name=a, int Age=0, char Sex=m, float Weight=0)strcpy(name,Name);age = Age;sex = Sex;weight = Weight;2022-5-293void Init()cinnameagesexweight;void ShowDog()coutDogs name:nameendl;coutDogs age:ageendl;coutDogs sex:sexendl;c

3、outDogs weight:weight=b)return 2*pi*b+4*(a-b);elsereturn 2*pi*a+4*(b-a);2022-5-297double Area()return (double)fabs(x1-x2)*(y1-y2)/4*pi;int main()int x1,y1,x2,y2;cin x1 y1 x2 y2;Ellipse e1(x1,y1,x2,y2);cout The area of e1: e1.Area() endl;cout The perimeter of e1: e1.Perimeter() endl;return 0;2022-5-2

4、983. 定义并实现三角形类,其成员变量包括三个边长定义并实现三角形类,其成员变量包括三个边长变量,成员函数包括判断是否合法、计算面积,以变量,成员函数包括判断是否合法、计算面积,以及是否构成直角三角形、锐角三角形的钝角三角形及是否构成直角三角形、锐角三角形的钝角三角形等函数。等函数。样例输入输出样例输入输出3 4 5直角三角形直角三角形2022-5-299class triangledouble f1,f2,f3;public:triangle(double a,double b,double c)f1=a;f2=b;f3=c;bool legal() if(f1f2+f3)&(f2f1+f

5、3)&(f3b)d=a;a=b;b=d;if(bc)d=b;b=c;c=d;if(a*a+b*b=c*c)return 0;/直角直角else if(a*a+b*babc;triangle tr1(a,b,c);if(tr1.legal() switch(tr1.type()case 0:cout直角三角形直角三角形endl;break;case 1:cout钝角三角形钝角三角形endl;break;case -1:cout锐角三角形锐角三角形endl;break;2022-5-2912elsecout不能构成三角形不能构成三角形endl;return 0;2022-5-2913 4. 定义并

6、实现地址类定义并实现地址类Address,包括姓名、所居住的,包括姓名、所居住的街道地址、城市和邮编等属性以及改变对象姓名的街道地址、城市和邮编等属性以及改变对象姓名的Changename函数、显示地址信息的函数、显示地址信息的Display函数。函数。样例输入输出样例输入输出张山张山 咸宁西路咸宁西路28号号 西安西安 710049张山张山 address:710049 西安西安 咸宁西路咸宁西路28号号李四李四李四李四 address:710049 西安西安 咸宁西路咸宁西路28号号2022-5-2914class Addresschar Name30;char StreetAddress

7、30;char City30;char Postcode30;public:Address(char *a,char *b,char *c,char *d)strcpy(Name,a);strcpy(StreetAddress,b);strcpy(City,c);strcpy(Postcode,d);2022-5-2915void Changename(char *a)strcpy(Name,a);void Display()coutName address:endl;coutPostcode City StreetAddressnamestreetaddresscitypostcode;Ad

8、dress x(name,streetaddress,city,postcode);x.Display();cinname;x.Changename(name);x.Display();return 0;2022-5-2917 5.定义并实现三维空间的定义并实现三维空间的Point3D类,包括类,包括x、y、z三三个成员变量,一个计算空间中两个点之间的距离的个成员变量,一个计算空间中两个点之间的距离的成员函数,并编写合适的构造函数和析构函数。成员函数,并编写合适的构造函数和析构函数。样例输入输出样例输入输出样例输入输出样例输入输出3 4 51 2 33.46412022-5-2918class

9、 Point3Ddouble x,y,z;public:Point3D(double _x,double _y,double _z)x=_x;y=_y;z=_z;Point3D()double Distance(Point3D &p)double a=p.x;double b=p.y;double c=p.z;return sqrt(x-a)*(x-a)+(y-b)*(y-b)+(z-c)*(z-c);2022-5-2919int main()double x,y,z,a,b,c;cinxyzabc;Point3D point1(x,y,z);Point3D point2(a,b,c);cou

10、tpoint1.Distance(point2)endl;return 0;2022-5-292020杨杨 琦琦西安交通大学西安交通大学计算机教学实验中心计算机教学实验中心2012.102012.10第8章 习题计算机程序设计(计算机程序设计(C+)2022-5-2921 1. 设计一个点类设计一个点类Point和其派生类彩色点类和其派生类彩色点类ColorPoint。样例输入输出样例输入输出1 1 3 4 5x,y=1,1r*256*256+g*256+b=1976372022-5-2922#include using namespace std;class Pointpublic:int

11、x, y; Point( int a= 0, int b= 0 )x=a;y=b;class ColorPoint : public Pointint r, g, b;2022-5-2923public:ColorPoint(int a,int bb,int c,int d,int e):Point(a,bb)r=c;g=d;b=e;void Print()coutx,y=x,yendl;coutr*256*256+g*256+b=r*256*256+g*256+babcde;ColorPoint x(a,b,c,d,e);x.Print();return 0;2022-5-2924 2. 设

12、计一个设计一个Person类和其派生类教师类和其派生类教师teacher,新增的,新增的属性有专业、职称和主讲课程,并为这些属性定义属性有专业、职称和主讲课程,并为这些属性定义相应的方法。相应的方法。样例输入输出样例输入输出11050632 Jack male 32 math prof mathapp身份证号:身份证号:11050632姓名:姓名:Jack性别:性别:male年龄:年龄:32专业:专业:math职称:职称:prof主讲课程:主讲课程:mathapp2022-5-2925class Personstring ID; /身份证号身份证号string Name; /姓名姓名strin

13、g Sex; /性别性别int Age; /年龄年龄public:Person(string id, string name, string sex, int age)ID= id;Name= name;Sex= sex;Age= age;2022-5-2926virtual void display()cout身份证号:身份证号:IDendl;cout姓名:姓名:Nameendl;cout性别:性别:Sexendl;cout年龄:年龄:Ageendl;2022-5-2927class teacher:public Personstring Major; /专业专业string Title;

14、/职称职称string Course; /主讲课程主讲课程public:teacher(string id, string name, string sex, int age, string major, string title, string course):Person(id,name,sex,age)Major=major;Title=title;Course=course;2022-5-2928virtual void display()Person:display();cout专业:专业:Majorendl;cout职称:职称:Titleendl;cout主讲课程:主讲课程:Cou

15、rseidnamesexagemajortitlecourse;teacher x(id,name,sex,age,major,title,course);x.display();return 0;2022-5-2930 3. 设计一个汽车类设计一个汽车类vehicle,包含的数据成员有车轮,包含的数据成员有车轮个数个数wheels和车重和车重weight。小车类。小车类car是它的私有子类是它的私有子类其中包含载人数其中包含载人数passenger_load。卡车类。卡车类truck是是vehicle的私有子类其中包含载人数的私有子类其中包含载人数passenger_load和和载重量载重量

16、payload,每个类都有相关数据的输出方法。,每个类都有相关数据的输出方法。样例输入输出样例输入输出输入小车参数输入小车参数4 3.2 10车轮数:车轮数:4车重:车重:3.2载人数:载人数:10输入卡车参数输入卡车参数12 7.8 4 32.5车轮数:车轮数:12车重:车重:7.8载人数:载人数:4载重量:载重量:32.52022-5-2931class vehicledouble Wheels; /车轮数车轮数double Weight; /车重车重public:vehicle(double wheels, double weight) Wheels=wheels;Weight=weig

17、ht;virtual void Display()cout车轮数:车轮数:Wheelsendl;cout车重:车重:Weightendl;2022-5-2932class car:public vehicledouble Passenger_load; /载人数载人数public:car(double wheels, double weight, double passenger_load):vehicle(wheels,weight)Passenger_load=passenger_load;virtual void Display()vehicle:Display();cout载人数:载人

18、数:Passenger_loadendl;2022-5-2933class truck:public vehicledouble Passenger_load; /载人数载人数double Payload; /载重量载重量public:truck(double wheels, double weight, double passenger_load, double payload):vehicle(wheels,weight)Passenger_load=passenger_load;Payload=payload;virtual void Display()vehicle:Display()

19、;cout载人数:载人数:Passenger_loadendl;cout载重量:载重量:Payloadendl;2022-5-2934int main()double wheels1, weight1, passenger_load1;double wheels2, weight2, passenger_load2, payload;cout输入小车参数输入小车参数wheels1weight1passenger_load1;car c(wheels1, weight1, passenger_load1);c.Display();cout输入卡车参数输入卡车参数wheels2weight2pas

20、senger_load2payload;truck t(wheels2, weight2, passenger_load2, payload);t.Display();return 0;2022-5-2935 6设计一个椭圆类设计一个椭圆类Ellispe,其属性为圆心坐标及半,其属性为圆心坐标及半长轴和半短轴的长度,并用通过构造函数对这些属长轴和半短轴的长度,并用通过构造函数对这些属性初始化,通过成员函数计算椭圆的面积。性初始化,通过成员函数计算椭圆的面积。(取取3.1415926)样例输入输出样例输入输出2.5 6.3 5 20圆心坐标为圆心坐标为2.5 6.3面积为面积为314.15920

21、22-5-2936class Ellispedouble x, y; /圆心坐标圆心坐标double semi_major_axis; /半长轴半长轴double semi_minor_axis; /半短轴半短轴public:Ellispe(double X, double Y, double Semi_major_axis, double Semi_minor_axis)x=X;y=Y;semi_major_axis=Semi_major_axis;semi_minor_axis=Semi_minor_axis;2022-5-2937void Display()cout圆心坐标为圆心坐标为x

22、 yendl;cout面积为面积为area()xysemi_major_axissemi_minor_axis;Ellispe e(x, y, semi_major_axis, semi_minor_axis);e.Display();return 0;2022-5-293939杨杨 琦琦西安交通大学西安交通大学计算机教学实验中心计算机教学实验中心2012.102012.10第9章 习题计算机程序设计(计算机程序设计(C+)2022-5-2940 1. 定义一个哺乳动物定义一个哺乳动物Mammal类,再由此派生出狗类,再由此派生出狗Dog类,二者都定义类,二者都定义 Speak()成员函数,基

23、类中定义为成员函数,基类中定义为虚函数,定义一个虚函数,定义一个Dog类的对象,调用类的对象,调用Speak函数,函数,观察运行结果。观察运行结果。样例输入输出样例输入输出I am a mammal.I am a dog.2022-5-2941class Mammalpublic:virtual void Speak()coutI am a mammal.endl;class Dog:public Mammalpublic:virtual void Speak()coutI am a dog.Speak();p=&dog1;p-Speak();return 0;2022-5-2943 3设计一

24、个汽车类设计一个汽车类Motor,该类具有可载人数、轮,该类具有可载人数、轮胎数、马力数、生产厂家和车主五个数据成员,根胎数、马力数、生产厂家和车主五个数据成员,根据据Motor类派生出类派生出Car类、类、Bus类和类和Truck类。其中类。其中Bus类类除继承基类的数据成员之外,还具有表示车厢节数除继承基类的数据成员之外,还具有表示车厢节数的数据成员的数据成员Number;Truck类除继承基类的数据成员类除继承基类的数据成员之外,还具有表示载重量的数据成员之外,还具有表示载重量的数据成员Weight。每个类。每个类都有成员函数都有成员函数Display,用于输出各类对象的相关信息。,用于

25、输出各类对象的相关信息。在主函数中分别创建各类对象,并输出各类对象的在主函数中分别创建各类对象,并输出各类对象的信息。信息。2022-5-2944样例输入输出样例输入输出4 4 5 xjtu li(输入)(输入)I am a Carnumber_person:4number_tyre:4horsepower:5manufacturers:xjtuowner:li2(输入)(输入)I am a Busnumber_person:4number_tyre:4horsepower:5manufacturers:xjtuowner:linumber:25(输入)(输入)I am a Trucknumb

26、er_person:4number_tyre:4horsepower:5manufacturers:xjtuowner:liweight:52022-5-2945class Motorprotected: int number_person;int number_tyre;int horsepower;char manufacturers30;char owner30;public:Motor(int _number_person,int _number_tyre,int _horsepower,char *_manufacturers,char *_owner)number_person=_

27、number_person;number_tyre=_number_tyre;2022-5-2946horsepower=_horsepower;strcpy(manufacturers,_manufacturers);strcpy(owner,_owner);virtual void Display()coutnumber_person:number_personendl;coutnumber_tyre:number_tyreendl;couthorsepower:horsepowerendl;coutmanufacturers:manufacturersendl;coutowner:own

28、erendl;2022-5-2947class Car:public Motorpublic:Car(int _number_person,int _number_tyre,int _horsepower,char *_manufacturers,char *_owner):Motor(_number_person, _number_tyre, _horsepower,_manufacturers,_owner) void Display()coutI am a Carendl;Motor:Display();2022-5-2948class Bus:public Motorint numbe

29、r;public:Bus(int _number_person,int _number_tyre,int _horsepower,char *_manufacturers,char *_owner,int _number):Motor(_number_person, _number_tyre, _horsepower,_manufacturers,_owner) number=_number;void Display()coutI am a Busendl;Motor:Display();coutnumber:numberendl;2022-5-2949class Truck:public M

30、otorint weight;public:Truck(int _number_person,int _number_tyre,int _horsepower,char *_manufacturers,char *_owner,int _weight) :Motor(_number_person, _number_tyre, _horsepower,_manufacturers,_owner) weight=_weight;void Display()coutI am a Truckendl;Motor:Display();coutweight:weightnumber_personnumbe

31、r_tyrehorsepowermanufacturersowner;Car car(number_person,number_tyre,horsepower,manufacturers,owner);p=&car;p-Display();2022-5-2951cinnumber;Bus bus(number_person,number_tyre,horsepower,manufacturers,owner,number);p=&bus;p-Display();cinweight;Truck truck(number_person,number_tyre,horsepower,manufact

32、urers,owner,weight);p=&truck;p-Display();return 0;2022-5-2952 4定义一个定义一个Shape抽象类,在此基础上派生出抽象类,在此基础上派生出Square类、类、Rectangle类、类、Circle类和类和Trapezoid类,四个类,四个派生类都有成员函数派生类都有成员函数CaculateArea计算几何图形的面计算几何图形的面积,积,CaculatePerim计算几何图形的周长。要求用基类计算几何图形的周长。要求用基类指针数组,使它每一个元素指向一个派生类对象,指针数组,使它每一个元素指向一个派生类对象,计算并输出各自图形的面积和

33、周长。计算并输出各自图形的面积和周长。样例输入输出样例输入输出输入:输入:4.2 3.0 4.0 1.5 3.0 4.0 2.0输出:输出:I am a SquareArea:17.64Perim:16.8I am a RectangleArea:12Perim:14I am a CircleArea:7.06858Perim:9.42478I am a TrapezoidArea:7Perim:11.12312022-5-2953#include#includeusing namespace std;const double PI=3.1415926;class Shapepublic:virtual double CalculateArea()=0;virtual double CalculatePerim()=0;virtual void Display()coutArea:CalculateArea()endl;coutPerim:CalculatePerim()endl;2022-5-2954class Square:public Shapedouble ed

温馨提示

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

评论

0/150

提交评论