




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一章 1 如何理解对象 类的概念 对象和类的关系 对象 从概念上讲 对象是客观世界的实体及一些无法触及的抽象事件 从实现机制看 对象是状态和方法的封装体 类 是对一组客观对象的抽象 它将该组对象具有的共同特征集中起来 以说明该组对象的能力和性质 对象和类是抽象和具体的关系 对象是执 行过程中由所属的类动态生成的 如何理解封装 继承 多态的概念 封装是一种信息隐蔽技术 指的是将一个数据和与这个数据有关的操作集 合放在一起形成对象 用户只能见到对象封装界面上的信息 对象内部对 用户是隐蔽的 封装的目的是将设计者和使用者分开 继承性是自动地共享类 子类和对象中的方法和数据的机制 继承提供了 创建新类的方法 新类称为子类或派生类 已有类称为父类或基类 子类 继承父类的全部方法和属性 而自己还具有新增加的属性 多态性指发出同样的消息被不同类型的对象接收时导致完全不同的行为 发送消息即成员函数的调用 不同的行为对应不同的实现 多态包括编译 时多态和运行时多态 利用重命名 重定义让各个对象自己去解释 执行 对于需求分析和设计极为有利 第二章 2 C 与 C 语言的关系如何 本质差别是什么 C 与 C 语言兼容 对 C 语言作了很多改进 包括增加了新的运算符 增加了引 用的概念 允许函数重载 引进内联函数 对变量的说明更灵活 本质差别 C 既支持面向过程的设计 也支持面向对象的程序设计 是一种混 合语言 而 C 语言只支持面向过程的程序设计 3 如何用 visual C 版本编译系统编译运行一个 C 的源程序 见电子教案 JiaoAn 48 4 4 写出下列表达式的值 1 float x 2 5 y 4 7 int a 7 x a 3 int x y 2 4 2 int a b a 2 b 5 a b a b 2 2 5 3 9 5 编写程序实现输入一个整数 输出相应的五分制成绩 设 90 分以上为 A 80 分至 89 分为 B 70 至 79 分为 C 60 到 69 分为 D 60 分以下为 E include void main int grade cout grade if grade 100 grade 0 cout 90 cout 80 cout 70 cout 60 cout D n else cout E n 6 编写程序求 1 2 10 include void main long sum 0 t 1 for int i 1 i 10 i t i sum t cout sum sum endl 7 用循环语句编写程序打印如下图案 include void main for int i 1 i 4 i for int j 1 j 4 i j cout for int j 1 j 2 i 1 j cout cout endl 8 一个 10 个整数的数组 34 91 83 56 29 93 56 12 88 72 找出最小数和其下标 并打印出来 include void main int array 34 91 83 56 29 93 56 12 88 72 int index 0 for int i 1 ia i index i cout 最小数 array index endl 相应的下标 index endl 第 3 章习题答案 1 分析下列程序 写出运行结果 6 11 6 12 2 分析下列程序 写出运行结果 main x 5 y 1 n 1 fun x 6 y 21 n 11 main x 5 y 1 n 11 func x 8 y 31 n 21 while s 0 0 n void fun char s int n char ch for int i 0 i n 2 i ch s i s i s n i 1 s n i 1 ch s n 0 第 4 章习题答案 5 定义一个满足如下要求的 Date 类 1 用下面的格式输出日期 日 月 年 2 可运行在日期上加一天操作 3 设置日期 ifndef DATE define DATE include class Date public void Display void AddOneDay void SetDay int y int m int d protected bool Legal int y int m int d bool IsLeapYear int y int year int month int day void Date Display cout day month year 9999 y 1 d 1 m12 return false int dayLimit 31 switch m case 4 case 6 case 9 case 11 dayLimit if m 2 dayLimit IsLeapYear y 29 28 return d dayLimit false true bool Date IsLeapYear int y return y 4 endif 第 5 章 思考题 1 对象引用做函数参数与对象指针做函数参数有何不同 2 什么是 this 指针 它有何用 3 运算符 new 和 delete 的功能是什么 它们可以用来动态创建对象吗 4 函数的传址调用和引用调用各自有何特点 为什么 C 中经常使用引用 调用 习题 1 写出下列程序的输出结果 a 5 b 8 a 8 b 5 第 6 章习题答案 1 给定如图 1 所示的继承图 写出类的定义 在 C 类中访问 A 类的成员 函数以设置何读取数据 class A public int GetpA return pA void SetpA int a pA a private int pA class B public A public void OnB class C virtual public A virtual public B public Sc use GetpA and SetpA 2 实现如图 2 所示的类层次结构 A pA GetpA SetpA B OnB C Sc Shape ThreeDimensionShapeTwoDimensionShape SquareCircleTriangleCubeSphereCylinder include include using namespace std const double PI 3 14159265358979 class Shape public virtual void print 0 virtual float area 0 class TwoDimensional public Shape public virtual float perimeter 0 class ThreeDimensional public Shape public virtual float volume 0 class Circle public TwoDimensional public Circle float r radius r void print cout Shape is a circle n float perimeter return 2 PI radius float area return PI radius radius private float radius class Cone public ThreeDimensional public Cone float r float h radius r height h void print float area float volume return PI radius radius height 3 private float radius height void Cone print cout Cone radius radius height height endl float Cone area float s sqrt radius radius height height return PI radius radius s 3 写出下面程序运行结果写出下面程序运行结果 GrandParent is called Mother is called Father is called No1 Pet is called No2 Pet is called Baby is called 第 7 章习题答案 1 定义复数类的加法与减法 使之能执行下列运算 Complex a 2 5 b 5 3 c 0 0 c a b c 4 1 a c 6 5 6 include class Complex public Complex double r 0 double v 0 real r virt v friend Complex operator Complex a Complex b friend ostream private double real double virt ostream Complex operator Complex a Complex b return Complex a real b real a virt b virt void main Complex a 2 5 b 7 8 c 0 0 c a b cout c c 4 1 a cout c c b 5 6 cout c 2 编写一个时间类 实现时间的减 读和输出 include include include include class Time public Time char s NULL void operator Time void operator Time void Display protected int hour int minute int second Time Time char s char str 9 0 char t 3 0 if s strncpy str s 8 else strtime str hour atoi strncpy t str 2 24 minute atoi strncpy t second atoi strncpy t void Time operator Time st second ti second second st 60 st st 60 minute ti minute minute st 60 st st 60 hour ti hour hour st 24 void Time operator Time st 60 second ti second second st 60 st 60 st 60 minute ti minute 1 minute st 60 st 24 st 60 hour ti hour 1 hour st 24 void Time Display char result 9 XX XX XX result 0 0 hour 10 result 1 0 hour 10 result 3 0 minute 10 result 4 0 minute 10 result 6 0 second 10 result 7 0 second 10 cout result endl void main Time a Time b 02 15 30 a b a Display 第 9 章习题 1 编写一个测试程序 它初始化一个模版函数 这个函数模版返回两个值 中最小值的 include using namespace std template T min T x T y return x y x y include Ratio h int main cout min 22 44 min 22 44 endl cout min 66 66 33 33 min 66 66 33 33 endl Ratio x 22 7 y 314 100 cout min x y min x y endl 2 以下是一个整数栈类的定义 const int SIZE 100 class Stack public Stack Stack void Push int n int Pop private int stack SIZE int tos 编写一个栈类模版 以便为任何类型的对象提供栈结构数据操作 在应 用程序中创建整数栈 字符栈和浮点数栈 并提供一些数据供进栈 退栈 和打印操作 include template class Stack public Stack Stack delete stack void Push T T Pop private static const int SIZE T stack int tos template const int Stack SIZE 100 template Stack Stack tos 0 stack new T SIZE template void Stack Push T stack tos n template T Stack Pop if tos 0 return T 0 return stack tos void main Stack ai Stack ac Stack af int i 3 3 5 7 ai Push i 0 ai Push i 1 ai Push i 2 char c 3 2 5 9 ac Push c 0 ac Push c 1 ac Push c 2 float f 2 1 5 3 8 af Push f 0 af Push f 1 cout ai Pop cout ai Pop cout ai Pop endl cout ac Pop cout ac Pop cout ac Pop endl cout af Pop cout af Pop endl 第 10 章习题答案 3 编写一个程序 它读入一个文件 并统计文件的行数 include include void main char str 128 int count 0 ifstream fin 10 1 cpp fin getline str sizeof str while fin eof count fin getline str sizeof str cout the amount of lines of file 10 1 cpp is count endl 4 写一个右对齐文本的程序 它应该重复读入一个左对齐的行序列 然后 以右对齐的格式输出它们 例如 输入 Listen my children an you will hear Of the midnight ride of Paul Revere On the eighteenth of April in Seventy five 会输出 Listen my children an you will hear Of the midnight ride of Paul Revere On the eighteenth of April in Seventy five include include include using namespace std int main ifstream in 10 1 in const int SIZE 100 maximum number of lines stored string line SIZE s int n 0 len maxlen 0 while in eof getline in s len s length if len 0 cout s maxlen maxlen len line n s n n number of lines read for int i 0 i n i s line i len s length cout string maxlen len s endl 5 实现一个通讯录打印程序 通讯录的记录格式为 姓名 单位 电话 住址 宅电 要求先建立 Person 类 然后案对象读入和打印 include include include include class AddressBook public AddressBook char n char p char h virtual void Display ostream protected char name 20 char phone 10 char handp 15 AddressBook AddressBook char n char p char h strncpy name n 19 name 19 0 strncpy phone p 9 phone 9 0 strncpy handp h 14 handp 14 0 void AddressBook Display ostream ostream return out void main AddressBook a1 Dill Arnson 8869533AddressBook a2 Welch Shammas 6695482AddressBook a3 Portel Braumbel 5937221cout a1 cout a2 cout a3 第 11 章习题答案 6 给课件中多路捕获程序添加一个 Pastm 异常类型的处理程序 如果 运 算符重载在 String 对象中检测到一个字符 按字典顺序在 m 之后的 小写字母 该异常处理程序救灾屏幕上显示一个错误 include include class String public String char int int Length return len class Range 异常类 1 public Range int j index j int index class Size 异常类 2 class Pastm 异常类 3 char if k return p k throw Range k private char p int len static int max int String max 20 String String char str int si if si 0 max si throw Size p new char si strncpy p str si len si void g String for int n 0 n num n cout str n cout endl void f 代码区 1 try 代码区 2 String s abcdefghijklmnop 16 g s catch String Range r cerr out of range r index endl 代码区 3 catch String Size cerr nerror size illegal n catch String Pastm cerr nerror lower cast found following m n 代码区 4 cout nThe program will be continued here n n void main 代码区 5 f cout nf is completed here n 7 设有下列类声明 class A public A n new int init private int n 写出 init 引发异常的处理程序 include class A public class Error A n new int init private int n void init do something throw Error void main try A a catch A Error 北方交通大学成教学院 2002 2003 学年第二 学期 面向对象程序设计及应用 期末试卷 A 答案 一 选择题 30 分 i C ii A iii C iv A v C vi B vii D viii D ix B x C xi C xii A xiii C xiv B xv C 二 阅读下面程序 找出其中的错误并说明理由 12 分 class Point double x y public Point double ix double iy ix x iy y void ShowPoint void ShowPoint void Point ShowPoint cout x y endl void main Point point 对象 point 和引用 p 应初始化 p point p ShowPoint cout point x point y endl main 中不能直接调用 x y 三 程序分析题 38 分 1 在主函数旁给出程序运行结果 8 分 4 在主函数旁给出程序运行结果 10 分 在主函数旁给出程序运行结果 10 分 请尝水果 这水果味道好极了 请尝橙子 这橙子味道美极了 我是中国第3号大橙子 这橙子味道美极了 我是美国1号苹果 这水果还可以 4 在主函数旁给出程序运行结果 10分 GrandParent is called Mother is called Father is called No1 Pet is called No2 Pet is called Baby is called 四 程序设计与填空 20 分 l 略 北方交通大学成教学院 2002 2003 学年第二 学期 面向对象程序设计及应用 期末试卷 B 答案 五 选择题 30 分 1 D 2 A 3 C 4 B 5 C6 B 7 D8 C 9 C 10 C 11 B 12 A 13 B 14 C 15 C 六 阅读下面程序 找出其中的错误并说明理由 12 分 class Score double mScore public Score double zS mScore zS cout Myscore is initialized as mScore void ShowScore friend void friendFun erro
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年长春理工大学公开招聘博士人才(71人)考前自测高频考点模拟试题及参考答案详解一套
- 班组安全教育和培训计划课件
- 2025年烟台市公费医学生考试选聘(139人)考前自测高频考点模拟试题及答案详解(考点梳理)
- 班组安全培训课件提意见
- 班组安全培训记录范例课件
- 2025广西南宁上林县白圩镇中心卫生院招聘村卫生室公共卫生服务协助人员5人考前自测高频考点模拟试题含答案详解
- 2025安徽芜湖市特种设备检验研究院招聘编外人员6人考前自测高频考点模拟试题及参考答案详解一套
- 2025北京明天幼稚集团招聘模拟试卷附答案详解(模拟题)
- 2025河北沧州孟村饶安高级中学招聘1人考前自测高频考点模拟试题带答案详解
- 2025福建厦门市集美区坑内小学顶岗教师招聘2人模拟试卷及答案详解(网校专用)
- 葫芦种植技术
- 热敏电阻器配方设计与制备工艺详解
- 监理工程师题库检测试题打印含答案详解【完整版】
- 《英语(第三版)》课件-Unit 3
- 2025-2026学年九年级英语上学期第一次月考 (江苏省连云港专用)原卷
- 2《归园田居》任务式公开课一等奖创新教案(表格式)统编版高中语文必修上册
- 银行文明礼仪课件
- GB/T 18380.12-2022电缆和光缆在火焰条件下的燃烧试验第12部分:单根绝缘电线电缆火焰垂直蔓延试验1 kW预混合型火焰试验方法
- GB 12326-2000电能质量电压波动和闪变
- 《空气动力学》配套教学课件
- 安全经验分享食物中毒
评论
0/150
提交评论