C语言程序设计(郑莉)课后习题答案 联系客服

发布时间 : 星期二 文章C语言程序设计(郑莉)课后习题答案更新完毕开始阅读826a4a9476c66137ef061931

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 << \<< endl; delete sp;

sp = new Rectangle(4,6);

cout << \<< endl; delete sp;

sp = new Square(5);

cout << \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

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 << \

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 << \

private:

myColor itsColor; };

Mammal::Mammal(): itsAge(1), itsWeight(5) {

cout << \}

Mammal::~Mammal() {

cout << \}

33

Dog::Dog(): itsColor (WHITE) {

cout << \}

Dog::~Dog() {

cout << \}

int main() {

Dog Jack; Jack.Speak(); Jack.WagTail();

cout << \

return 0; }

程序运行输出: Mammal constructor... Dog constructor... Mammal sound! Tail wagging... Fido is 1 years old Dog destructor... Mammal destructor...

7-7 定义一个基类,构造其派生类,在构造函数中输出提示信息,观察构造函数的执行情况。 解:

#include

class BaseClass { public: BaseClass(); };

BaseClass::BaseClass() {

cout << \构造基类对象!\ }

class DerivedClass : public BaseClass { public:

DerivedClass(); };

DerivedClass::DerivedClass() {

cout << \构造派生类对象!\}

void main() {

DerivedClass d; }

程序运行输出: 构造基类对象! 构造派生类对象!

7-8 定义一个Document类,有name成员变量,从Document派生出Book类,增加PageCount变量。 解:

#include #include

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;

34

}

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 << \Document::PrintNameOf(); }

void main() {

Document a(\Book b(\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

class object { private: int Weight; public: object() {

cout << \构造object对象\Weight = 0; }

int GetWeight(){ return Weight;} void SetWeight(int n){ Weight = n;}

~object() { cout << \析构object对象\};

class box : public object { private:

int Height,Width; public: box() {

cout << \构造box对象\Height = Width = 0; }

int GetHeight(){ return Height;} void SetHeight(int n){ Height = n;} int GetWidth(){ return Width;} void SetWidth(int n){ Width = n;}

35

~box() { cout << \析构box对象\};

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

class BaseClass { public: void fn1(); void fn2(); };

void BaseClass::fn1() {

cout << \调用基类的函数fn1()\}

void BaseClass::fn2() {

cout << \调用基类的函数fn2()\}

class DerivedClass : public BaseClass { public: void fn1(); void fn2(); };

void DerivedClass::fn1() {

cout << \调用派生类的函数fn1()\}

void DerivedClass::fn2() {

cout << \调用派生类的函数fn2()\}

void main() {

DerivedClass aDerivedClass;

DerivedClass *pDerivedClass = &aDerivedClass; BaseClass *pBaseClass = &aDerivedClass;

aDerivedClass.fn1(); aDerivedClass.fn2(); pBaseClass->fn1(); pBaseClass->fn2(); pDerivedClass->fn1(); pDerivedClass->fn2(); }

程序运行输出: 调用派生类的函数fn1() 调用派生类的函数fn2() 调用基类的函数fn1() 调用基类的函数fn2() 调用派生类的函数fn1() 调用派生类的函数fn2()

7-12 为例9-1的吹泡泡程序加一版权(About)对话框。然后修改例9-1的程序,加入以下内容: 程 序:

1.在程序首部加上文件包含命令 #include “resource.h”

2.在框架窗口类之前加入从CDialog类派生的对话框类:// 对话框类

class CAboutDlg: public CDialog { public: CAboutDlg();

enum {IDD = IDD_DIALOG1}; }; inline

36