📄 chapter8.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-style: italic;
font-weight: bold;
color: #FF0000;
}
.style2 {
font-size: 14px;
font-weight: bold;
}
.style3 {font-size: 14px}
-->
</style>
</head>
<body>
<p class="style1">第 八 章 多态性</span><br>
</p>
<p><span class=style2>8-1 什么叫做多态性 ?在C++中是如何实现多态的?<br>
</span></p>
<p><span class=style3>解: <br>
多态是指同样的消息被不同类型的对象接收时导致完全不同的行为,是对类的特定成员函数的再抽象。C++支持的多态有多种类型,重载(包括函数重载和运算符重载)和虚函数是其中主要的方式。 </span></p>
<p class=style3> </p>
<p class=style2>8-2 什么叫做抽象类?抽象类有何作用?抽象类的派生类是否一定要给出纯虚函数的实现?</strong><br>
</p>
<p class=style3>解: <br>
带有纯虚函数的类是抽象类。抽象类的主要作用是通过它为一个类族建立一个公共的接口,使它们能够更有效地发挥多态特性。抽象类声明了一组派生类共同操作接口的通用语义,而接口的完整实现,即纯虚函数的函数体,要由派生类自己给出。但抽象类的派生类并非一定要给出纯虚函数的实现,如果派生类没有给出纯虚函数的实现,这个派生类仍然是一个抽象类。<br>
</p>
<p class=style2>8-3 声明一个参数为整型,无返回值,名为fn1的虚函数。</strong><br>
</p>
<p class=style3>解: <br>
virtual void fn1( int );<br>
</p>
<p class=style2>8-4 在C++中,能否声明虚构造函数?为什么?能否声明虚析构函数?有何用途?</strong><br>
</p>
<p class=style3>解: <br>
在C++中,不能声明虚构造函数,多态是不同的对象对同一消息有不同的行为特性,虚函数作为运行过程中多态的基础,主要是针对对象的,而构造函数是在对象产生之前运行的,因此虚构造函数是没有意义的;可以声明虚析构函数,析构函数的功能是在该类对象消亡之前进行一些必要的清理工作,如果一个类的析构函数是虚函数,那么,由它派生而来的所有子类的析构函数也是虚函数。析构函数设置为虚函数之后,在使用指针引用时可以动态联编,实现运行时的多态,保证使用基类的指针就能够调用适当的析构函数针对不同的对象进行清理工作。<br>
</p>
<p class=style2>8-5 实现重载函数Double(x),返回值为输入参数的两倍;参数分别为整型、长整型、浮点型、双精度型,返回值类型与参数一样。</strong><br>
</p>
<p class=style3>解: <br>
源程序:<br>
#include <iostream.h></p>
<p class=style3>int Double(int);<br>
long Double(long);<br>
float Double(float);<br>
double Double(double);</p>
<p class=style3>int main()<br>
{<br>
int myInt = 6500;<br>
long myLong = 65000;<br>
float myFloat = 6.5F;<br>
double myDouble = 6.5e20;</p>
<p class=style3>int doubledInt;<br>
long doubledLong;<br>
float doubledFloat;<br>
double doubledDouble;</p>
<p class=style3>cout << "myInt: " << myInt << "\n";<br>
cout << "myLong: " << myLong << "\n";<br>
cout << "myFloat: " << myFloat << "\n";<br>
cout << "myDouble: " << myDouble << "\n";</p>
<p class=style3>doubledInt = Double(myInt);<br>
doubledLong = Double(myLong);<br>
doubledFloat = Double(myFloat);<br>
doubledDouble = Double(myDouble);</p>
<p class=style3>cout << "doubledInt: " << doubledInt << "\n";<br>
cout << "doubledLong: " << doubledLong << "\n";<br>
cout << "doubledFloat: " << doubledFloat << "\n";<br>
cout << "doubledDouble: " << doubledDouble << "\n";</p>
<p class=style3>return 0;<br>
}</p>
<p class=style3>int Double(int original)<br>
{<br>
cout << "In Double(int)\n";<br>
return 2 * original;<br>
}</p>
<p class=style3>long Double(long original)<br>
{<br>
cout << "In Double(long)\n";<br>
return 2 * original;<br>
}</p>
<p class=style3>float Double(float original)<br>
{<br>
cout << "In Double(float)\n";<br>
return 2 * original;<br>
}</p>
<p class=style3>double Double(double original)<br>
{<br>
cout << "In Double(double)\n";<br>
return 2 * original;<br>
}</p>
<p class=style3>程序运行输出:<br>
myInt: 6500<br>
myLong: 65000<br>
myFloat: 6.5<br>
myDouble: 6.5e+20<br>
In Double(int)<br>
In Double(long)<br>
In Double(float)<br>
In Double(double)<br>
DoubledInt: 13000<br>
DoubledLong: 130000<br>
DoubledFloat: 13<br>
DoubledDouble: 1.3e+21<br>
</p>
<p class=style2>8-6 定义一个Rectangle类,有长itsWidth、宽itsLength等属性,重载其构造函数Rectangle()和Rectangle(int width, int length)。</strong><br>
</p>
<p class=style3>解: <br>
源程序:<br>
#include <iostream.h></p>
<p class=style3>class Rectangle<br>
{<br>
public:<br>
Rectangle();<br>
Rectangle(int width, int length);<br>
~Rectangle() {}<br>
int GetWidth() const { return itsWidth; }<br>
int GetLength() const { return itsLength; }<br>
private:<br>
int itsWidth;<br>
int itsLength;<br>
};</p>
<p class=style3>Rectangle::Rectangle()<br>
{<br>
itsWidth = 5;<br>
itsLength = 10;<br>
}</p>
<p class=style3>Rectangle::Rectangle (int width, int length)<br>
{<br>
itsWidth = width;<br>
itsLength = length;<br>
}</p>
<p class=style3>int main()<br>
{<br>
Rectangle Rect1;<br>
cout << "Rect1 width: " << Rect1.GetWidth() << endl;<br>
cout << "Rect1 length: " << Rect1.GetLength() << endl;</p>
<p class=style3>int aWidth, aLength;<br>
cout << "Enter a width: ";<br>
cin >> aWidth;<br>
cout << "\nEnter a length: ";<br>
cin >> aLength;</p>
<p class=style3>Rectangle Rect2(aWidth, aLength);<br>
cout << "\nRect2 width: " << Rect2.GetWidth() << endl;<br>
cout << "Rect2 length: " << Rect2.GetLength() << endl;<br>
return 0;<br>
}</p>
<p class=style3>程序运行输出:<br>
Rect1 width: 5<br>
Rect1 length: 10<br>
Enter a width: 20</p>
<p class=style3>Enter a length: 50</p>
<p class=style3>Rect2 width: 20<br>
Rect2 length: 50<br>
</p>
<p class=style2>8-7 定义计数器Counter类,对其重载运算符 + 。</strong><br>
</p>
<p class=style3>解: <br>
源程序:<br>
typedef unsigned short USHORT;<br>
#include <iostream.h></p>
<p class=style3>class Counter<br>
{<br>
public:<br>
Counter();<br>
Counter(USHORT initialValue);<br>
~Counter(){}<br>
USHORT GetItsVal()const { return itsVal; }<br>
void SetItsVal(USHORT x) {itsVal = x; }<br>
Counter operator+ (const Counter &);<br>
private:<br>
USHORT itsVal;<br>
};</p>
<p class=style3>Counter::Counter(USHORT initialValue):<br>
itsVal(initialValue)<br>
{<br>
}</p>
<p class=style3>Counter::Counter():<br>
itsVal(0)<br>
{<br>
}</p>
<p class=style3>Counter Counter::operator+ (const Counter & rhs)<br>
{<br>
return Counter(itsVal + rhs.GetItsVal());<br>
}</p>
<p class=style3>int main()<br>
{<br>
Counter varOne(2), varTwo(4), varThree;<br>
varThree = varOne + varTwo;<br>
cout << "varOne: " << varOne.GetItsVal()<< endl;<br>
cout << "varTwo: " << varTwo.GetItsVal() << endl;<br>
cout << "varThree: " << varThree.GetItsVal() << endl;</p>
<p class=style3>return 0;<br>
}</p>
<p class=style3>程序运行输出:<br>
varOne: 2<br>
varTwo: 4<br>
varThree: 6<br>
</p>
<p class=style2>8-8 定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。</strong><br>
</p>
<p class=style3>解: <br>
源程序:<br>
#include <iostream.h><br>
class Mammal<br>
{<br>
public:<br>
Mammal():itsAge(1) { cout << "Mammal constructor...\n"; }<br>
~Mammal() { cout << "Mammal destructor...\n"; }<br>
virtual void Speak() const { cout << "Mammal speak!\n"; }<br>
};</p>
<p class=style3>class Dog : public Mammal<br>
{<br>
public:<br>
Dog() { cout << "Dog Constructor...\n"; }<br>
~Dog() { cout << "Dog destructor...\n"; }<br>
void Speak()const { cout << "Woof!\n"; }<br>
};</p>
<p class=style3>int main()<br>
{<br>
Mammal *pDog = new Dog;<br>
pDog->Speak();</p>
<p class=style3>return 0;<br>
}<br>
程序运行输出:<br>
Mammal constructor...<br>
Dog constructor...<br>
Woof!<br>
Dog destructor...<br>
Mammal destructor...<br>
</p>
<p class=style2>8-9 定义一个Shape抽象类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。</strong><br>
</p>
<p class=style3>解: <br>
源程序:<br>
#include <iostream.h></p>
<p class=style3>class Shape<br>
{<br>
public:<br>
Shape(){}<br>
~Shape(){}<br>
virtual float GetArea() =0 ;<br>
virtual float GetPerim () =0 ;<br>
};</p>
<p class=style3>class Circle : public Shape<br>
{<br>
public:<br>
Circle(float radius):itsRadius(radius){}<br>
~Circle(){}<br>
float GetArea() { return 3.14 * itsRadius * itsRadius; }<br>
float GetPerim () { return 6.28 * itsRadius; }<br>
<br>
private:<br>
float itsRadius;<br>
};</p>
<p class=style3>class Rectangle : public Shape<br>
{<br>
public:<br>
Rectangle(float len, float width): itsLength(len), itsWidth(width){};<br>
~Rectangle(){};<br>
virtual float GetArea() { return itsLength * itsWidth; }<br>
float GetPerim () { return 2 * itsLength + 2 * itsWidth; }<br>
virtual float GetLength() { return itsLength; }<br>
virtual float GetWidth() { return itsWidth; }<br>
private:<br>
float itsWidth;<br>
float itsLength;<br>
};</p>
<p class=style3>void main()<br>
{<br>
Shape * sp;<br>
<br>
sp = new Circle(5);<br>
cout << "The area of the Circle is " << sp->GetArea () << endl;<br>
cout << "The perimeter of the Circle is " << sp->GetPerim () << endl;<br>
delete sp;<br>
sp = new Rectangle(4,6);<br>
cout << "The area of the Rectangle is " << sp->GetArea() << endl;<br>
cout << "The perimeter of the Rectangle is " << sp->GetPerim () << endl;<br>
delete sp;<br>
}</p>
<p class=style3>程序运行输出:<br>
The area of the Circle is 78.5<br>
The perimeter of the Circle is 31.4<br>
The area of the Rectangle is 24<br>
The perimeter of the Rectangle is 20<br>
</p>
<p class=style2>8-10 对Point类重载++(自增)、--(自减)运算符</strong><br>
</p>
<p class=style3>解: <br>
#include <iostream.h></p>
<p class=style3>class Point<br>
{<br>
public:</p>
<p class=style3>Point& operator++();<br>
Point operator++(int);<br>
<br>
Point& operator--();<br>
Point operator--(int);<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -