《面向对象程序设计与VC++实践》习题答案.ppt_第1页
《面向对象程序设计与VC++实践》习题答案.ppt_第2页
《面向对象程序设计与VC++实践》习题答案.ppt_第3页
《面向对象程序设计与VC++实践》习题答案.ppt_第4页
《面向对象程序设计与VC++实践》习题答案.ppt_第5页
已阅读5页,还剩79页未读 继续免费阅读

下载本文档

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

文档简介

习题22 1 4 比较值调用和引用调用 值调用是指发生函数调用时 给形参分配内存空间 并直接将实参的值传递给形参 这一过程是参数的单向传递过程 一旦形参获得了值便与实参脱离关系 此后无论形参发生了怎样的改变 都不会影响到实参 引用调用不为形参分配内存 在执行主调函数中的调用语句时 系统自动用实参来初始化形参 形参是实参的一个别名 对形参的任何操作也就直接作用于实参 传址是把实参的地址传给形参 从而形参和实参同占一个内存空间 所以形参变实参也变 传值是实参把值赋值给形参 两个参数占有不同的地址 所以实参不随形参变化而变化 7 编写程序 打印如下的杨辉三角形 111121133114641 include include includevoidmain inti j a 5 5 1 第一行第一列赋值1 其他为0for i 1 i 5 i a i 0 1 第一列全置为1for j 1 j i j a i j a i 1 j 1 a i 1 j 每个数是上面两数之和 for i 0 i 5 i 输出杨辉三角 cout setw 5 i for j 0 j 5 i j printf for j 0 j i j 只输出a 5 5 的下三角cout setw 2 a i j printf 2d a i j cout endl 方法一 include include includevoidmain inti j a 5 5 for i 0 i 5 i a i 0 1 a i i 1 第一列 对角全置为1for j 1 j i j a i j a i 1 j 1 a i 1 j 每个数是上面两数之和 for i 0 i 5 i 输出杨辉三角 cout setw 5 i for j 0 j 5 i j printf for j 0 j i j 只输出a 5 5 的下三角cout setw 2 a i j printf 2d a i j cout endl 方法一变形 includevoidmain inta 5 9 i j for i 0 i1 j a i j a i 1 j 1 a i 1 j 1 for i 0 i 5 i for j 0 j 9 j if a i j 0 cout 值为0的元素输出为空格elsecout a i j cout endl 方法二 include defineN5voidmain inti j a N N a N N 1 1 1 for i 2 i N i a i 0 1 a i i 1 for j 1 j i j a i j a i 1 j 1 a i 1 j for i 0 i N i for j 0 j 20 2 i j printf for intk 0 k i k printf d a i k 正确为 printf 4d a i k printf n 错误答案 8 将一个一维数组中相同的元素删除到只保留一个 然后按由大到小的顺序输出 includevoidmain inta 6 1 7 3 7 22 9 inti j t flag intm 6 n n m n控制输出不同的元素个数 删除相同元素for i 0 i m i for j i 1 j n j if a i a j for intk j k n k a k a k 1 前移n j 冒泡排序for i 0 i n i flag 0 for j 0 j n i 1 j if a j a j 1 t a j a j a j 1 a j 1 t flag 1 if flag 0 break cout 排序后的数组为 n for i 0 i n i cout a i n 8 字符数组 include includevoidmain chara 8 agbbcc chart inti j for i 0 i 8 i for j 0 j i j if a i a j a i 0 for i 0 i 8 i cout setw 2 a i endl for i 0 i 8 i for j i 1 j 8 j if a i a j t a i a i a j a j t for i 0 i 8 i cout a i endl includevoidmain inta 10 1 2 1 4 1 6 9 6 7 6 inti j t flag b 10 冒泡排序for i 0 i 10 i flag 0 for j 0 j 10 i 1 j if a j a j 1 t a j a j a j 1 a j 1 t flag 1 if flag 0 break t 0 删除相同元素for i 0 i 9 t i if a i a i 1 for j i 1 j 10 t j a j 1 a j t 当相同元素多于2个时出错cout 排序后的数组为 n for i 0 i 10 t i cout a i n 错误答案 删除相同元素b 0 a 0 j 0 for i 1 i 10 i if a i b j b j 1 a i j 或if a i a i 1 b j 1 a i j 或if a j a i a j 1 a i j 相邻元素相等则删除一个 8 写一函数 实现两个字符串的比较 相等的结果为0 不等时结果为第一个不相等字符的ASCII差值 includeintcmps char p char q voidmain chars1 128 s2 128 cin s1 cin s2 cout cmps s1 s2 cmps s1 s2 intcmps char p char q for p 0 intcmps char p char q for p q intcmps char p char q while p q p q return p q 习题3 3 1填空题 1 生存期与存储区域关系密切 一般存储区域分为代码区 codearea 数据区域 dataarea 栈区 stackarea 和堆区 heaparea 代码区用来存放程序代码 与其他存储区相对应的生存期分别为静态生存期 局部生存期 和动态生存期 2 局部变量一般具有块作用域 还可能具有作用域 全局变量具有文件作用域 3 静态成员属于类 而不属于对象 它由同一个类的所有对象共同维护 为这些对象所共享 静态函数成员可以直接引用该类的静态数据和函数成员 而不能直接引用非静态数据成员 对于公有的静态函数成员 可以通过类名或对象名来调用 而一般的非静态函数成员只能通过对象名来调用 4 在实际程序设计中 一个源程序可划分为三个文件 类声明文件 类实现文件 和类的使用文件 3 3阅读程序题 1 以下是一个类中包含另一个类对象成员 类的组合 的例子 试分析并给出以下程序的运行结果 includeclassSon intage public Son age 1 Son inti age i voidprint cout 儿子的年龄是 age endl classFather intage Sons1 s2 类的组合public Father inta1 inta2 intf s2 a2 s1 a1 age f voidprint cout 父亲的年龄是 age endl Son 父亲的年龄是 38第一个儿子的年龄是 10第二个儿子的年龄是 5 include includeclassstudent charname 10 intage public student charin name intin age strcpy name in name age in age intget age returnage char get name returnname friendintcompare student 2 程序代码如下 voidmain studentstu student 王红 18 student 吴伟 19 student 李丽 17 inti min 0 max 0 for i 1 i 3 i if compare stu max stu i 1 max i elseif compare stu max stu i 1 min i cout 最大年龄 stu max get age 姓名 stu max get name endl cout 最小年龄 stu min get age 姓名 stu min get name endl 程序运行结果为 最大年龄 19 姓名 吴伟最小年龄 17 姓名 李丽 includeclassstack classnode intdata node prev public node intd node n data d prev n friendclassstack 3 下列程序实现的是堆栈的压入和弹出 其中有两个类 一个是结点类 它包含结点值和指向上一结点的指针 另一个类是堆栈类 数据成员为堆栈的头指针 它是结点类的友员 试分析程序 并说明堆栈的压入和弹出过程 classstack node top public stack top 0 voidpush inti intpop voidstack push inti node n newnode i top top n intstack pop node t top if top top top prev intc t data deletet returnc return0 main intc stacks for intj 0 j c s push c for j 0 j 10 j 10个整数出栈 cout s pop 实现了输入的顺序与输出的顺序相反cout n return1 分析 实现了输入的顺序与输出的顺序相反 3 4完成下列程序 1 下面是一个计算器类的定义 请完成该类的实现 成员函数的定义 并在主函数中先将计算器给定初值99 然后进行二次加1 一次减1 最后显示计算器的值 classcounter intvalue public counter intnumber 构造函数voidincrement 给原值加lvoiddecrement 给原值减1intgetvalue 取得计数值voiddisplay 显示计算器值 includeclasscounter intvalue public counter intnumber voidincrement 给原值加lvoiddecrement 给原值减1intgetvalue 取得计数值voiddisplay 显示计算器值 counter counter intnumber value number voidcounter increment value voidcounter decrement value intcounter getvalue returnvalue voidcounter display cout value value endl voidmain countercounter1 99 counter1 increment counter1 increment counter1 decrement counter1 display 2 下列程序接收若干用户的姓名和电话 然后输出 include iostream h include string h include iomanip h constintN 5 classperson charname 10 charnum 10 public voidgetdata strcpy name na strcpy num nu voidoutdata personpn N voidperson outdata personpn N inti for i 0 i N i cout width 10 cout width 10 voidmain char na 5 li zh li zh li char nu 5 01 02 03 04 05 personobj 5 对象数组for inti 0 i 5 i obj i getdata na i nu i pt obj char na char nu或 charna charnu coutoutdata pt 或 pt outdata obj 3 5编程题 1 定义一个名为rectangle的矩形类 其属性数据为矩形左上角和右下角点的坐标 要求通过对该类对象的调用计算矩形的面积 include includeclassRectangle intX1 Y1 X2 Y2 public Rectangle intx1 inty1 intx2 inty2 X1 x1 Y1 y1 X2 x2 Y2 y2 intArea intRectangle Area returnfabs X2 X1 Y2 Y1 voidmain RectangleA 10 20 30 50 cout 面积为 A Area endl 2 定义一个名为complex的复数类 其属性数据为复数的实部和虚部 要求构造函数和拷贝构造函数 并定义成员函数打印复数的值 includeclasscomplex 复数类声明 doublereal doubleimag public complex doubler 0 0 doublei 0 0 构造函数 real r imag i complex complex 3 定义一个名为circle的圆类 其属性数据为圆的半径 定义成员函数计算圆的面积 编写主函数计算一个内径和外径分别为5和8的圆环的面积 includeconstfloatPI 3 14159 definePI3 14159classcircle 类声明 doubleradius public circle doubler 0 0 radius r 构造函数doublearea returnPI radius radius voidmain circleA 5 circleB 8 cout 圆环面积 B area A area endl 4 试定义一个字符串类string 使其至少具有内容 contents 和长度 length 两个数据成员 分别定义不同的成员函数 用于显示字符串 求字符串长度 给原字符串后连接另一个字符串 include includeclassstring 类声明 intlength char contents public string length 0 contents 0 string constchar str 构造函数voiddisplay const 显示值voidgetlength voidappend constchar tail string string constchar str length strlen str contents newchar length 1 strcpy contents str voidstring display const 显示 if contents 0 cout empty n elsecout 字符串的内容为 contents endl voidstring getlength 获取长度 length strlen contents cout 字符串的长度为 length endl voidstring append constchar tail 连接 char tmp length strlen tail tmp newchar length 1 strcpy tmp contents strcat tmp tail deletecontents contents tmp voidmain strings0 s1 astring s0 display s1 display s1 getlength strings2 this s2 append astring cout 连接的 s2 display 5 编写一个程序 声明一个Cat类 拥有静态数据成员HowManyCats 用以记录Cat的个体数目 拥有静态成员函数GetHowMany 用以存取HowManyCats 设计程序并测试这个类 体会静态数据成员和静态成员函数的用法 includeclassCat staticintHowManyCat 引用性说明public Cat HowManyCat Cat HowManyCat staticvoidGet HowManyCat cout 总数 HowManyCat endl intCat HowManyCat 0 定义性说明voidmain Catf1 Cat Get HowManyCat Catf2 f2 Get HowManyCat f1 Cat Cat Get HowManyCat 7 实现一单链表的逆置 并输出逆置前后的结果 structNode intdata Node next classList Node head public List head NULL voidInsertList intaData intbData 链表结点的插入voidDeleteList intaData 链表结点的删除voidOutputList 链表结点的输出Node Gethead returnhead Node reverse Node head voidList OutputList 链表输出函数 Node current head while current NULL coutdatanext cout endl 7 实现一单链表的逆置 并输出逆置前后的结果 Node reverse Node head node p q r p head p指向1号q p next q指向2号while q NULL r q next r指向3号q next p 2号的next指向1号p q p指向2号q r q指向3号 head next NULL 1号的next指向NULL head p 循环结束之后 p指向原来的末结点 现在是头结点returnhead 返回头结点 习题4 4 1填空题 1 派生新类的过程经历三个过程 吸收基类成员 改造基类成员和添加新的成员 2 在类族中 直接参与派生出某类的基类称为直接基类 基类的基类甚至更高层的基类称为间接基类 3 在继承中 如果只有一个基类 则这种继承方式称为单继承 如果基类名有多个 则这种继承方式称为多继承 4 C 中的运算符除了类属关系运算符 作用域分辨符 成员指针运算符 sizeof运算符和三目运算符 之外 全部可以重载 而且只能重载C 中已有的运算符 不能臆造新的运算符 5 如果用普通函数重载双目运算符 需要1个操作数 重载单目运算符 需要0个操作数 如果用友员函数重载双目运算符 需要2个操作数 重载单目运算符 需要1个操作数 6 当基类中的某个成员函数被声明为虚函数后 此虚函数就可以在一个或多个派生类中被重新定义 在派生类中重新定义时 其函数原型 包括返回类型 函数名和参数个数 以及参数类型和参数的顺序都必须与基类中的原型完全相同 4 3阅读程序题分析下列程序 写出运行结果程序代码如下 考察 继承关系 includeclassB intx1 x2 public voidInit intn1 intn2 x1 n1 x2 n2 intinc1 return x1 intinc2 return x2 voiddisp cout B x1 x1 x2 x2 endl classD1 B intx3 public D1 intn3 x3 n3 voidInit intn1 intn2 B Init n1 n2 intinc1 returnB inc1 intinc2 returnB inc2 intinc3 return x3 voiddisp cout D1 x3 x3 endl classD2 publicB intx4 public D2 intn4 x4 n4 intinc1 inttemp B inc1 temp B inc1 temp B inc1 returnB inc1 intinc4 return x4 voiddisp cout D2 x4 x4 endl B x1 2 x2 2D1 x3 3D2 x4 6D2 x4 6D2 x4 6 voidmain Bb b Init 2 2 b disp D1d1 3 d1 Init 5 5 d1 inc1 d1 disp D2d2 6 d2 Init 4 4 d2 B disp d2 disp d2 inc1 d2 inc2 d2 B disp d2 disp d2 B inc1 d2 B disp d2 disp B x1 2 x2 2D1 x3 3B x1 4 x2 4D2 x4 6B x1 0 x2 3D2 x4 6B x1 1 x2 3D2 x4 6 2 程序代码如下 课件例题 考察 虚拟派生中构造函数的执行顺序 includeclassLevel1 声明基类Level1 public intn1 Level1 intin n1 n1 in n1 cout ThisisLevel1 n1 n1 endl classLevel21 virtualpublicLevel1 Level1为虚基类 派生类Level21 public intn21 Level21 inta Level1 a n21 a cout ThisisLevel21 n21 n21 endl classLevel22 virtualpublicLevel1 Level1为虚基类 派生类Level22 public intn22 Level22 inta Level1 a n22 a cout ThisisLevel22 n22 n22 endl classLevel3 publicLevel21 publicLevel22 声明派生类Level3 public intn3 Level3 inta Level1 a Level21 a Level22 a n3 a cout ThisisLevel3 n3 n3 endl voidmain Level3obj 3 定义Level3类对象obj ThisisLevel1 n1 3ThisisLevel21 n21 3ThisisLevel22 n22 3ThisisLevel3 n3 3 程序运行结果为 3 下列程序中 基类base和派生类d1 d2中都含有私有 保护和公有成员 d1类是base的派生类 d2是d1的派生类 试分析下列程序的访问权限 考察 继承中成员访问属性的变化 includeclassbase private intn1 protected intk1 public base n1 0 k1 1 voidfun1 cout n1 k1 endl classd1 publicbase intn2 protected intk2 public d1 n2 10 k2 11 voidfun2 cout n1 k1 endl cout n2 k2 endl classd2 publicd1 intn3 protected intk3 public d2 n3 20 k3 21 voidfun3 cout n1 k1 endl cout n2 k2 endl cout n3 k3 endl voidmain basebaseobj d1d1obj d2d2obj baseobj fun1 d1obj fun2 d2obj fun3 回答下列问题 l派生类d1中成员函数fun2 能否访问基类base中的成员fun1 n1和k1 l派生类d1的对象能否访问基类base中的成员fun1 n1和k1 l派生类d2中成员函数fun3 能否访问直接基类d1中的成员fun2 n2和k2 能否访问基类base中的成员fun1 n1和k1 l派生类d2的对象能否访问直接基类d1中的成员fun2 n2和k2 能否访问基类base中的成员fun1 n1和k1 以上程序有错 请改正 并上机验证 1 能访问base中公有的fun1 和保护的k1 不能访问私有的n1 2 能访问base中公有的fun1 不能访问n1和k1 3 能访问d1中公有的fun2 和k2 不能访问私有的n2能访问base中公有的fun1 和k1 不能访问私有的n1 4 能访问d1中公有的fun2 不能访问私有的n2和k2能访问base中公有的fun1 不能访问私有的n1和k1 fun2函数中 cout n1 k1 endl 替换为fun1 fun3函数中 cout n1 k1 endl cout n2 k2 endl 替换为fun2 4 下列程序是一个有关虚基类及其派生类的初始化的程序 如果虚基类定义有非默认形式的 即带形参 构造函数 在整个继承结构中 直接或间接继承虚基类的所有派生类 都必须在构造函数的成员初始化表中列出对虚基类的初始化 includeclassB1 public intn1 B1 intin n1 n1 in n1 cout B1 n1 n1 endl classB21 virtualpublicB1 public intn21 B21 inta B1 a n21 a cout B21 n21 n21 endl classB22 virtualpublicB1 public intn22 B22 inta B1 a n22 a cout B22 n22 n22 endl classB3 publicB21 publicB22 public intn3 B3 inta B1 a B21 a B22 a n3 a cout B3 n3 n3 endl voidmain B3obj 5 B1 n1 5B21 n21 5B22 n22 5B3 n3 5 程序运行结果为 如果程序运行结果为 B1 n1 5B22 n22 5B21 n21 5B3 n3 5上述程序应该怎么改 如果将B3 inta B1 a B21 a B22 a n3 a cout B3 n3 n3 endl 中的B21 a 和B22 a 的位置调换 程序的运行结果是否会有变化 classB3 publicB21 publicB22改为classB3 publicB22 publicB21 没有变化 初始化的顺序与继承顺序有关 与初始化列表的顺序无关 4 4完成下列程序 1 下列程序是一个从Point类私有派生新的矩形Rectangle类的程序 请填空完成程序 并上机运行验证 classRectangle private floatW H public voidInitR floatx floaty floatw floath W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW include includeclassPoint private floatX Y public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff floatGetX returnX floatGetY returnY classRectangle private floatW H public voidInitR floatx floaty floatw floath W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW voidmain Rectanglerect rect InitR 2 3 20 10 rect Move 3 2 cout Thedataofrect X Y W H endl cout rect GetX rect GetY rect GetW rect GetH endl 程序运行结果为 Thedataofrect X Y W H 5 5 20 10 X xOff Y yOff privatePoint InitP x y 2 下列程序中声明一个圆类circle和一个桌子类table 另外声明一个圆桌类roundtable 它是由circle和table两个类派生的 要求声明一个圆桌类对象 并输出圆桌的高度 面积和颜色 请填空完成程序 并上机运行验证 classtable doubleheight public table doubleh height h doubleget height returnheight classroundtable publictable publiccircle char color public roundtable doubleh doubler charc color newchar strlen c 1 char get color returncolor include includeclasscircle doubleradius public circle doubler radius r doubleget area return classtable doubleheight public table doubleh height h doubleget height returnheight classroundtable publictable publiccircle char color public roundtable doubleh doubler charc color newchar strlen c 1 char get color returncolor voidmain roundtablert 0 8 1 0 白色 cout 圆桌数据 endl cout 圆桌高度 rt get height endl cout 圆桌面积 rt get area endl cout 圆桌颜色 rt get color endl 3 14 radius radius circle r table h strcpy color c 3 这是一个用运算符重载为友元重载的方法重做复数加减法的运算 请填空完成程序并上机运行验证 类似课件例题 cl 5 4 c2 2 10 c3 c1 c2 3 6 c4 c1 c2 7 14 real r imag i complex c1 real c2 real c1 imag c2 imag complex c1 real c2 real c1 imag c2 imag includeclasscomplex private doublereal doubleimag public complex doubler 0 0 doublei 0 0 friendcomplexoperator complexc1 complexc2 friendcomplexoperator complexc1 complexc2 voiddisplay complexoperator complexc1 complexc2 return complexoperator complexc1 complexc2 return voidcomplex display cout real imag endl voidmain complexc1 5 4 c2 2 10 c3 c4 cout cl c1 display cout c2 c2 display c3 c1 c2 cout c3 c1 c2 c3 display c4 c1 c2 cout c4 c1 c2 c4 display 4 5编程题 1 声明一个水果类和树类 并描述它们的一些特征 在此基础上派生出苹果类和桃子类 定义它们的一些基本功能 2 编写程序声明一个Shape基类 再派生出Rectangle和Circle类 二者都有GetArea 函数计算对象的面积 includeconstdoublePI 3 14159 classShapes 基类Shapes intx y public Shapes intxx intyy 0 x xx y yy voidGetArea cout Theareaofshapesisundefined endl classRectangle publicShapes 派生类Rectangle intwide high public Rectangle intxx intyy intw inth Shapes xx yy wide w high h voidGetArea cout Theareaofrectangleis wide high endl 方法一 classCircle publicShapes 派生类Circle intradius public Circle intxx intyy intr Shapes xx yy radius r voidGetArea cout Theareaofcircleis PI radius radius endl voidmain Shapessh 5 8 sh GetArea Rectanglerect 5 8 10 10 rect GetArea Circlecir 5 8 10 cir GetArea 方法一 includeconstdoublePI 3 14159 classShapes 基类Shapes protected intx y public voidsetvalue intxx intyy 0 x xx y yy voidGetArea cout Theareaofshapesisundefined endl classRectangle publicShapes 派生类Rectangle public voidGetArea cout Theareaofrectangleis x y endl classCircle publicShapes 派生类Circle public voidGetArea cout Theareaofcircleis PI x x endl voidmain Shapessh Rectanglerect Circlecir sh setvalue 5 8 sh GetArea rect setvalue 5 8 rect GetArea cir setvalue 10 cir GetArea 方法二 3 编写一个能输入和输出学生和教师数据的程序 学生数据有编号 姓名 班号和成绩 教师数据有编号 姓名 职称和部门 要求声明一个person类 并作为学生数据操作类student和教师数据操作类teacher的基类 include includeclassperson private char name intno public person char n1 intn2 name newchar strlen n1 1 strcpy name n1 no n2 char get name returnname intget no returnno classstudent publicperson private chardepart 10 intdegree public student char n1 intn2 char n3 intn4 person n1 n2 strcpy depart n3 degree n4 intget degree returndegree char get depart returndepart 方法一 说明指针与数组的赋值问题 classteacher publicperson private charprof 10 chardepart 10 public teacher char n1 intn2 char n3 char n4 person n1 n2 strcpy prof n3 strcpy depart n4 char get prof returnprof char get depart returndepart voidmain students1 李明 1001 计算机 10 teachert1 曾二 899 教授 理学院 cout s1 get name s1 get no s1 get degree s1 get depart endl cout s1 get name s1 get no t1 get prof t1 get depart endl include includeclassperson char name num public person char str1 char str2 name newchar strlen str1 1 strcpy name str1 num newchar strlen str2 1 strcpy num str2 char get num returnnum char get name returnname classstudent publicperson char classnum intsorce public student char str1 char str2 char classnum1 intsorce1 person str1 str2 classnum newchar strlen classnum1 1 strcpy classnum classnum1 sorce sorce1 voidget student cout person get num endl cout person get name endl cout classnum endl cout sorce endl 方法二 说明在主函数中输入值问题 classteacher publicperson char dep pos public teacher char str1 char str2 char dep1 char pos1 person str1 str2 dep newchar strlen dep1 1 strcpy dep dep1 pos newchar strlen pos1 1 strcpy pos pos1 voidget teacher cout person get num endl cout person get name endl cout dep endl cout pos endl voidmain charnum 10 name 10 classnum 10 chardep 10 pos 10 intsorce cout num name classnum sorce coutget student cout num name dep pos teacherb num name dep pos ptr2 ptr2 数组与指针 数组的实质是内存中一块连续的空间 存储着同一类型变量的数据 要访问数组中的某个元素 可以通过 数组名 下标 这样的语法形式来实现 结论 如果知道数组第1个 下标为0 元素的地址 将这个地址保存在某个指针变量中 那么对数组元素的访问就完全可以转换为指针操作 C 语言就是这样对数组元素进行访问的 在声明一个数组时 C 语言将在内存中开辟两个空间 一个用于保存数组元素 另一个用于保存数组的第1个元素的地址 数组名就是用于保存数组第1个元素的地址的指针型常量 通过保存数组第1个元素地址的数组名这个指针型常量 可以使用 运算符访问数组元素 也可以直接使用指针的运算规则访问数组元素 字符串赋值 函数strcpy将第2个参数 字符串 复制到第1个参数 字符数组 中 这个字符数组的长度应当足以放下字符串及其NULL终止符 函数strncpy与strcpy相似 只是strncpy指定了从字符串复制到字符数组的字符数 include includevoidmain charpSource Iamateacher charpDestination youareastudent pDestination字符串长度 pSource字符串长度strcpy pDestination pSource 等价于strncpy pDestination pSource 16 cout pSource endl cout pDestination endl 4 重载函数Double x 要求返回值为输入参数的两倍 参数分别为整型 长整型 浮点型 双精度型 返回值类型与参数一样 includeconstdoublePI 3 14159 classMyclass public intDouble intx return2 x longDouble longx return2 x floatDouble floatx return2 x doubleDouble doublex return2 x voidmain Myclasstest inta 2 cout test Double a endl longb 2 cout test Double b endl floatc 2 0 cout test Double c endl doubled 2 0 cout test Double d endl voidmain Myclasstest cout test Double 5 endl cout test Double 5l endl cout test Double 5 0f endl cout test Double 5 0 endl voidmain Myclasstest cout test Double 5 endl cout test Double long 5 endl cout test Double float 5 0 endl cout test Double 5 0 endl includeconstdoublePI 3 14159 classShapes 抽象基类Shapes intx y public Shapes intxx intyy x xx y yy virtualvoidGetArea 0 纯虚函数成员virtualvoidGetPrim 0 纯虚函数成员 classRectangle publicShapes 派生类Rectangle intwide high public Rectangle intxx intyy intw inth Shapes xx yy wide w h

温馨提示

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

评论

0/150

提交评论