C++程序设计例题.doc_第1页
C++程序设计例题.doc_第2页
C++程序设计例题.doc_第3页
C++程序设计例题.doc_第4页
C++程序设计例题.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

C+程序设计例题1#include/类的定义格式class jk1public:int a;int b; void f1()cout注意函数的书写格式n; void f2()cout注意中英文标点符号n;void main() jk1 x;x.a=66;x.b=88;coutx.ax.b;x.f1();x.f2();/*1. 格式:class jk1 . ; (1)关键字 (2)取的类名 (3)对类的描述 (4)类定义的结束2. 英文标点符号3. 类定义的另一种形式 void main() class jk1public:char *str;double x;x; x.str=五羊开泰;x.x=3.1415926;coutx.strx.x; */2 #include/公有属性、私有属性、保护属性class ok2public:int a; void fa()cout在任何函数中都可以访问它n;private:int b;void fb()cout只有成员函数才可以访问n;protected:int c; void fc()cout除了成员函数可以访问外,派生类的成员函数才可以访问n;void main()ok2 x;x.a=11;coutx.a;x.fa();x.b=22;coutx.b;x.fb();x.c=33;coutx.c;x.fc();3#include/成员的私有属性使用#includeclass ok3 char a20;int b;void kk()cout我是私有函数,不可随随便便调用哟。n;public:void inab(char aa,int bb)strcpy(a,aa);b=bb; void outab()coutaendlbendl;kk();void main()ok3 x;x.inab(1234567,888);x.outab();4 #include/成员函数的另一种书写格式class ok4 int a;int b;public: void f(int x)a=x;coutaendl; void g(int y); void h();void ok4:g(int y)b=y;coutbendl;void ok4:h()cout我是函数定义语句,在类中应该有函数声明语句endl;void main()ok4 x;x.f(100);x.g(200);x.h();5 #include/内联函数class ok5public:int a; void cina(int aa)a=aa;cout在类中定义的成员函数默认为内联函数; void out(); ;inline void ok5:out()cout在类外定义的成员函数要用inline声明才能为内联函数;void main()ok5 x;x.cina(88);coutx.aendl;x.out();6 #includeiostream.h/构造函数一class ok6private:int a;double b;public:ok6()a=0;b=0.0;cout我是构造函数; void cinb(int aa,double bb)a=aa;b=bb; void outab()coutabendl; ;void main()ok6 x;x.cinb(99,3.1415926);x.outab();7 #include/构造函数二class ok7private:int a;double b;public:ok7(int x,double y)a=x;b=y;cout二个参数endl; ok7(int x)a=x;cout一个参数endl; ok7()cout不带参数endl; void out()coutabendl; ;void main()ok7 x(77,3.1415926);x.out();ok7 y(88);y.out();ok7 z;z.out();8 #include/拷贝构造函数class ok8 int a;public:ok8(int aa)a=aa;cout我是构造函数n; ok8(ok8 &x)a=x.a;cout我是拷贝构造函数n; void out()couta;void main() ok8 x(100);x.out();/调用构造函数ok8 y(x);y.out();/调用拷贝构造函数ok8 z=x;z.out();/调用拷贝构造函数9 #include/释放函数,对象释放时调用它,顺序相反class ok int a;public:ok(int aa)a=aa; ok()couta可以调用,不可重载!n;void main() ok x1(1),x2(2);x1.ok();ok y(33);x2.ok();10 #include/典型的构造函数的举例#includeclass Person char name10,sex;int age;public:Person(char *n,int a,char s) strcpy(name,n);age=a;sex=s; Person() Person()cout再见!n; void print()coutname/age/sexendl;void main()Person w(王老五,28,m);Person c(翠花,20,w);w.print();c.print(); w.Person(); Person x; x=w; x.print();11 #include/对象成员的访问、THIS指针、对象数组、动态对象#includeclass pb1 public: int a;double b; pb1(int aa,double bb)a=aa;b=bb;couta好!n; pb1()cout好好!n; pb1()couta再会!n; void out()couta b全局变量void main()pb1 x(1,1.1);x.out();/静态分配内存,用栈(内存中的一个区域),分配速度快但不灵活-局部变量pb1 *p;p=new pb1;p-a=2;p-b=2.2;p-out();delete p;/动态分配内存,用堆(自由内存),比较灵活但分配速度慢-局部变量pb1 a2;a0.a=66;a0.b=66.66;a1.a=88;a1.b=88.88;a0.out();a1.out();pb1 *b2;b0=new pb1;b1=new pb1;12 #include/静态成员(数据、函数)class wg double m;static double sm;static int sn;public:wg(double m0)m=m0;sm+=sm+m0;sn+; wg()sm-=sm-m;sn-; static void disp() cout卖瓜的总重量为:smendl; cout卖瓜的总数为:snendl; ;double wg:sm=0;int wg:sn=0;void main()wg x1(5.5),x2(6.6),x3(2.2),x4(3.5);x2.wg();x1.disp();x4.disp();13 #include/友元函数#includeclass girl char * name,* dial;public:girl(char *n,char *d) name=new charstrlen(n)+1;strcpy(name,n); dial=new charstrlen(d)+1;strcpy(dial,d); friend void disp(girl x);void disp(girl s) s.dialendl;void main()girl w(小白菜;disp(w); /*friend void disp(girl&x);在girl类中声明disp()为girl类的友元函数*/14 #include#includeclass boy;/必须先声明class girl char * name,* dial;public:girl(char *n,char *d) name=new charstrlen(n)+1;strcpy(name,n); dial=new charstrlen(d)+1;strcpy(dial,d); friend boy;/声明类boy为本类的友元类;class boy char * name;int age;/定义友元类,最好在类girl之后定义public:boy(char *n,int a) name=new charstrlen(n)+1;strcpy(name,n);age=a; void disp(girl s)coutboy的name age为:name ageendl; coutgirl的name dirl为:s.dialendl;/本函数中用到了类girl,即使类boy在类girl之前定义,本函数的定义也必须类girl之后定义;void main()boy b(赵六,18);girl g(小白菜;b.disp(g); 15 #include#includeclass girl;/必须先声明class boy char * name;int age;/必须在类girl之前定义public:boy(char *n,int a) name=new charstrlen(n)+1;strcpy(name,n);age=a; void disp(girl s);/此为声明语句,其函数定义必须在类girl之后;class girl char * name,* dial;public:girl(char *n,char *d) name=new charstrlen(n)+1;strcpy(name,n); dial=new charstrlen(d)+1;strcpy(dial,d); friend void boy:disp(girl s);void boy:disp(girl s)/定义成员友元函数coutboy的name age为:name ageendl;coutgirl的name dirl为: s.dialendl;void main()boy b(赵六,18);girl g(小白菜;b.disp(g); /*friend void disp(girl&x);在girl类中声明disp()为girl类的友元函数*/16 #include/顺序栈、静态对象class stack int a10,top;public: stack()top=-1;void push(int data)if(top=-1)&(top9)top+;atop=data;coutatop-1)&(top=9)coutatop出栈了!n;top-;else cout已到栈底再也无法出栈!n;void main()stack x;x.push(135);x.push(246);x.pop();x.pop();x.pop();17 #include/动态栈class stack int size,*root, *top;public: stack(int n)size=n;top=root=new intsize;stack()delete root;void push(int data)if(top=root+size)cout栈已满无法入栈!n;else *top+=data;coutdata入栈!n; void pop()if(top=root)cout已到栈底再也无法出栈!n;else top-;cout*toppush(135);p-push(246);p-pop();p-pop();p-pop();delete p;18 #includestruct workerschar *num;char *name;ift age;short key;unionchar *cadre;char *teacher;int worker;a6;void input()a0.num=2001001;=赵向;a0.age=18;a0.key=1;a0.cadre=县级;a1.num=2001002;=钱多;a1.age=18;a1.key=2;a1.teacher=教授;a2.num=2001003;=孙子;a2.age=18;a2.key=3;a2.worker=18;a3.num=2001004;=李四;a3.age=18;a3.key=1;a3.cadre=科级;a4.num=2001005;=周五;a4.age=18;a4.key=2;a4.teacher=讲师;a5.num=2001006;=武王;a5.age=18;a5.key=3;a5.worker=22;void output()cout编号 姓名 年龄 类别 级别 endl;for(int i=0;i6;i+)coutai.num ai.age ai.key ;switch(ai.key)case 1:coutai.cadreendl;break;case 2:coutai.teacherendl;break;case 3:coutai.workerendl;break;void main() input();output();19 #includestruct workerschar num6;char name12;char sex;short kind;unionchar cadre8;char teacher123short worker;a20;void input(int n)for(int i=0;in;i+)cout请输入第i+1条记录:endl;coutai.num;;coutai.sex;coutai.kind;switch(ai.kind) case 1:coutai.cadre;break;case 2:coutai.teacher;break;case 3:coutai.worker;break;void output(int n)for(int i=0;in;i+)coutai.num ai.sex ;switch(ai.kind) case 1:coutai.cadre;break;case 2:coutai.teacher;break;case 3:coutai.worker;break;coutendl;void count(int n)int c1=0,c2=0,c3=0;for(int i=0;in;i+)switch(ai.kind)case 1:c1+;break;case 2:c2+;break;case 3:c3+;break;cout干部:c1/教师:c2/工人:c3endl;void main() input(3);output(3);count(3);20 #includeclass ok2public:int a3 void fa()cout公有属性n;private:int b;void fb()cout私有属性n;protected:int c; void fc()cout保护属性n;void main()ok2 x;x.a=11;coutx.a;x.fa();x.b=22;coutx.b;x.fb();x.c=33;coutx.c;x.fc();/*公有属性的成员:在任何函数中都可以使用(出现)私有属性的成员:只有在成员函数和友元函数中使用保护属性的成员:除了同私有属性的成员之外,派生类(子类)中也可以使用*/21 #include/给类的数据成员赋初值#includeclass ok2 int a;double b;char *c; char *d;public:ok2(int aa=0,double bb=0.0,char *cc=$,char *dd=$) a=aa;b=bb;c=cc;d=new charstrlen(dd)+1;strcpy(d,dd); void disp()couta b c dendl; ;void main()ok2 x1(1,1.1,李四,计算机系);x1.disp();ok2 x2(2,2.2,李四);x2.disp();ok2 x3(3,3.3);x3.disp();ok2 x4(4);x4.disp();ok2 x5;x5.disp();/参数个数的变化及通过类的构造函数给类的数据成员赋初值的形式(初值不可省略)22 #include/类的继承class AAA private: int a;protected: int b;public: int c; void f()cout少壮不努力n;class BBB:public AAApublic: void g()coutabc;cout不可见 公-公 保护-保护 /父类以private方式派生出子类: 私-不可见 公-私 保护-私/父类以protected方式派生出子类:私-不可见 公-保护 保护-保护/保护属性:只能被本类的成员函数和友元函数以及派生类的成员函数和友元函数所使用/*class ccc:public BBBspublic:void ggg()coutabyendl;cout老大徒伤悲n;*/23 #include/在类的继承过程中构造函数的调用#includeclass AA int a;public:AA()coutAA好endl; AA()coutAA再见endl; ;class BB:public AA int b;public:BB()coutBB好endl; BB()coutBB再见endl;void main() BB x;/构造函数和释放函数是不能继承的24 #include/在类的继承过程中构造函数的调用#includeclass AAA public:char name20;int age; AAA(char *n,int x)strcpy(name,n);age=x; AAA() AAA() void list()coutname age;class BBB:public AAA int nn;public:BBB(char *n,int x,int x2):AAA(n,x)nn=x2; void disp()cout ;x.age=18;x.list();x.disp();/构造函数和释放函数是不能继承的25 #include/通过派生类给基类的数据成员赋初值#includeclass person char *name;int age;public:person(char *n,int x) name=new charstrlen(n)+1;strcpy(name,n)3age=x; person()delete name; void disp()coutname age ; ;class employee:public person char *department;double salary;public:employee(char *n,int x,char *p=,double y=0):person(n,x) department=new charstrlen(p)+1;strcpy(department,p);salary=y; employee()delete department; void disp()cout department salaryendl; ;void main()employee e1(李四,20,计算机系,888.88);e1.person:disp();e1.disp();/当一个派生类的对象中含有基类的数据成员的值时,必须定义一个构造函数,能够通过派生类的构造函数调用基类的构造函数26 #include/多继承(多基派生)#includeclass base public:int x0; base(int x0)x0=x0;cout是base!n; base()cout我是base!n; base();class A:virtual public base public:int a; A(int x1)a=x1;cout是A!n; A()cout我是A!n; A();class B:virtual public base public: int b; B(int x2)b=x2;cout是B!n; B()cout我是B!n;class AB:public B,public A int ab;public:AB(int x0,int x1,int x2,int x3):B(x2),A(x1),base()ab=x3; AB() ;void main()AB x(0,1,2,3);AB y;27 #include/继承问答一class AAA public: void f()coutAAAn; ;class BBB:public AAA public: void f()coutBBBn; ;class CCC:public BBB public: void f()cout不可见 公-公 保护-保护 /父类以private方式派生出子类: 私-不可见 公-私 保护-私/父类以protected方式派生出子类:私-不可见 公-保护 保护-保护/x.AAA:f();x.BBB:f();x.CCC:f();(可略)28 #include/继承问答二 虚基类class AAA public: void f()cout好好学习,天天向上!n; ;class BBB:virtual public AAA ;class CCC:virtual public BBB ;void main()CCC x;x.f();/在此,第二个virtual略不略一样29 #include/由对象为结点组成的链表#includeclass personchar name20;int age;person * next;public:person(char *nm,int n,person *er) strcpy(name,nm);age=n;next=er; void disp()cout|nameage|endl; static void disp(person *s) while(s)coutname/agenext;void main()person x3(王五,28,NULL);person x2(李四,20,&x3);person x1(张三,18,&x2);person *head=&x1;x2.disp();x3.disp(head);30 #include/局部成员屏蔽外部成员,静态绑定:在编译时就实现了绑定class Apublic:int abc;A()coutA好!n; ;class B:public Apublic:int abc;B():A()coutB好!n; ;class C:public Bpublic:int abc;C():B()coutC好!n; ;void main() C x;x.abc=11;coutx.abcendl;31 #include/动态聚束(绑定):在编译时还无法确定绑定对象,只有在运行时才能够实现绑定class basepublic: void f()cout1basen; ;class A:public basepublic: void f()cout2An; ;class B:public Apublic:void f()cout3Bn; ;class C:public Bpublic:virtual void f()cout4Cn; ;class D:public Cpublic:void f()coutf();p=&a;p-f();p=&b;p-f();p=&c;p-f();p=&d;p-f(); 32 #include/动态聚束的变化class base0public:virtual void v()coutbase好!n; ;class base1:public base0public:void v()coutbase1好!n; ;class A1:public base1public:void v()coutA1好!n; ;class A2:public A1public:void v()coutA2好!n; ;class B1:public base1public:void v()coutB1好!n; ;class B2:public B1public:void v()coutv();A2 a2;pb=&a2;pb-v();B1 b1;pb=&b1;pb-v();B2 b2;pb=&b2;pb-v(); */base1 *pb; A1 a1;pb=&a1;pb-v();A2 a2;pb=&a2;pb-v();B1 b1;pb=&b1;pb-v();B2 b2;pb=&b2;pb-v(); base1 x;pb=&x;pb-v();33 #include/动态聚束的变化class Apublic:virtual void v1()coutA中的v1n;a1(); void a1()coutA中的a1n;v2(); virtual void v2()coutA中的v2n; ;class B:public Apublic:void b1()coutB中的b1n;v2(); void v2()coutB中的v2n; void v1()coutB中的v1n;a1(); ;void main() A a;a.v1();B b;b.v1();34#include/动态聚束的变化class basepublic:base() virtual void v1()coutbase中的v1n; ;class A:public basepublic:A():base()coutA中的v1n;v1(); virtual void v1()coutA中的v1n; ;class B:public Apublic:B():A()coutA中的v1n;A:v1();void v1()coutB中的v1n; ;void main() A a;B b;35 #include/多基继承中的指针使用class basepubli

温馨提示

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

最新文档

评论

0/150

提交评论