第 七 章 继承与派生
7-1 比较类的三种继承方式public公有继承、protected保护继承、private私有继承之间的差别。
解:
不同的继承方式,导致不同访问属性的基类成员在派生类中的访问属性也有所不同:
公有继承,使得基类public(公有)和protected(保护)成员的访问属性在派生类中不变,而基类private(私有)成员不可访问。
私有继承,使得基类public(公有)和protected(保护)成员都以private(私有)成员身份出现在派生类中,而基类private(私有)成员不可访问。
保护继承中,基类public(公有)和protected(保护)成员都以protected(保护)成员身份出现在派生类中,而基类private(私有)成员不可访问。
7-2 派生类构造函数执行的次序是怎样的?
解:
派生类构造函数执行的一般次序为:调用基类构造函数;调用成员对象的构造函数;派生类的构造函数体中的内容。
7-3 如果在派生类B已经重载了基类A的一个成员函数fn1(),没有重载成员函数fn2(),如何调用基类的成员函数fn1()、fn2()?
解:
调用方法为: A::fn1();
fn2();
7-4 什么叫做虚基类?有何作用?
解:
当某类的部分或全部直接基类是从另一个基类派生而来,这些直接基类中,从上一级基类继承来的成员就拥有相同的名称,派生类的对象的这些同名成员在内存中同时拥有多个拷贝,我们可以使用作用域分辨符来唯一标识并分别访问它们。我们也可以将直接基类的共同基类设置为虚基类,这时从不同的路径继承过来的该类成员在内存中只拥有一个拷贝,这样就解决了同名成员的唯一标识问题。
虚基类的声明是在派生类的定义过程,其语法格式为:
class 派生类名:virtual 继承方式 基类名
上述语句声明基类为派生类的虚基类,在多继承情况下,虚基类关键字的作用范围和继承方式关键字相同,只对紧跟其后的基类起作用。声明了虚基类之后,虚基类的成员在进一步派生过程中,和派生类一起维护一个内存数据拷贝。
7-5 定义一个Shape基类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。
解:
源程序:
#include <iostream.h>
class Shape
{
public:
Shape(){}
~Shape(){}
virtual float GetArea() { return -1; }
};
class Circle : public Shape
{
public:
Circle(float radius):itsRadius(radius){}
~Circle(){}
float GetArea() { return 3.14 * itsRadius * itsRadius; }
private:
float itsRadius;
};
class Rectangle : public Shape
{
public:
Rectangle(float len, float width): itsLength(len), itsWidth(width){};
~Rectangle(){};
virtual float GetArea() { return itsLength * itsWidth; }
virtual float GetLength() { return itsLength; }
virtual float GetWidth() { return itsWidth; }
private:
float itsWidth;
float itsLength;
};
class Square : public Rectangle
{
public:
Square(float len);
~Square(){}
};
Square::Square(float len):
Rectangle(len,len)
{
}
void main()
{
Shape * sp;
sp = new Circle(5);
cout << "The area of the Circle is " << sp->GetArea () << endl;
delete sp;
sp = new Rectangle(4,6);
cout << "The area of the Rectangle is " << sp->GetArea() << endl;
delete sp;
sp = new Square(5);
cout << "The area of the Square is " << sp->GetArea() << endl;
delete sp;
}
程序运行输出:
The area of the Circle is 78.5
The area of the Rectangle is 24
The area of the Square is 25
7-6 定义一个哺乳动物Mammal类,再由此派生出狗Dog类,定义一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。
解:
源程序:
#include <iostream.h>
enum myColor{ BLACK, WHITE };
class Mammal
{
public:
// constructors
Mammal();
~Mammal();
//accessors
int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
//Other methods
void Speak() const { cout << "Mammal sound!\n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
Dog();
~Dog();
myColor GetColor() const { return itsColor; }
void SetColor (myColor color) { itsColor = color; }
void WagTail() { cout << "Tail wagging...\n"; }
private:
myColor itsColor;
};
Mammal::Mammal():
itsAge(1),
itsWeight(5)
{
cout << "Mammal constructor...\n";
}
Mammal::~Mammal()
{
cout << "Mammal destructor...\n";
}
Dog::Dog(): itsColor (WHITE)
{
cout << "Dog constructor...\n";
}
Dog::~Dog()
{
cout << "Dog destructor...\n";
}
int main()
{
Dog Jack;
Jack.Speak();
Jack.WagTail();
cout << " Jack is " << Jack.GetAge() << " years old\n";
return 0;
}
程序运行输出:
Mammal constructor...
Dog constructor...
Mammal sound!
Tail wagging...
Fido is 1 years old
Dog destructor...
Mammal destructor...
7-7 定义一个基类,构造其派生类,在构造函数中输出提示信息,观察构造函数的执行情况。
解:
#include <iostream.h>
class BaseClass
{
public:
BaseClass();
};
BaseClass::BaseClass()
{
cout << "构造基类对象!" << endl;
}
class DerivedClass : public BaseClass
{
public:
DerivedClass();
};
DerivedClass::DerivedClass()
{
cout << "构造派生类对象!" << endl;
}
void main()
{
DerivedClass d;
}
程序运行输出:
构造基类对象!
构造派生类对象!
7-8 定义一个Document类,有name成员变量,从Document派生出Book类,增加PageCount变量。
解:
#include <iostream.h>
#include <string.h>
class Document
{
public:
Document(){};
Document( char *name );
char *Name; // Document name.
void PrintNameOf(); // Print name.
};
Document::Document( char *name )
{
Name = new char[ strlen( name ) + 1 ];
strcpy( Name, name );
};
void Document::PrintNameOf()
{
cout << Name << endl;
}
class Book : public Document
{
public:
Book( char *name, long pagecount );
void PrintNameOf();
private:
long PageCount;
};
Book::Book( char *name, long pagecount ):Document(name)
{
PageCount = pagecount;
}
void Book::PrintNameOf()
{
cout << "Name of book: ";
Document::PrintNameOf();
}
void main()
{
Document a("Document1");
Book b("Book1",100);
b.PrintNameOf();
}
程序运行输出:
Name of book: Book1
7-9 定义基类Base,有两个共有成员函数fn1()、fn2(),私有派生出Derived类,如果想在Derived类的对象中使用基类函数fn1(),应怎么办?
解:
class Base
{
public:
int fn1() const { return 1; }
int fn2() const { return 2; }
};
class Derived : private Base
{
public:
int fn1() { return Base::fn1();};
int fn2() { return Base::fn2();};
};
void main()
{
Derived a;
a.fn1();
}
7-10 定义object类,有weight属性及相应的操作函数,由此派生出box类,增加Height和width属性及相应的操作函数,声明一个box对象,观察构造函数与析构函数的调用顺序。
解:
#include <iostream.h>
class object
{
private:
int Weight;
public:
object()
{
cout << "构造object对象" << endl;
Weight = 0;
}
int GetWeight(){ return Weight;}
void SetWeight(int n){ Weight = n;}
~object() { cout << "析构object对象" << endl;}
};
class box : public object
{
private:
int Height,Width;
public:
box()
{
cout << "构造box对象" << endl;
Height = Width = 0;
}
int GetHeight(){ return Height;}
void SetHeight(int n){ Height = n;}
int GetWidth(){ return Width;}
void SetWidth(int n){ Width = n;}
~box() { cout << "析构box对象" << endl;}
};
void main()
{
box a;
}
程序运行输出:
构造object对象
构造box对象
析构box对象
析构object对象
7-11 定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用DerivedClass的对象以及BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。
解:
#include <iostream.h>
class BaseClass
{
public:
void fn1();
void fn2();
};
void BaseClass::fn1()
{
cout << "调用基类的函数fn1()" << endl;
}
void BaseClass::fn2()
{
cout << "调用基类的函数fn2()" << endl;
}
class DerivedClass : public BaseClass
{
public:
void fn1();
void fn2();
};
void DerivedClass::fn1()
{
cout << "调用派生类的函数fn1()" << endl;
}
void DerivedClass::fn2()
{
cout << "调用派生类的函数fn2()" << endl;
}
void main()
{
DerivedClass aDerivedClass;
DerivedClass *pDerivedClass = &aDerivedClass;
BaseClass *pBaseClass = &aDerivedClass;
aDerivedClass.fn1();
aDerivedClass.fn2();
pBaseClass->fn1();
pBaseClass->fn2();
pDerivedClass->fn1();
pDerivedClass->fn2();
}
程序运行输