《C++程序设计教程 第2版》教学素材PPT讲稿教学课件第12章 继承和派生例子.docx
《《C++程序设计教程 第2版》教学素材PPT讲稿教学课件第12章 继承和派生例子.docx》由会员分享,可在线阅读,更多相关《《C++程序设计教程 第2版》教学素材PPT讲稿教学课件第12章 继承和派生例子.docx(16页珍藏版)》请在冰豆网上搜索。
《C++程序设计教程第2版》教学素材PPT讲稿教学课件第12章继承和派生例子
例12.1先定义“点”类Point,
再由“点”类派生出“圆”类Circle。
#include
#definePI3.14159
classPoint//定义“点”类
{
intx,y;
public:
Point(inta=0,intb=0)
{x=a;y=b;}
voidShowPoint()
{cout<<"Point:
("<intGetx()
{returnx;}
intGety()
{returny;}
voidSetxy(inta,intb)
{x=a;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();//D
x1+=x_offset;y1+=y_offset;
Setxy(x1,y1);//E
}
voidShowCircle()
{
ShowPoint();//F
cout<<"Radius:
"<cout<<"Area:
"<
}
};
voidmain()
{
Circlec(1,1,1);
c.ShowCircle();
c.Move(1,2);
c.ShowCircle();
c.Setxy(4,5);//重新置圆心坐标
c.Setr
(2);//重新置半径值
c.ShowCircle();
}
回ppt讲稿
例12.2先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。
#include
#definePI3.14159
classPoint
{
protected:
//A
intx,y;
public:
Point(inta=0,intb=0){x=a;y=b;}
voidShowPoint()
{cout<<"Point:
("<intGetx(){returnx;}
intGety(){returny;}
voidSetxy(inta,intb){x=a;y=b;}
};
classRadius
{
protected:
//B
intr;
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()
{returnPI*r*r;}//直接访问基类的保护成员
voidMove(intx_offset,inty_offset)
{x+=x_offset;y+=y_offset;}
voidShowCircle()
{
ShowPoint();
cout<<"Radius:
"<cout<<"Area:
"<
}
};
voidmain()
{
Circlec(1,1,1);
c.ShowCircle();
c.Move(1,2);
c.ShowCircle();
c.Setxy(4,5);
c.Setr
(2);
c.ShowCircle();
}
程序的运行结果为:
Point:
(1,1)
Radius:
1Area:
3.14159
Point:
(2,3)
Radius:
1Area:
3.14159
Point:
(4,5)
Radius:
2Area:
12.5664
返回ppt讲稿
例12.3多重继承中基类构造函数和析构函数的调用顺序
#include
classBase1
{
protected:
intdata1;
public:
Base1(inta=0)
{data1=a;
cout<<"BaseConstructor1\n";
}
~Base1()
{cout<<"BaseDestructor1\n";}
};
classBase2
{
protected:
intdata2;
public:
Base2(inta=0)
{data2=a;
cout<<"BaseConstructor2\n";
}
~Base2()
{cout<<"BaseDestructor2\n";}
};
classDerived:
publicBase1,publicBase2//A
{
intd;
public:
Derived(intx,inty,intz):
Base1(x),Base2(y)//B
{d=z;cout<<"DerivedConstructor\n";}
~Derived()
{cout<<"DerivedDestructor\n";}
voidShow()
{cout<};
voidmain()
{Derivedc(1,2,3);
c.Show();
}
程序的运行结果是:
BaseConstructor1
BaseConstructor2
DerivedConstructor
1,2,3
DerivedDestructor
BaseDestructor2
BaseDestructor1
返回ppt讲稿
例12.4对象成员构造函数和析构函数的调用顺序
#include
classBase1
{
protected:
intdata1;
public:
Base1(inta=8)
{data1=a;
cout<}
~Base1()
{cout<};
classBase2
{
protected:
intdata2;
public:
Base2(inta=9)
{data2=a;
cout<}
~Base2()
{cout<};
classDerived:
publicBase1,publicBase2//A
{intd;
Base1b1,b2;//B
public:
Derived(intx,inty,intz):
Base1(x),Base2(y),b1(x+y),b2(x+z)//C
{d=z;cout<<"DerivedConstructor\n";}
~Derived()
{cout<<"DerivedDestructor\n";}
voidShow()
{cout<};
voidmain()
{Derivedc(1,2,3);
c.Show();
}
程序的运行结果是:
1,BaseConstructor1
2,BaseConstructor2
3,BaseConstructor1//构造对象成员b1时的输出
4,BaseConstructor1//构造对象成员b2时的输出
DerivedConstructor
1,2,3
DerivedDestructor
4,BaseDestructor1//析构对象成员b2时的输出
3,BaseDestructor1//析构对象成员b1时的输出
2,BaseDestructor2
1,BaseDestructor1
返回ppt讲稿
例12.7多层继承中的冲突,注意本例的继承关系如右下图所示:
#include
classA
{
protected:
intx;
public:
voidShow()
{cout<<"x="<};
classB
{
protected:
intx;
public:
voidShow()
{cout<<"x="<};
classC:
publicA,publicB//公有继承A、B类
{
inty;
public:
voidSetAx(inta){A:
:
x=a;}
voidSetBx(inta){B:
:
x=a;}
voidSety(intb){y=b;}
intGety(){returny;}
};
classD:
publicC//公有继承C类
{
intz;
public:
voidSetz(inta){z=a;}
intGetz(){returnz;}
};
voidmain(void)
{Dd;
d.SetAx(10);d.SetBx(20);d.Sety(30);d.Setz(40);
cout<<"A";
d.C:
:
A:
:
Show();//E报错
cout<<"B";
d.C:
:
B:
:
Show();//F报错
cout<<"y="<cout<<"z="<}
解决1:
在C类中增加成员函数:
voidShowA(){cout<<"x="<:
x<<'\n';}
voidShowB(){cout<<"x="<:
x<<'\n';}
再将E行和F行改写成:
d.ShowA();和d.ShowB();即可。
解决2:
把E行和F行改写成:
d.A:
:
Show();和d.B:
:
Show();即可。
返回ppt讲稿
例12.8支配规则示例
#include
classA
{
protected:
intx;
public:
voidSet(inta)
{x=a;}
voidShow()
{cout<<"x="<};
classB:
publicA
{
protected:
问题:
类B类对象有几个数据成员。
intx;
public:
voidSetAx(inta)
{A:
:
x=a;}//访问的是基类A的x
voidSetBx(inta)
{x=a;}//访问的是派生类B的x
voidShow()
{cout<<"x="<};
voidmain(void)
{
Bb;
b.SetAx
(1);
b.SetBx
(2);
b.A:
:
Show();//访问的是基类A的Show()
b.Show();//访问的是派生类B的Show()
}
返回ppt讲稿
例12.11虚基类和非虚基类构造函数的调用。
#include
classA
{
public:
A(){cout<<"A";}
~A(){cout<<"~A";}
};
classB
{
public:
B(){cout<<"B";}
~B(){cout<<"~B";}
};
classC
{
public:
C(){cout<<"C";}
~C(){cout<<"~C";}
};
classD
{
public:
D(){cout<<"D";}
~D(){cout<<"~D";}
};
classE:
virtualpublicB,publicA,publicD,virtualpublicC
{
};
intmain()
{
Ec;
return0;
}
程序的运行结果是:
BCAD~D~A~C~B
返回ppt讲稿
例12.12访问对象成员的成员
#include
#include
classPoint
{
intx,y;
public:
Point(inta=0,intb=0){x=a;y=b;}
voidSetx(inta){x=a;}
voidSety(inta){y=a;}
intGetx(){returnx;}
intGety(){returny;}
voidShow()
{cout<<"point("<};
classLine
{
Pointp1,p2;//对象成员
public:
Line(intx1,inty1,intx2,inty2):
p1(x1,y1),p2(x2,y2)
//调用对象成员构造函数
{}
doubleLength()
{intx1,y1,x2,y2;
x1=p1.Getx();y1=p1.Gety();
//访问对象成员p1的成员
x2=p2.Getx();y2=p2.Gety();
//访问对象成员p2的成员
returnsqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
voidShow()
{p1.Show();//访问对象成员p1的成员
p2.Show();//访问对象成员p2的成员
cout<<"Length="<}
};
voidmain()
{Lineline(0,0,1,1);
line.Show();
}
本程序的运行结果是:
point(0,0)
point(1,1)
Length=1.41421
注意在本程序中,Point类和Line类不是继承关系,只是Point类的两个对象,是Line类的对象成员,访问对象成员的成员,与访问一般对象的成员遵循同样的规则。
返回ppt讲稿
例12.13访问基类成员
#include
#include
classPoint
{
protected:
intx,y;//定义x、y为保护成员,以使在公有派生类中可直接访问它们
public:
Point(inta=0,intb=0){x=a;y=b;}
voidSetx(inta){x=a;}
voidSety(inta){y=a;}
intGetx(){returnx;}
intGety(){returny;}
voidShow()
{cout<<"point("<};
classLine:
publicPoint//公有继承
{
protected:
intx1,y1;
public:
Line(inta,intb,intc,intd):
Point(a,b)
//调用基类构造函数
{x1=c;y1=d;}
doubleLength()
{returnsqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));}
//A直接访问基类保护成员x、y
voidShow()
{
Point:
:
Show();//访问基类成员函数Show()
cout<<"point("<cout<<"Length="<}
};
voidmain()
{
Lineline(0,0,1,1);
line.Show();
}
返回ppt讲稿
例12.14赋值兼容规则
#include
classPoint
{
protected:
intx,y;//保护成员
public:
Point(inta=0,intb=0){x=a;y=b;}
voidShow()
{cout<<"point("<};
classLine:
publicPoint//公有继承
{
protected:
intx1,y1;
public:
Line(inta,intb,intc,intd):
Point(a,b)//调用基类构造函数
{x1=c;y1=d;}
};
voidmain()
{Lineline(2,2,6,6);
Pointp;
p=line;//A
p.Show();
}
程序运行结果是:
Point(2,2)
返回ppt讲稿