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

第七章


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

6.

#include<iostream>

#include<cstdio>


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.

#include <iostream>

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.



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

#include <iostream>

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;

}


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值