C++语言程序设计(第四版 郑莉) 第七章部分题解

第七章


第五题感觉题目有点模糊,不懂究竟要干什么

6.

C++语言程序设计(第四版 郑莉) 第七章部分题解_第1张图片

#include

#include


using namespace std;


class Mamal

{

public:


Mamal(){cout << "Calling Mamal construction" << endl;}

~Mamal(){cout << "Destructing Mamal" << endl;}


};



class Dog:public Mamal

{

public:


Dog(){cout << "Calling Dog construction" << endl;}

~Dog(){cout << "Destructing Dog" << endl;}


};

int main()


{

Dog d;


return 0;



}


10. C++语言程序设计(第四版 郑莉) 第七章部分题解_第2张图片

#include

using namespace std;

class object

{

private:

int Weight;

public:

object()

{

cout << "Constructing object" << endl;

Weight = 0;

}

int GetWeight(){ return Weight;}

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

~object() { cout << "Destructing object" << endl;}

};


class box : public object

{

private:

int Height,Width;

public:

box()

{

cout << "Constructing 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 << "Destructing box" << endl;}

};


int main()

{

box a;

return 0;

}



11.

C++语言程序设计(第四版 郑莉) 第七章部分题解_第3张图片



//这道题我不明白指针的指向,

#include

using namespace std;

class BaseClass

{

public:

void fn1();

void fn2();

};

void BaseClass::fn1()

{

cout << "Calling the base class fn1()" << endl;

}

void BaseClass::fn2()

{

cout << "Calling the base class fn2()" << endl;

}


class DerivedClass : public BaseClass

{

public:

void fn1();

void fn2();

};

void DerivedClass::fn1()

{

cout << "Calling the derived class fn1()" << endl;

}

void DerivedClass::fn2()

{

cout << "Calling the derived class fn2()" << endl;

}

int main()

{

DerivedClass aDerivedClass;

DerivedClass *pDerivedClass = &aDerivedClass;

BaseClass *pBaseClass = &aDerivedClass;


aDerivedClass.fn1();

aDerivedClass.fn2();

pBaseClass->fn1();

pBaseClass->fn2();

pDerivedClass->fn1();

pDerivedClass->fn2();

return 0;

}


你可能感兴趣的:(C++语言程序设计(第四版,郑莉)部分题解)