




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1.1编写一个基于对象的程序,要求:(1) 定义一个时间类 Time,类内有私有数据成员hour (小时)、minute (分钟)、sec (秒),公有成员函数set_time()、show_time()。(2) set_time() 函数和 show_time()函数在类内定义。 set_time() 作用是从键盘输入时间、分钟、秒的值,show_time() 的作用是在屏幕上显示时间、分钟、秒的值。(3) 在main()函数定义 Time类的对象t1,并调用set_time()函数给时间赋值,调用show_time()函数输出时间的值。#include using namespace st
2、d;class Timepublic:void set_time()cinhour;cinminute;cinsec;void show_time()coutvhourvv:vvmi nu te:sece ndl;private: int hour;int minute;int sec;int main()Time t1; t1.set_time(); t1.show_time(); return 0; 1.2编写一个基于对象的程序,求长方体的体积,要求:(1) 定义一个长方体类Box,类内有私有数据成员lengh (长八width (宽)、height (高),公有成员函数get_value
3、()、volume。(2) get_value() 函数和 volume。函数在类外定义。 get_value()作用是从键盘输入长、宽、高的值, volume()的 作用是计算长方体的体积并在屏幕上显示。(3) 在main()函数定义Box类的对象box1,并调用get_value()函数给长、宽、高赋值,调用volume()函数输出长方体体积。#include using namespace std;class Boxpublic:void get_value();void volume();private:float lengh;float width;float height;void
4、 Box:get_value() coutlengh;cinwidth;cinheight;void Box:volume() coutvolmue of boxl is lengh*width*heightendl;int main()Box boxl; box1.get_value(); box1.volume(); return 0; 1.3.编写一个基于对象的程序,求一个有十个数据的整型数组中元 素的最大值,要求:(1) 定义一个类Array_max,类内有私有数据成员 array10 max分别存储十个整数、最大值,公有成员函数set_value() max_volume()。(2)
5、 set_value() 函数和 max_volume()函数在类外定义。 get_value() 作用是从键盘输入数组十个元素的值, max_volume()的作用是求出并显示数组元素的最大值。(3) 在main()函数定义 Array_max类的对象 arrmax,并调用 set_value() 函数给数组赋值,调用 max_volume()函数求出并 显示数组元素的最大值。#include using namespace std;class Array_maxpublic:void set_value();void max_value();private:int array10;int
6、max;;void Array_max:set_value() int i;for (i=0;i arrayi;void Array_max:max_value()int i;max=array0;for (i=1;imax) max=arrayi;coutvmax=vvmax;int main()Array_max arrmax;arrmax.set_value();arrmax.max_value();return 0;1.4编写一个程序,用成员函数重载运算符“+”,使之能用于两个复数相加。#include using namespace std;class Complexpublic:C
7、omplex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator + (Complex &c2);void display();private:double real;double imag;Complex Complex:operator + (Complex &c2)Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;void Complex:display()coutvv(vvrealvv,vvimagvvi)vendl;int m
8、ain()Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutvvc1=;c1.display();coutvvc2=;c2.display(); coutvvc1+c2=;c3.display();return 0;1.5+ ” ,使之能用于两个编写一个程序,用友元函数重载运算符“复数相加。#include class Complexpublic:Complex()real=0;imag=0;Complex(double r)real=r;imag=0;Complex(double r,double i)real=r;imag=i;friend Complex
9、 operator* (Complex &c1,Complex &c2); void display();private:double real;double imag;Complex operator* (Complex &c1,Complex &c2)return Complex(c1.real+c2.real, c1.imag+c2.imag);void Complex:display()coutvv(vvrealvv,vvimagvvi)vendl;int main()Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutvvc1=; c1.display
10、();coutvvc2=; c2.display();coutvvc1+c2=; c3.display();return 0;1.6编写一个基于对象的程序,求圆球的体积,要求:(1) 定义一个圆球类 Circle ,类内有私有数据成员radius (半 径),公有成员函数 get_value() 、volume。(2) get_value() 函数和 volume。函数在类外定义。get_value()作用是从键盘输入半径的值,volume()的作用是计算圆球的体积并在屏幕上显示。(圆球体积计算公式为:v=4/3 n r3)(3) 在 main()函数定义 Circle 类的对象circle1
11、 ,并调用 get_value()函数给球半径赋值,调用 volume()函数输出圆球 的体积。#include using namespace std;class Circlepublic:void get_value();void volume();private:float radius;void Circle:get_value() coutplease input radius:;cinradius;void Circle:volume()coutvolmueofcircle1isv4.0/3*3.14159*radius*radius*radiusvvendl; int main(
12、) Circle circlel; circle1.get_value(); circle1.volume(); return 0; 1.7编写一个基于对象的程序,要求:(1) 定义一个日期类 Date ,类内有私有数据成员year (年八 month(月)、day(日)公有成员函数 set_date() 、show_date()。(2) set_date() 函数和 show_date()函数 在类外 定义。set_date() 作用是从键盘输入年、月、日的值,show_date()的作用是在屏幕上显示年、月、日的值。(3) 在main()函数定义 Date类的对象d1,并调用set_ da
13、te()函数给日期赋值,调用show_date()函数输出日期的值。#include using namespace std;class Datepublic:void set_date();void show_date();private:int year;int month;int day;void Date:set_date() cin year; cinmonth; cinday;void Date:show_date()coutvyearvv-vv mon th-daye ndl; int main()Date d1;d1.set_date();d1.show_date();retu
14、rn 0;2.1编写一个面向对象的程序,要求:(1) 定义一个基类Student,类内有私有数据成员num(学号)、name (姓名)、sex (性别),公有成员函数get_value()、display() , get_value() 作用是从键盘给 num、name sex 赋 值,display() 的作用是显示 num name sex的值。(2) 定义一个派生类 Student1 , Student1公有继承自 Student 类。Student1类新增私有数据成员 age (年龄)、addr (地址), 新增 公有成 员函数 get_value_1() 、display_1() 。
15、 get_value_1() 的作用是实现从键盘给 num name sex、age、 addr 赋值,display_1() 的作用是显示 num、name sex、age、 addr的值。(3) 在main()函数定义Student1类的对象stud1 ,并调用 get_value_1()函数给对象赋值,调用display_1() 函数显示学 生的所有信息。#include using namespace std;class Studentpublic:void get_value()cinnumnamesex;void display()coutnum: numendl; coutnam
16、e: nameageaddr;void display_1() display();coutage: vvagevvendl; coutvaddress: vvaddrvvendl; private:int age;char addr30;int main()Studentl studl; stud1.get_value_1(); stud1.display_1(); return 0;2.2编写一个面向对象的程序,要求:(1) 定义一个基类Student,类内有私有数据成员num(学号)、name (姓名)、sex (性别),公有成员函数get_value()、display。, get_v
17、alue()作用是从键盘给 num name sex 赋值,display() 的作用是显示 num name sex的值。(2) 定义一个派生类 Studentl , Studentl私有继承自 Student 类。Studentl类新增私有数据成员 age (年龄)、addr (地址), 新增 公有成 员函数 get_value_1() 、display_1() 。 get_value_1() 的作用是实现从键盘给 num name sex、age、 addr 赋值,display_1() 的作用是显示 num、name sex、age、 addr的值。(3) 在main()函数定义Stu
18、dent1类的对象stud1 ,并调用 get_value_1()函数给对象赋值,调用display_1() 函数显示学 生的所有信息。#include using namespace std;class Studentpublic:void get_value()cinnumnamesex;void display()coutnum: numendl;coutname: nameageaddr;void display_1()display。;coutage: vvagevvendl;coutvaddress: vvaddrvvendl;private:int age;char addr30
19、;int main()Studentl studl;stud1.get_value_1();stud1.display_1();return 0;2.3编写一个面向对象的程序,要求:(1) 定义一个基类Student,类内有私有数据成员num(学号)、name (姓名)、sex (性别),公有成员函数get_value()、display() , get_value() 作用是从键盘给num、name sex 赋值,display() 的作用是显示 num name sex的值。(2) 定义一个派生类 Studentl , Studentl保护继承自 Student类。Studentl类新增私
20、有数据成员age (年龄)、addr (地址), 新增 公有成 员函数 get_value_1() 、display_1() 。 get_value_1() 的作用是实现从键盘给 num name sex、age、 addr 赋值,display_1() 的作用是显示 num name sex、age、 addr的值。(3) 在main()函数定义Studentl类的对象studl ,并调用 get_value_1()函数给对象赋值,调用display_1() 函数显示学 生的所有信息。#include using namespace std;class Studentpublic:void
21、get_value()cinnumnamesex;void display()coutnum: numendl;coutname: nameageaddr;void display_1()display();cout #include using namespace std; class Studentpublic:Student(int n,string nam,char s ) num=n;name=nam;sex=s; void show() coutvvnum: vvnumvvendl;coutvvname: vvnamevvendl; coutvvsex: vvsexvvendl;p
22、rotected: int num; string name; char sex ;class Student1: public Studentpublic:Student1(int n,string nam,char s,int a,char Student(n,nam,s)age=a; addr=ad;void show() Student:show(); coutvvage: vvagevvendl; coutvvaddress: vvaddrvvendlvvendl;private:int age; string addr;int main()ad)BeijingStudentl st
23、ud1(10010,Wang-li,f,19,115 Road,Shanghai);studl .show();return 0;2.5编写一个面向对象的程序,要求:(1) 定义一个基类Student,类内有保护数据成员 num(学号)、 name (姓名),公有成员包括构造函数、show()函数。构造函数带2个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即 num name的值。(2) 定义一个派生类 Studentl , Studentl公有继承自 Student 类。Studentl类新增私有数据成员 age (年龄)、addr (地址) 以及子对象 monitor (
24、班长,Student类型),新增公有成员包 括构造函数、show()函数。构造函数带 6个参数用于定义对象 时赋初值,show()函数作用是显示学生的所有信息,即本人的 num name age、addr 以及班长的 num name(3) 在main()函数定义Student1类的对象stud1并赋初值, 调用show()函数显示该学生的所有信息。#include viostream#includeusing namespace std;class Studentpublic:Student(int n,string nam)num=n;name=nam;void show() coutvv
25、num: vvnumvvendl; coutvvname: vvnamevvendl;protected:int num;string name;class Studentl: public Studentpublic:Student1(int n,string nam,int n1,string nam1,int a,string ad):Student(n,nam),monitor(n1,nam1)age=a;addr=ad;void show() coutvvThis student is:vendl;Student:show();coutvvage: vvagevvendl; cout
26、vvaddress: vvaddrvvendivvendl; coutvvClass monitor is:vvendl;monitor .show();private:Student monitor;int age;string addr;int main()Studentl stud1(10010,Wang-li,10001,Li-sun,19,115 Beijing Road,Shanghai);studl .show();return 0;2.6写一个面向对象的程序,定义抽象基类Shape,由它派生出 2 个类:Circle (圆形)、Rectangle (矩形),显示两个图形的 面积
27、。要求:(1) 抽象基类Shape的公有成员有纯虚函数area()。(2) Circle 类公有继承自 Shape类,新增数据成员radius (半径),公有成员有构造函数和求圆面积的area()函数。(3) Rectangle类公有继承自 Shape类,新增数据成员length(长)、width (宽),公有成员有构造函数和求矩形面积的area()函数。(4) 在main()函数定义Circle 类的对象circle1 并赋初值, 调用area()函数显示该圆面积;定义 Rectangle 类的对象 rectangle1 并赋初值,调用area()函数显示该矩形面积。#include usi
28、ng namespace std;class Shapepublic:virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r)constreturnvirtualdouble area()3.14159*radius*radius;protected:double radius;class Rectangle:public Shapepublic:Rectangle(double l,double w):length(l),width(w) virtual double area() const return length*width;protected:double length,width;int main()Circle circle(2.5);coutarea of circle =circle.area()endl;Re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 紧固件销售与市场拓展考核试卷
- 幕墙施工中的精细化管理考核试卷
- 原油加工过程质量控制考核试卷
- 纺织品企业绿色供应链与环保措施考核试卷
- 生命体征测量技术
- 5-11序列信号发生器1-分析与设计
- 1-6码制-二-十进制码
- 小学一年级下册数学期末考试试卷及答案
- 2025年北京大兴区中考一模物理试卷试题(含答案详解)
- 统编版语文五年级下册第14课《刷子李》精美课件
- 全息投影技术课件
- 西政安徽校友会通讯录
- 2017沂源县新医药产业园区控制性详细规划
- 初中语文人教七年级下册驿路梨花写作顺序
- 养老护理员第一章职业道德
- 深层平板载荷试验检测地基承载力作业指导书
- (完整)EHS培训考核题库及答案
- GB/T 27007-2011合格评定合格评定用规范性文件的编写指南
- GB/T 25744-2010钢件渗碳淬火回火金相检验
- GB/T 23445-2009聚合物水泥防水涂料
- 项目启动会监理方讲话稿
评论
0/150
提交评论