OOP习题(7)

一、判断题

1.In C++, inheritance allows a derived class to directly access all of the functions and data of its base class.

T        F

解析:F。继承分为对于不同“等级”的成员的继承,诸如private等。所以在不同的继承等级面前,不一定能访问类中的所有的变量,因此为错误。

2.One class can have more than one super classes.

T        F

解析:T。C++中允许一个类有多个父类。

二、填空题

1.write the output of the code below.

(1)the output at //1 is _______

(2)the output at //2 is _______

(3)the output at //3 is _______

(4)the output at //4 is _______

(5)the output at //5 is _______

#include <iostream>
#include <string>
using namespace std ;

class Testing
{
private:
    string words; 
    int number ;
public:
    Testing(const string & s = "Testing")
    {
        words = s ;
        number = words.length();
        if (words.compare("Testing")==0)
            cout << 1;
        else if (words.compare("Heap1")==0)
            cout << 2;
        else
            cout << 3;
    }
    ~Testing()
    {
        cout << 0;
    }
    void show() const
    {
        cout << number;
    }
};
int main()
{
    Testing *pc1 , *pc2;
    pc1 = new Testing ;          //1
    pc2 = new Testing("Heap1");  //2
    pc1->show();   //3
    delete pc1 ;   //4
    delete pc2 ;   //5
    return 0;
}

解析:1、2、7、0、0.对于代码加以分析,我们可以按照main函数的过程来进行“运行”:创建了pc1和pc2两个对象,而由于其构造函数是有缺省值的,在没有给值时words默认为Testing,于是pc1的words为Testing,在构造函数中输出为“1”,pc2的words为Heap1,在构造函数中输出为“2”.而对于pc1执行show函数&#x

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博学者普克尔特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值