欢迎来到天天文库
浏览记录
ID:56721483
大小:44.50 KB
页数:21页
时间:2020-07-06
《《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子).doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、例12.1先定义“点”类Point,再由“点”类派生出“圆”类Circle。#include#definePI3.14159classPoint//定义“点”类{intx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<2、y=b;}};classCircle:publicPoint//定义“圆”类,公有继承{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+=3、x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCircle(){ShowPoint();//Fcout<<"Radius:"<4、);}回ppt讲稿例12.2先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、y(){returny;}voidSetxy(inta,intb){x=a;y=b;}};classRadius{protected://Bintr;public:Radius(intra=0){r=ra;}voidSetr(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea6、(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<7、,5);c.Setr(2);c.ShowCircle();}程序的运行结果为:Point:(1,1)Radius:1Area:3.14159Point:(2,3)Radius:1Area:3.14159Point:(4,5)Radius:2Area:12.5664返回ppt讲稿例12.3多重继承中基类构造函数和析构函数的调用顺序#includeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"8、BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived:publicBase1,publicBase2//A{intd;public:Derived
2、y=b;}};classCircle:publicPoint//定义“圆”类,公有继承{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+=
3、x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCircle(){ShowPoint();//Fcout<<"Radius:"<4、);}回ppt讲稿例12.2先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、y(){returny;}voidSetxy(inta,intb){x=a;y=b;}};classRadius{protected://Bintr;public:Radius(intra=0){r=ra;}voidSetr(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea6、(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<7、,5);c.Setr(2);c.ShowCircle();}程序的运行结果为:Point:(1,1)Radius:1Area:3.14159Point:(2,3)Radius:1Area:3.14159Point:(4,5)Radius:2Area:12.5664返回ppt讲稿例12.3多重继承中基类构造函数和析构函数的调用顺序#includeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"8、BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived:publicBase1,publicBase2//A{intd;public:Derived
4、);}回ppt讲稿例12.2先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<5、y(){returny;}voidSetxy(inta,intb){x=a;y=b;}};classRadius{protected://Bintr;public:Radius(intra=0){r=ra;}voidSetr(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea6、(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<7、,5);c.Setr(2);c.ShowCircle();}程序的运行结果为:Point:(1,1)Radius:1Area:3.14159Point:(2,3)Radius:1Area:3.14159Point:(4,5)Radius:2Area:12.5664返回ppt讲稿例12.3多重继承中基类构造函数和析构函数的调用顺序#includeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"8、BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived:publicBase1,publicBase2//A{intd;public:Derived
5、y(){returny;}voidSetxy(inta,intb){x=a;y=b;}};classRadius{protected://Bintr;public:Radius(intra=0){r=ra;}voidSetr(intra){r=ra;}intGetr(){returnr;}};classCircle:publicPoint,publicRadius{public:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea
6、(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<7、,5);c.Setr(2);c.ShowCircle();}程序的运行结果为:Point:(1,1)Radius:1Area:3.14159Point:(2,3)Radius:1Area:3.14159Point:(4,5)Radius:2Area:12.5664返回ppt讲稿例12.3多重继承中基类构造函数和析构函数的调用顺序#includeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"8、BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived:publicBase1,publicBase2//A{intd;public:Derived
7、,5);c.Setr(2);c.ShowCircle();}程序的运行结果为:Point:(1,1)Radius:1Area:3.14159Point:(2,3)Radius:1Area:3.14159Point:(4,5)Radius:2Area:12.5664返回ppt讲稿例12.3多重继承中基类构造函数和析构函数的调用顺序#includeclassBase1{protected:intdata1;public:Base1(inta=0){data1=a;cout<<"
8、BaseConstructor1";}~Base1(){cout<<"BaseDestructor1";}};classBase2{protected:intdata2;public:Base2(inta=0){data2=a;cout<<"BaseConstructor2";}~Base2(){cout<<"BaseDestructor2";}};classDerived:publicBase1,publicBase2//A{intd;public:Derived
此文档下载收益归作者所有