C++复习提纲整理版.doc_第1页
C++复习提纲整理版.doc_第2页
C++复习提纲整理版.doc_第3页
C++复习提纲整理版.doc_第4页
C++复习提纲整理版.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

C+复习1. 第1章绪论:(1)所有黑体字考填空(关注动词,名词)(2)不同进制之间的换算(3)信息的存储单位及换算基本数据类型:整型(4字节)、浮点型(float,4;double, 8)、字符型(1个字节)、布尔型(1字节)数组:数组名是首地址;sizeof()函数2. 第2章C+简单程序设计:(1)常用变量所占用内存的大小,(2)G、M、K、byte和bit之间的转换(3)各种运算符及表达代表的意义(4)输入输出流(5)基本控制结构3. 第3章函数:(1)内联函数、带默认形参值的函数(1)函数重载的形式4. 第4章类与对象:(例1、2、3,习题9、10、11)习题4-9,4-11后面有答案。(1)构造函数和析构函数没有返回类型,(2)生成类的对象并同时初始化,(3)删除类的对象以及对象指针,(4)会设计一个基本的类,具有几个成员变量并对这些变量进行操作(返回值,重新设置值)5. 第5章数据的共享与保护:(例1、2、3、4、5)(1)const也可以区分重载,(2)静态变量的使用方法6. 第6章数组、指针与字符串:(习题4、17、18)17,18挑错 ,后面有答案。(1) 数组与指针的区别,(2) 计算数组与指针占用的内存空间7. 第7章继承与派生:(例3、4、5)(1) 不同派生方式的访问控制,(2) 继承时调用构造函数和析构函数的顺序,8. 第8章多态性:(例题4、5、6,习题6)习题8-6后面有答案。(1) 什么是抽象类,习题4-9,设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。#includeusing namespace std;class Rectangle()public:Rectangle(inttop,intleft,intbottom,int right);Rectangle()intgetTop() const return top;intgetLeft() constreturn left;intgetBottom() constreturn bottom;intgetRight() constreturn right;voidsetTop(int top)top=top;voidsetLeft(int left)left=left;voidsetBottom(int bottom)bottom=bottom;voidsetRight(int right)right=right;intgetArea()const;private:int top;int left;int bottom;int right;Rectangle:Rectangle(inttop,intleft,intbottom,int right)this-top=top;this-left=left;this-bottom=bottom;this-right=right;int Rectangle:getArea() constint width=right-left;int height=top-bottoom;return(width*height);int main()Rectangle rect(100,20,50,80);coutArea:rect.getArea()endl;return 0;习题 4-11定义并实现一个矩形类,有长和宽两个属性,由成员函数计算矩形的面积。在类中初始化-构造函数释放空间析构函数定义求矩形面积的函数#includeusing namespace std;class Rectangle public:Rectangle(float l,float w)length=l;width=w;Rectangle()floatgetArea()return length*width;floatgetlength()return width;private:float length;float width;int main()float length, width;coutlength;coutwidth;Rectangle r(length,width);cout长为:length宽为:width的矩形的面积为:r.getArea()endl;return 0;习题6-4已知有一个数组名叫oneArray,用一条语句求出其元素的个数。答案intnArrayLength=sizeof(oneArray) 或sizeof(oneArray0);习题6-17下面程序有什么问题?#includeusing namespace std;int main()int *p;/指针 P没有初始化,即没有指向某个确定的内存单元,而是指向内存中随机的一个地址,随后为这个随机地址赋值存在风险。*p=9;coutThe value at p:*p;return 0;习题6-18下面程序有什么问题?改正。#includeusing namespace std;int fn1();int main()int a=fn1();coutthe value of a isa;return 0;int fn1()int *p=new int (5);return *p;解答:本程序中给*p分配的内存没有被释放掉。修改#includeusing namespace std;int *fn1();int main()int *a=fn1();coutthe value of a isa;delete a;return 0;int *fn1()int *p=new int (5);return p;习题8-6编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数getArea()、计算对象周长的函数getPerim()。源程序:#inclueusing namespace std;class Shapepublic:Shape()Shape()virtual float getArea()=0;virtual float getPerim()=0;classCircle:pulbic Shapepublic:Circle(float radius):itsRadius(radius)Circle()floatgetArea()return 3.14*itsRadius*itsRadius;floatgetPerim()return 6.28*itsRadius;private:floatitsRadius;classRectangle:public Shapepublic:Rectangle(float len,float width):itsLength(len),itsWidth(width);Rectangle() ;virtual float getArea() return itsLength*itsWidth;floatgetPerimi()return 2*itsLength+2*itsWidth;virtual float GetLength() return itsLength;virtual float GetWidth() return itsWidth;private:floatitsWidth;floatitsLength;intmian()Shape *sp;sp=new Circle(5);coutThe area of the Circle is getArea()endl;coutThe perimeter of the Circle is getPerim()endl;deletesp;sp=new Rectangle(4,6);coutThe area of the Rectangle is getArea()endl;coutThe perimeter of the Rectangle is getPerim()endl;deletesp;return 0;二类的设计1. 重载函数:名称相同,参数不同(类型、数量),const2. 设计类并测试:构造函数的作用及特殊性默认构造函数(无参、当提供带参数的构造函数后就不可调用)拷贝构造函数的作用及特点静态变量的初始化,带默认形参值的函数定义类定义后加分号三、类的继承1. public,private,protected继承后,父类成员在子类成员中的访问属性2. 子类的声明:初始化列表用于调用父类构造函数及对象成员构造函数调用3. 子类对象的构建与析构构造次序:调用父类构造函数(对于多继承,按继承声明时的顺序)调用子类中对象成员的构造函数(对于多个对象,按照在子类中的声明次序)子类中其他非对象成员的初始化析构次序:子类中非对象成员的析构(主要针对动态内存分配,一般采用默认构造函数即可)调用子类中对象成员的析构函数(对于多个对象,按照在子类中的声明次序的逆序)调用父类析构函数(对于多继承,按继承声明时顺序的逆序)1. 虚基类的概念与作用,(纯)虚函数声明及与多态性及抽象类的关系。四、参考习题与解答1. 子类的派生及其构造函数(习题7-10的扩展)#include#includeusing namespace std;class Object public:Object(float w) coutconstructing Objectendl;weight=w;Object() coutdestructing Object endl;voidsetWeight(float w)weight=w;floatgetWeight()return weight;private:float weight;classBox:public Objectpublic:Box(float wt, float ht, float wh):Object(wt) coutconstructing Box endl; height=ht;width=wh;Box() coutdestructing Box endl;voidsetWidth(float wh)width=wh;voidsetHeight(float ht)height=ht;floatgetWidth()return width;floatgetHeight()return height;float volume();return width*width*height;float density()return getWeight()/volume(); void displayInfo()coutWeight:getWeight() Height:height Width:widthendl;private:float height, width; ;int main()Box box(10,4,5); box.displayInfo();coutVolume:box.volume()endl;coutDensity:box.density()endl;box.setWidth(2);box.setWeight(8);box.displayInfo();coutVolume:box.volume()endl;coutDensity:box.density()endl;return 0; 2. 子类对象的创建与析构#includeusing namespace std;class Apublic:A(int a)coutaendl;A()coutDeconstruct Aendl;class Bpublic:B(int b)coutbendl;B()coutDeconstruct Bendl;class Cpublic:C(int c)coutcendl;C()coutDeconstruct Cendl;class Dpublic:D(int d)coutdendl;D()coutDeconstruct Dendl;class E: public B,public Apublic:E(int t1,int t2,int t3,int t4,int t5):A(t1),B(t2),c0(t3),d0(t4)e=t5;private:C c0;D d0;int e;int main()E e0(1,2,3,4,5);return 0;/* run ouput-2134Deconstruct DDeconstruct CDeconstruct ADeconstruct B-*/3. 虚函数与多态的参考代码#include#includeusing namespace std;class Mammal public:Mammal(int kind) kindOfMammal=kind;intgetKind()return kindOfMammal;virtual void speak()=0;private:intkindOfMammal;classDog:public Mammalpublic: Dog (int kind, string name):Mammal(kind) nameOfDog=name;void speak()coutwangwangendl;private:stringnameOfDog; ;classCat:public Mammalpublic: Cat (int kind, string colour):Mammal(kind) color=colour;void speak()coutmiaomiaospeak(); void MammalSpeak2(Mammal& ml) /通过继承实现多态ml.speak(); int main() Dog dog(2,Tom); Cat cat(2,yellor); MammalSpeak1(&dog);MammalSpeak1(&cat);coutendl;MammalSpeak2(dog);MammalSpeak2(cat);return 0; 程序设计重点题习题一/设计BankAccount类,具有私有数据成员如下:账户余额balance,利率interest_rate,设计构造函数BankAccount()和BankAccount(double dollars,double rate),以及set和get方法,并测试该类。#include iostreamusing namespace std;classBankAccountpublic:BankAccount()balance=0;interest_rate=0;BankAccount(double dollar,double rate)balance=dollar;interest_rate=rate;voidSetbalance(double x)balance=x;voidSetinterest_rate(double x)interest_rate=x;doubleGetbalance()return balance;doubleGetinterest_rate()return interest_rate;void update();private:doubleinterest_rate,balance;voidBankAccount:update()balance+=balance*interest_rate;void _tmain(intargc, _TCHAR* argv)BankAccount a;BankAccountb(15,0.1);coutAt frist as balance is a.Getbalance()endl;coutAt frist as interest_rate is a.Geti

温馨提示

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

评论

0/150

提交评论