欢迎来到天天文库
浏览记录
ID:15384844
大小:59.50 KB
页数:19页
时间:2018-08-03
《c++ 第12章 继承和派生(例子)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、例12.1先定义“点”类Point,再由“点”类派生出“圆”类Circle。#include#definePI3.14159classPoint//定义“点”类{intx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<2、nt//定义“圆”类,公有继承{intr;//“圆”的半径public:Circle(intx,inty,intra):Point(x,y)//B{r=ra;}voidSetr(intra){r=ra;}doubleArea()//求圆的面积{returnPI*r*r;}voidMove(intx_offset,inty_offset)//将圆心坐标平移{intx1=Getx();//存取基类的私有成员inty1=Gety();//Dx1+=x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCircle(){3、ShowPoint();//Fcout<<"Radius:"<4、ream.h>#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<6、t';cout<<"Area:"<7、cludeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived8、:publicBase1,publicBase2//A{intd;public:Derived
2、nt//定义“圆”类,公有继承{intr;//“圆”的半径public:Circle(intx,inty,intra):Point(x,y)//B{r=ra;}voidSetr(intra){r=ra;}doubleArea()//求圆的面积{returnPI*r*r;}voidMove(intx_offset,inty_offset)//将圆心坐标平移{intx1=Getx();//存取基类的私有成员inty1=Gety();//Dx1+=x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCircle(){
3、ShowPoint();//Fcout<<"Radius:"<4、ream.h>#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<6、t';cout<<"Area:"<7、cludeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived8、:publicBase1,publicBase2//A{intd;public:Derived
4、ream.h>#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<6、t';cout<<"Area:"<7、cludeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived8、:publicBase1,publicBase2//A{intd;public:Derived
5、(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<6、t';cout<<"Area:"<7、cludeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived8、:publicBase1,publicBase2//A{intd;public:Derived
6、t';cout<<"Area:"<
7、cludeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived
8、:publicBase1,publicBase2//A{intd;public:Derived
此文档下载收益归作者所有