《C++期末复习(42页).doc》由会员分享,可在线阅读,更多相关《C++期末复习(42页).doc(44页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-C+第二章 C+的初步知识1、 面向对象程序设计的三大特性:封装性、继承性、多态性。2、 十进制、八进制、十六进制的输出形式:十进制:coutdecsetw(3)x八进制:coutoctsetw(6)x十六进制:couthexsetw(5)x /setw(m)表示设置输出宽度3、 定义常变量用const 例如:const int a=1;4、 函数模板TemplateT sum(Ta ,Tb)return a+b;#includeInt main() Int i1=6,i2=3,i; Double d1=12.34,d2=45.67,d; I=sum(i1,i2);d=sum(d1,d2);
2、cout”i_sum=”iendl;cout”d_sum=”dendl;return 0;5、 引用:#include Void main() Int intone; Int &rint=intone; Intone=5; Coutintoneendl; Coutrintendl; 6、 引用的特性:(1)、声明引用时,必须指定它代表的是哪个变量,即对引用初始化;(2)、引用初始化后不能再被重新声明为另一个变量的别名,如: Int a=3,b=4; Int &c=a; c=&b;错误 int &c=b;错误 (3)、不能建立“void”类型的引用Void &a=1;错误(4)、不能建立引用数组
3、Char a5=”like”;Char &ra5=a; 错误(5)、可以建立指针变量的引用Int i=1;Int *p=&i;Int *&pt=p;(6)、int a;Int &b=a;错误Int a=1;Const int &b=a; /可以用const限制b=5;错误,不可以改变引用值(7)、int temp=i+3;Const int &a=temp;是常量或表达式是必须用const7、 分别用指针变量和引用参数实现两个变量的值互换(1)、#includeVoid swap(int *p1,int *p2) int temp;temp=*p1;*p1=*p2;*p2=temp;int m
4、ain()int i=3,j=5;swap(&i ,&j);cout”i=”i” j”jendl;return 1;(2)、#includeVoid swap(int &a,int &b) int temp;temp=a;a=b;b=temp;int main()int i=3,j=5;swap(i ,j);cout”i=”i” j”jendl;return 1;程序的运行结果均为:i=5 j=38、 内联函数inline 的使用,只适用于小函数,而且经常被调用如:#includeInline double circle(double r)Return 3.14159*r*r;int main
5、()for(int i=1;i=3;i+) cout ”r=”i” s=|circle(i)endl;return 0;程序的运行结果为:r=1 s=3.14159r=2 s=12.2664r=3 s=28.2744019、new 与delete 分别相当于C语言中的malloc 和free 例如:.person *p;p=new person;.第三章 类和对象1、对私有数据成员,不能进行访问,如:class savingpublic:.private: floate balance;void fn()savings a;savings b;a.ance=100.5;错误,不能进行访问b.a
6、nce=200.5;错误2、构造函数与析构函数(1)、构造函数特点:没有返回值,可以有参数,可以重载;(2)、析构函数特点:没有返回值,没有参数,不能进行重载,不能随意调用3、构造函数与析构函数程序,分析运行结果#includeclass studentpublic:student()cout“constructing student.n”;semeshours=100;gpa=3.5;student() cout”destructing student.n”;protected: Int semeshours; float gpa; class teacher public:teacher(
7、)cout”constructing teacher.n”;Teacher() Cout”destructing teacher.n”;Class tutorpair()Public:Tutorpair()Cout”constructing tutorpair.n”;Nomeetings=0;tutorpair()Cout”destructing tutorpair.n”;Protected: Student student; Teacher teacher; Int nomeetings;Void main()Tutorpair tp;Cout”back in main.n”;运行结果为:C
8、onstructing studentConstructing teacherConstructing tutorpairBack in mainDestructing tutorpairDestructing teacher Destructing student3、程序分析题(1)、给出下列程序的运行结果Class testPrivate: int num;Public: test(); Int getint()return num; test();test:test()num=0;Test:test()cout”destructor is active”endl;void main()t
9、ext X3;cout”exiting main”endl; 该程序的运行结果为:Exiting mainDestructor is active Destructor is active Destructor is active (2)、完成下面类中的成员函数的定义:Class testprivate: int num; float f1;public: test(int,float f); test(test&);test:test(int n,float f) num=n; f1=f;test:test(test&t)num=t.num;fq=t.f1;第四章 类和对象深入讨论1、对象数
10、组的应用#includeclass exampublic: void set_x(int n)x=n;int get_x()return x;private: int x;int main()exam ob4;int I;for(i=0;i4;i+) obi.set_x(i);for(i=0;i4;i+) coutobi.get_x() ;coutendl;return 0;本程序的运行结果为:0 1 2 32、使用对象指针和对象引用作为函数参数(1)、#includeclass aclasspublid: aclass(int n) i=n; void set(int n) i=n; int
11、 get()private:int i;void sqr(aclass *ob)cout”copy of obj has I value of”;coutget()set(ob-get()&ob-get();main()aclass obj(10);sqr(&obj);cout”now,obj.i in main() has been changed:”;coutobj.get()”n”;return 0;程序的运行结果如下:copy of obj has I value of 10now,obj.i in main() has been changed:100(2)、#includeclas
12、s aclasspublid: aclass(int n) i=n; void set(int n) i=n; int get()private:int i;void sqr(aclass &ob)cout”copy of obj has I value of”;coutob.get()set(ob.get()&ob.get();main()aclass obj(10);sqr(obj);cout”now,obj.i in main() has been changed:”;coutobj.get()”n”;return 0;程序的运行结果如下:copy of obj has I value
13、of 10now,obj.i in main() has been changed:1003、对象成员的初始化#includeclass A public:A(int x1,float y1) x=x1;y=y1;void show() coutn x=x y=y;private:int x;float y;class B public:B(int x1,float y1,int z1):a(x1,y1) z=z1;void show() a.show();cout z=z;private:A a;int z;main()B b(11,22,33);b.show();return 0;程序的运
14、行结果为:x=11 y=22 z=334、一个源程序按照结构划分为3个文件,即是,X.h;X.cpp;Y.cpp (X表示类名,Y表示工程名)第五、六章1、基类中的public和proctected成员在派生类中可以直接访问;基类中的private成员在派生类中不可以直接被访问参考:访问公有继承基类的成员#includeclass Apublic: void setA(int i,int j) x=i; y=j;void showA()cout”x=”xendl;cout”y=”yendl;private: int x;protected: itn y;class B:public Apubl
15、ic:void setB(int i,int j,int k )setA(i,j);z=k;void showB()cout”x=”xendl;/非法,x在派生类B中为不可直接访问成员cout”y=”yendl;/合法,y在派生类B中为protected成员private: int z;int main()B obj;obj.setB(10,20,30);obj.showA(); /合法,showA()在类B中为public成员obj.showB();return 0;2、基类和对象成员初始化以及执行顺序p150 例5.6#includeclass Apublic:A(int i)a=i;co
16、ut”A constructor”endl;void dispA()cout”a=”aendl;private: int a;class Bpublic:B(int j):obj1(j+10)b=j;cout”B constructor”endl;private:int b;A obj1;class C:public Bpublic: C(int k):B(k-2),obj(k+2) c=k; cout”C constructor”endl; void dispC()obj.dispA();dispB();cout”c=”cendl;private: int c; A obj;void mai
17、n()C c(2);c.dispC();程序执行结果如下:A constructorB constructor A constructorC constructora=4;b=0;c=2;注意:在创建派生类的对象时,执行顺序为,1.基类的成员对象;2.基类;3.派生类成员对象;4.派生类3、虚基类解决二义性问题,注意程序的运行结果p163#includeclass Apublic:A()a=7;cout”A class a=”aendl;protected: int a;class B:virtual public Apublic: B()a=a+10;cout”B class a=”aend
18、l;class C:virtual public Apublic: C()a=a+20;cout”C class a=”aendl;class D:public C,public Bpublic:D()Cout”D class a=”aendl;void main()D obj;程序运行结果如下:A class a=7C class a=27B class a=37D class a=374、赋值兼容规则p168class B0 public: void display()coutB0:display()endl;class B1 :public B0 public: void display
19、()coutB1:display()endl;class D1 :public B1 public:void display();coutD1:display()display();void main() B0 b0; B1 b1; D1 d1; B0 *p; p=&b0; fun(p); p=&b1; fun(p); p=&d1; fun(p);此例说明,可以将派生类对象的地址赋值给基类,但通过这个基类类型的指针,缺只能访问从基类继承的成员,派生类中的程序无法执行。例6.4 p184class Shape public:Shape(int x1,int y1) x=x1;y=y1;virtu
20、al double area() return 0.0;private:int x,y;class rectangle :public Shape public:rectangle(int x1,int y1,int w1,int h1) w=w1;h=h1;:Shape(x1,y1)virtual double area( return 1.0*w*h;private:int w,h;void f0(Shape s) coutthe area iss.area()endl;void f1(Shape *p)coutthe area isarea()endl;void f2(Shape &r)
21、coutthe area isr.area()endl;void main() rectangle rect(10,10,100,200),*p=▭ f0(rect); f1(p); f2(rect);程序运行结果为:the area is 0the area is 20000the area is 200006、例6.9 p191文本输出中纯虚函数的设计#include#include class CWSpublic: virtual void mywrite()=0;class CHAR:public CWS public: CHAR(char ch)c=ch; void myw
22、rite() putchar(this-c);putchar(t);private: char c;class WORD:public CWSpublic:WORD(char*pw);void mywrite();private: char*word;WORD:WORD(char*pw)char*pw1=new char(strlen(pw)+1);int i=0,j=0;while(pwi!=&pwi!=0) pw1i=pw1j+;word=new char(i);while(ji) wordj=pw1j+;wordj= ;void WORD:mywrite()int i=0;while(w
23、ordi!= )putchar(wordi+);putchar(t);class STR:public CWS public: STR(char *ps); void mywrite();private: char *str;STR:STR(char *ps)str=new char(strlen(ps)+1);strcpy(str,ps);void STR:mywrite()int i=0;while(stri!=0)putchar(stri+);putchar(n);void fun(CWS&r)r.mywrite();void main()CHAR c1(c);WORD wd1(“:wo
24、rds”);WORD wd2(“double words”);STR st(“your words and letters”);fun(c1);fun(wd1);fun(wd2);fun(st);程序的运行结果如下:c words double words your words and letters第七章 运算符重载例7.2 p202#includeclass complexpublic:complex(double r=0,double i=0)real=r;image=I;friend complex operator+(complex c1,complex c2);friend com
25、plex operetor-(complex c1,complex c2);void display() coutreal”+”image”i”endl;private: double real; double image;complex operator+(complex c1,complex c2)complex temp;temp.real=c1.real+c2.real;temp.image=c1.image+c2.image;return temp;complex operator-(complex c1,complex c2)complex temp;temp.real=c1.re
26、al-c2.real;temp.image=c1.image-c2.image;return temp;void main()complex c1(5,6),c2(3,4),c3;c3=c1+c2;c3.display();c3=c1-c2;c3.display();c3=1.5+c1;c3.display()c3=c1+2.1;c3.display();程序的运行结果如下:8+10i 2+2i6.5+6i7.1+6i例7.3 p203class complex public:complex(double r=0,double i=0) real=r; image=i;complex oper
27、ator+(complex c) complex temp,c; temp.real=real+c.real; temp.image =image+c.image; return temp;complex operator-(complex c) complex temp; temp.real =real+c.real; temp.image =image+c.image ; return temp;void display() coutreal+imageiendl;private:double real;double image;void main() complex c1(5,6),c2
28、(3,4),c3; c3=c1+c2; c3.display (); c3=c1-c2; c3.display (); c3=c1+2.1; c3.display ();程序的运行结果为:省第九章C+的输入和输出例9.13 p255 对文本文件进行读写操作#includefstream.h#includestdlib.h#includeiostream.hvoid main() fstream outfile,infile; outfile.open(e:f1.doc,ios:out); if(!outfile) coute:f1.doc cant open.n;abort(); outfile湖北省武汉市n Welcome to HuBei!;outfile.close();infile.open(e:f1.doc,ios:in); if(!infile) coute:f1.doc cant open.n; abort(); char s80; while(!infile.eof() infile.getline(s,sizeof(s); coutsendl;infile.close ();注意:输入输出是对内存而言的,读写是针对磁盘而言的第 44 页-