您好,欢迎访问三七文档
c++继承经典例子#includeiostream.hclassBase{private:intb_number;public:Base(){}Base(inti):b_number(i){}intget_number(){returnb_number;}voidprint(){coutb_numberendl;}};classDerived:publicBase{private:intd_number;public://constructor,initializerusedtoinitializethebasepartofaDerivedobject.Derived(inti,intj):Base(i),d_number(j){};//anewmemberfunctionthatoverridestheprint()functioninBasevoidprint(){coutget_number();//accessnumberthroughget_number()coutd_numberendl;}};intmain(){Basea(2);Derivedb(3,4);coutais;a.print();//print()inBasecoutbis;b.print();//print()inDerivedcoutbasepartofbis;b.Base::print();//print()inBasereturn0;sdb新手上路UID1050精华0积分0帖子25阅读权限10注册2007-4-2状态离线#2使用道具发表于2007-7-1114:16资料个人空间短消息加为好友没有虚析构函数,继承类没有析构//Example:non-virtualdestructorsfordynamicallyallocatedobjects.#includeiostream.h#includestring.hclassThing{public:virtualvoidwhat_Am_I(){coutIamaThing.\n;}~Thing(){coutThingdestructorendl;}};classAnimal:publicThing{public:virtualvoidwhat_Am_I(){coutIamanAnimal.\n;}~Animal(){coutAnimaldestructorendl;}};voidmain(){Thing*t=newThing;Animal*x=newAnimal;Thing*array[2];array[0]=t;//basepointerarray[1]=x;for(inti=0;i2;i++)array-what_Am_I();deletearray[0];deletearray[1];return;}sdb新手上路UID1050精华0积分0帖子25阅读权限10注册2007-4-2状态离线#3使用道具发表于2007-7-1114:20资料个人空间短消息加为好友纯虚函数,多态#includeiostream.h#includemath.hclassPoint{private:doublex;doubley;public:Point(doublei,doublej):x(i),y(j){}voidprint()const{cout(x,y);}};classFigure{private:Pointcenter;public:Figure(doublei=0,doublej=0):center(i,j){}Point&location(){returncenter;}//returnanlvaluevoidmove(Pointp){center=p;draw();}virtualvoiddraw()=0;//drawthefigurevirtualvoidrotate(double)=0;//rotatethefigurebyanangle};classCircle:publicFigure{private:doubleradius;public:Circle(doublei=0,doublej=0,doubler=0):Figure(i,j),radius(r){}voiddraw(){coutAcirclewithcenter;location().print();coutandradiusradiusendl;}voidrotate(double){coutnoeffect.\n;}//mustbedefined};classSquare:publicFigure{private:doubleside;//lengthofthesidedoubleangle;//theanglebetweenasideandthex-axispublic:Square(doublei=0,doublej=0,doubled=0,doublea=0):Figure(i,j),side(d),angle(a){}voiddraw(){coutAsquarewithcenter;location().print();coutsidelengthside.\nTheanglebetweenonesideandtheX-axisisangleendl;}voidrotate(doublea){angle+=a;coutTheanglebetweenonesideandtheX-axisisangleendl;}voidvertices(){coutTheverticesofthesquareare:\n;//calculatecoordinatesoftheverticesofthesquare}};intmain(){Circlec(1,2,3);Squares(4,5,6);Figure*f=&c,&g=s;f-draw();f-move(Point(2,2));g.draw();g.rotate(1);s.vertices();//Cannotusegheresincevertices()isnotamemberofFigure.return0;}////////////////////////////////////////////////////////////////////#includeiostream.h#includestring.hclassThing{public:virtualvoidwhat_Am_I(){coutIamaThing.\n;}~Thing(){coutThingdestructorendl;}};classAnimal:publicThing{public:virtualvoidwhat_Am_I(){coutIamanAnimal.\n;}~Animal(){coutAnimaldestructorendl;}};voidmain(){Thingt;Animalx;Thing*array[2];array[0]=&t;//basepointerarray[1]=&x;for(inti=0;i2;i++)array-what_Am_I();return;}[本帖最后由sdb于2007-7-1114:41编辑]sdb新手上路UID1050精华0积分0帖子25阅读权限10注册2007-4-2状态离线#4使用道具发表于2007-7-1114:21资料个人空间短消息加为好友多继承#includeiostream.hclassA{private:inta;public:A(inti):a(i){}virtualvoidprint(){coutaendl;}intget_a(){returna;}};classB{private:intb;public:B(intj):b(j){}voidprint(){coutbendl;}intget_b(){returnb;}};classC:publicA,publicB{intc;public:C(inti,intj,intk):A(i),B(j),c(k){}voidprint(){A::print();B::print();}//useprint()withscoperesolutionvoidget_ab(){coutget_a()get_b()endl;}//useget_a()andget_b()withoutscoperesolution};intmain(){Cx(5,8,10);A*ap=&x;B*bp=&x;ap-print();//useC::print();bp-print();//useB::print();//bp-A::print();//asifxisinheritedfromBonly,//cannotaccessA::print();x.A::print();//useA::print();x.get_ab();return0;}sdb新手上路UID1050精华0积分0帖子25阅读权限10注册2007-4-2状态离线#5使用道具发表于2007-7-1114:23资料个人空间短消息加为好友共同基类的多继承#includeiostream.hclassR{intr;public:R(intanInt){r=anInt;};printOn(){coutr=rendl;};};classA:publicR{inta;public:A(intint1,intint2):R(int2){a=int1;};};classB:publicR{intb;public:B(intint1,intint2):R(int2){b=int1;};};classC:publicA,publicB{intc;public:C(intint1,intint2,intint3):A(int2,int3),B(int2,int3){c=int1;}};intmain(){inti;Rrr(10);Aaa(20,30);Bbb(40,50);Ccc(5,7,9);rr.printOn();aa.printOn();//inheritsRprintOnbb.printOn();//inheritsRprintOn//cc.printOn();//wouldgiveerrorreturn0;}sdb新手上路UID1050精华0积分0帖子25阅读权限10注册2007-4-2状态离线#6使用道具发表于2007-7-1114:25资料个人空间短消息加为好友虚基类#includeiostream.hclassR{intr;public:R(intx=0):r(x){}//constructorinRvoidf(){coutr=rendl;}voidprintOn(){coutprintOnR=rendl;}};classA:publicvirtualR{inta;public:A(intx,inty):R(x),a(y){}//constructorinAvoidf(){couta=aendl;R::f();}};classB:publicvirtualR{intb;public:B(intx,intz):R(x),b(z){}//constructorinBvoidf(){coutb=bendl;R::f();}};classC:publicA,publicB{intc;public://constructorinC,whichconstructsanRobjectfirstC(intx,inty,intz,intw):R(x)
本文标题:c++继承经典例子
链接地址:https://www.777doc.com/doc-5368394 .html