




已阅读5页,还剩116页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一章 面向对象的方法学一.阅读程序题解答:二.完成程序题解答:三.改错题解答:四.编程题解答:五.填空题1. 面向对象程序设计方法的基本特征主要有抽象性、( )、继承性和多态性。2. ( )是子类自动共享父类数据结构和方法的机制,这是类之间的一种关系。3. 不同的对象,收到同一消息可以产生不同的结果,这种现象称为( )。4. 编译时的多态性是通过( )来实现的。5. 运行时的多态性是通过( )来实现的。6. ( )是对一组具有共同属性特征和行为特征的对象的抽象。7. ( )是一个类所描述的一个具体的对象。8. 类和对象之间的关系是( )的关系。9. ( )就是从众多的事物中抽取出共同的、本质性的特征,舍弃其非本质的特征。10. 子类继承了多个父类的数据结构和方法,则称为( )。11. 面向对象程序设计方法的基本特征主要有( )、封装性、继承性和多态性。12. 面向对象程序设计方法的基本特征主要有抽象性、封装性、( )和多态性。13. 面向对象程序设计方法的基本特征主要有抽象性、封装性、继承性和( )。14. 把面向对象思想应用于软件开发过程中,指导开发活动的系统方法,简称( )方法。15. 20世纪90年代中期由Booch,Rumbaugh和Jacoson共同提出了( ),把众多面向对象分析和设计方法综合成一种标准,使面向对象的方法成为主流的软件开发方法。解答:..5.第二章 类与对象一.阅读程序题1. #includeclass CSample int i;public: CSample() i=1; couti+; void disp() cout+i; CSample() couti+; ;void main() CSample s; s.disp(); 2. #includeclass Sampleprivate:int x;public:Sample() cout(x=0);Sample(int i,int j) cout(x=i+j);Sample()coutx;void main()Sample *p1=new Sample, *p2=new Sample(3,4);delete p1; delete p2; 3. #include class Samplepublic:Sample()Sample(int a)x=a;Sample(Sample &a)x=a.x+;void disp()coutx+;private:int x;void fun(Sample &s1, Sample s2) s1.disp ();s2.disp ();void main()Sample s1(2),s2=s1; fun(s2,s1);s1.disp ();s2.disp ();4. #includeclass Sampleint x;public:Sample()x=0;Sample(int a) cout(x=a);Sample() cout+x;void disp() coutx;void main()Sample s1(2);s1.disp ();s1.Sample (); 5. #include class Sampleint x;public:Sample()cout(x=0);Sample(int i)cout(x=i);Sample()coutxendl;void disp()coutx;void main()Sample s(3);int i=0; if(i=0) Sample s; s.disp ();6. #include class Samplepublic:Sample()coutConstructorendl;Sample()coutDestructorendl;void fn(int i)static Sample c;couti=iendl;void main()fn(10);fn(20);7. #includeclass Sampleint n;public:Sample(int i)n=i;friend int add(Sample &s1,Sample &s2);int add(Sample &s1,Sample &s2) return s1.n+s2.n;void main()Sample s1(10),s2(20);coutadd(s1,s2)endl;8. #includeclass B;class Aint i;public:int set(B&);int get()return i;A(int x)i=x;class Bint i;public:B(int x)i=x;friend A;int A:set(B &b) return i=b.i;void main()A a(1);B b(2);couta.get(),;a.set(b);couta.get()endl;9. #include float f(float x, float y)return x+y;int f(int x, int y)return x+y;void main()float a, b, c;a = b = 50.5; c = f(a, b);cout c;10. #include int max(int x, int y)if(x y) return x;else return y;int max(int x, int y, int z)return max(x, max(y, z);void main()int a = 3, b = 4, c = 5, d = 0; d = max(a, b);cout d;11. #include int p(int x = 4, int y = 5) return x + y;void main()int a = 3, b = 4, c = 0; c = p(b);cout c;12. #include int add(int x, int y = 8);void main()int a = 6; cout add(a, add(a) endl;int add(int x, int y)return x + y;13. #include int p(int x = 4, int y = 5, int z = 6) return x + y + z;void main()int a = 3, b = 4, c = 5; c = p(b, a);cout c;14. #include int p(int x = 4, int y = 5, int z = 6) return x + y + z;void main()int a = 3, b = 4, c = 0; c = p(b, c);cout c;15. #include int n = 1; void Fun();void main()n+;Fun();if(n 0) int n = 5; cout Block:n= n ,;cout Main:n = n endl;void Fun()int n = 10; cout Fun:n=n ,; 解答:..5.二.完成程序题1. 有一个学生类student,包括学生姓名、成绩,设计一个友元函数,比较两个学生成绩的高低,并求出最高分和最低分的学生。解:#include(1)_ class studentchar name10;int deg;public:(2)_/定义构造函数strcpy(name,na);deg=d;char *getname() return name;(3)_;int compare(student &s1,student &s2) if(s1.degs2.deg)return 1; else if(s1.deg=s2.deg)return 0; else return -1; void main()student st=student(王华,78),student(李明,92),student(张伟,62),student(孙强,88);int i,min=0,max=0; for(i=1;i4;i+)if(compare(stmax,sti)=-1) (4)_else if(compare(sti,stmin)=1) min=i;cout输出结果:endl;cout 最高分:stmax.getname()endl;cout 最低分:stmin.getname()endl;2. 有一个学生类student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:大于等于90:优;8090:良;7079:中;60!69:及格;小于60:不及格。#include#include#includeclass studentchar name10;int deg;char level7;public:(1)_/定义构造函数strcpy(name,na);deg=d;char *getname() return name;(2)_;void disp()coutsetw(10)namesetw(6)degsetw(8)level=90) strcpy(s.level,优);else if(s.deg=80) strcpy(s.level,良);else if(s.deg=70) strcpy(s.level,中);else if(s.deg=60) strcpy(s.level,及格);elsestrcpy(s.level,不及格);void main()int i;student st=student(王华,78),student(李明,92),student(张伟,62),student(孙强,88);cout输出结果:endl;coutsetw(10)姓名setw(6)成绩setw(8)等级endl;for(i=0;i4;i+)(3)_ /成绩转换(4)_/输出结果3. 定义一个汽车类,实现汽车数目的修改、显示等功能。#includeclass Carprivate:int number;public:(1)_ /定义构造函数 number = x;void SetNumber(int n) number = n;void display() (2)_ /输出汽车数目;int main()Car *p;(3)_;p-SetNumber (20);(4)_/输出汽车数目return 0; 4. 定义一个数组类,实现输入输出#includeclass Arraypublic:(1)( )/定义构造函数 (2)( ); /申请能存放num个整型元素的内存空间 arraysize=num;void SetArray() int i; for(i=0;iarraysize;i+) Ptri=i;void Display() int i; for(i=0;iarraysize;i+) coutPtriendl;Array() (3)( ); /释放内存空间private:int *Ptr;int arraysize;void main()Array a(10);a.SetArray();(4)( ) /输出 5. 将distance定义为类Point的友元函数,计算两点之间的距离。#include (1)( )class Pointprivate:int x;int y;public:(2)( )(3)( );double distance((4) )double d;d=pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2); /pow为幂函数return sqrt(d);void main()Point p1(3,4),p2(0,0);cout二点之间的距离为:distance(p1,p2)endl;解答:.5.三.改错题1. class Apublic:void A(int A,int B,int c)x=a;y=b;z=c; int set(int A,int b)x=a;y=b; void set(int A,int B,int c=0)x=a;y=b;z=c;static void disp()coutxyzendl; private:int x,y,z;void main()A a(1,2,3);A.set(120,300); 2. #includeclass Aprivate:int k;public:void A(int n)k=n; A(int n)k=n; class Bprivate:int j;A a; public:B(int x,int y) A a(x); j=y;void main()B b(9); 解答:1.2.四.编程题1. 下面是一个类的测试程序,设计出能使用如下测试程序的类。void main()Test a;a.init(68,55);a.print();其执行结果为:测试结果:68-55=13 2. 设计内联函数inline char trans(char ch);实现大小写字母转换3. 给定三个数21,15,22,设计内联函数int max(int a, int b)求两个数的最大数,输出结果“在21 15 22之中最大的是:22”。4. 设计重载函数overload,如果输入整数5,则输出5,如果输入字符a,则输出字符b。5. 定义一个三角形类,用成员函数计算其周长、面积。解答:.5.五.填空题1. ( )的功能是在创建对象时,给数据成员赋初值,即对象的初始化。2. ( )的功能是释放一个对象,在对象删除之前,用它来做一些内存释放等清理工作,它的功能与构造函数的功能正好相反。3. 定义对象时,编译系统会自动地调用( )。4. 一个类中有且仅有一个析构函数,且应为( )。5. 在堆中创建的数据对象称为( )。6. 当函数被声明为一个类的( )后,它就可以通过对象名访问类的私有成员和保护成员。7. 定义A是一个类,那么执行语句“A a, b(3),*p;”调用了( )次构造函数。8. this指针是C+实现( )的一种机制。9. 如果友元函数带了两个不同的类的对象,其中一个对象所对应的类要在后面声明。为了避免编译时的错误,编程时必须通过( )告诉C+,该类将在后面定义。10. 已知类中的一个成员函数说明为:void Set (X &a)其中,X &a的含义是( )。11. 为了使类中的某个成员不被类的对象通过成员操作符访问,则不能把该成员的访问权限定义为( )。12. 在类的定义中,用于为对象分配内存空间,对类的数据成员进行初始化并执行其内部管理操作的函数是( )。13. 在C+语言程序中,对象之间的相互通信通过调用( )函数实现。14. 类的具体表现是通过声明( )来操作的。15. 默认构造函数的函数体是( )。解答:..5.第三章 函数重载与内联函数一.阅读程序题1. #includeclass Sampleint x,y;public:Sample()x=y=0;Sample(int a,int b)x=a,y=b;void disp()coutx=x,y=yendl; void main() Sample s1,s2(1,2),s3(10,20); Sample *Pa3; Pa0=&s1; Pa1=&s2; Pa2=&s3; for(int i=0;idisp(); 2. #includeclass Sampleint n;public:Sample()Sample (int m)n=m;friend void square(Sample &s)s.n=s.n*s.n;void disp()coutn=nendl;void main()Sample a(10);square(a);a.disp();3. #includeclass B;class Aint i;friend B;void disp()coutiendl;class Bpublic:void set(int n)A a;a.i=n;a.disp();void main()B b;b.set(2);4. #includeclass teacher;class studentchar *name;public:student(char *s)name=s;friend void print(student &,teacher &);class teacherchar *name;public:teacher(char *s)name=s;friend void print(student &,teacher &);void print(student &a,teacher &b)coutthe student is:endl;coutthe teacher is:endl;void main()student s(Li Hu);teacher t(Wang Ping);print(s,t);5. #include#includeclass Samplepublic:int x,y;Sample()x=y=0;Sample(int a,int b)x=a;y=b;void disp()coutx=x,y=yendl;Sample()if(x=y)coutx=yendl;elsecoutx!=yendl; void main() Sample s1(2,3); s1.disp(); if(s1.x=2) exit(0); 6. #includeclass Samplepublic:Sample();Sample(int);Sample();void display();protected:int x; Sample:Sample() x=0; coutconstructing normallyn; Sample:Sample(int m)x=m;coutconstructing with a number:xendl; void Sample:display() coutdisplay a number:xendl; Sample:Sample()coutdestructingn; void main() Sample obj1; Sample obj2(20); obj1.display(); obj2.display(); 7. #include iostream.hclass Apublic:A()coutA的构造函数endl;void main()A b2,*p3;8. #includeclass Sampleint x,y;public:Sample()x=y=0;Sample(int a,int b)x=a;y=b;void disp()coutx=x,y=yendl; void main() Sample s1,s2(5,21),s3(32,17); Sample *pa3=&s1,&s2,&s3; for(int i=0;idisp(); 解答:..二.完成程序题1. 有一个学生类student,包括学生姓名、成绩,设计一个友元类,输出成绩大于等于80分 以上者。#include#include#includeclass studentchar name10;int deg;public:(1)_/定义构造函数strcpy(name,na);deg=d;char *getname()(2)_(3)_; void disp(student &s) if(s.deg=80) coutsetw(10)setw(6)s.degendl; void main() student st=student(王华,78),student(李明,92),student(张伟,62),student(孙强,88); cout输出结果:endl; coutsetw(10)姓名setw(6)成绩endl; for(int i=0;i4;i+) (4)_; /输出 2. 有一个向量类Vector,包括一个点的坐标位置x和y,设计两个友元函数,实现两个向量的加法和减法的运算#includeclass Vectorint x,y;public:(1)_/定义构造函数(2)_ /定义构造函数void disp()cout(x,y);(3)_(4)_; Vector add(Vector &v1,Vector &v2) Vector v; v.x=v1.x+v2.x; v.y=v1.y+v2.y; return v; Vector sub(Vector &v1,Vector &v2) Vector v; v.x=v1.x-v2.x; v.y=v1.y-v2.y; return v; void main()Vector v1(2,4),v2(5,7),v3; v3=add(v1,v2);v3.disp();v3=sub(v1,v2);v3.disp();3. 编写一个程序,设计一个Person类,包括学号、姓名和成绩等私有数据成员,不含任何成员函数,只将display(Person p)设置为该类的友元函数。#include#includeclass Personint no;char name10;int score;(1)_Person(int n,char nam,int deg);(2)_; (3)_strcpy(name,nam); void display(Person p) cout输出结果endl; cout学生 (学号p.no )成绩为:p.score endl; void main() Person obj(91106,张明,98); (4)_; 4. 下面程序中用来求数组和。请在下面程序的横线处填上适当内容#include class Arrint *a,n;public:(1)_/定义构造函数Arr(int *aa, int nn) n=nn; a=(2)_ for(int i=0;inn;i+) *(a+i)=*(aa+i);Arr()(3)_int GetValue(int i) return (4)_;void main()int b5=10,20,30,40,50;Arr a1(b,5);int i=0,s=0; for(;i5;i+) s+=a1.GetValue(i);couts=sendl;5. 求直角三角形斜边长度#include #include (1)_class Pointprivate:double x,y;(2)_public:(3)_x=i;y=j;Point(Point &p)x=p.x;y=p.y; class Line private: Point p1,p2; public: Line(Point &xp1,Point &xp2):p1(xp1),p2(xp2) double GetLength(); ; double Line:GetLength() double dx=p2.x-p1.x; double dy=p2.y-p1.y; return sqrt(dx*dx+dy*dy); void main() Point p1,p2(6,8); (4)_ coutL1.GetLength()endl; 解答:.5.三.改错题1. class Aprivate:const int m_a;int m_b;public:void A(int i,int j):m_a(i),m_b=(j); void set(int i)m_b=i;int disp() constcoutm_a=m_atm_b=m_bendl; void main()A a1(10,20);const A a2(30,60); a1.set(30);a2.set(30); 2. 有一处错误。#includeclass Apublic:void fun()couta.funendl;class Bpublic:void fun()coutb.funendl;void gun()coutb.gunendl;class C:public A,public Bprivate:int b;public:void gun()cout C.gunendl;void main()C obj;obj.fun(); obj.gun(); 定义了一个基类shape及派生类rectangle,通过sharpe的指针调用rectangle的area函数。共有四处错误。#include class shape public: int area () return 0; ; class rectangle: public shape int a, b;public: void setLength (int x = 0, int y)a=x; b=y; int area () return a*b; ; void main () rectangle r;r. setLength (3,5); shape *s=r;cout s.area () endl; 解答:1.2.四.编程题1. 利用重载求两个整数、三个整数和四个整数的最小值。2. 利用重载计算长方形、正方形、圆、梯形的面积。3. 利用重载实现对10个整数和10个实数的排序。4. 定义一个矩形类,用成员函数其周长、面积。5. 定义一个圆类,用成员函数其周长、面积。解答:.5.五.填空题1. ( )是指两个或两个以上的函数具有相同的函数名,但参数类型不一致或参数个数不同。2. 函数重载是C+对C语言的扩展,包括( )。3. 成员函数重载的一个很重要的应用就是重载( )。4. 通过对( )进行重载,可以实现定义对象时初始化赋值的多样性。5. 析构函数( )重载。(填可以或不能)6. 在函数调用时,若某个参数省略,则其后的参数皆应省略而采用( )。7. ( )是一个函数,它与一般函数的区别是在使用时可以像宏一样展开,所以没有函数调用的开销。8. 在内联函数体中,不能含有( ),如switch和while语句等。9. 在类内给出函数体定义的成员函数被默认为( )。10. 一个函数功能不太复杂,但要求被频繁调用,选用( )。11. 内联函数的关键字是( )。12. 函数重载是指两个或两个以上的函数具有( )的函数名,但参数类型不一致或参数个数不同。13. 函数重载是指两个或两个以上的函数具有相同的函数名,但( )不一致或参数个数不同。14. 函数重载是指两个或两个以上的函数具有相同的函数名,但参数类型不一致或( )不同。15. 在C+中,设置参数默认值时,应当( )设置。解答:1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 贵州工程职业学院《汽车检测与诊断技术》2023-2024学年第二学期期末试卷
- 2024年成囊材料项目投资申请报告代可行性研究报告
- 工程项目设计介绍
- 汽车日常维养核心要点
- 2025年广东佛山顺德区杏晖投资控股集团有限公司招聘笔试参考题库含答案解析
- 机械毕业设计致谢要点
- 高速公路定期检测项目技术状况指数MQI评定报告
- 2025年江苏南通市经济技术开发区总公司招聘笔试参考题库附带答案详解
- 汽车新纪元:零部件制胜-把握市场趋势开创未来
- 珠宝首饰店铺设计
- 2025-2031年中国核电用钛合金管道行业发展前景预测及投资方向研究报告
- 环保舆情防范预案
- 2025年甘肃公务员省考《行测》真题(含答案)
- JGJT322-2013混凝土中氯离子含量检测技术规程标准
- 具身智能项目建设规划方案(参考模板)
- 2025年福建厦门湖里区市场监督管理局招聘协管员93人高频重点模拟试卷提升(共500题附带答案详解)
- 科学小实验手摇发电机
- 西亚、中亚、北非音乐文化
- 三类人员安全教育
- 2024电能存储系统用锂蓄电池和电池组安全要求
- 2023年招聘业务员考试试题
评论
0/150
提交评论