您的位置:首页 > 编程语言 > C语言/C++

C++笔试题2(基础题)

2017-03-22 22:33 471 查看
温馨提醒:此文续《C++笔试题(基础题)

(112)请写出下列程序的输出内容

代码如下:

#include <iostream>
using namespace std;

class A
{
public:
A()
{
cout << "A::A()" << endl;
}
virtual ~A()
{
cout << "A::~A()" << endl;
}
void fun1() const
{
cout << "A::fun1()" << endl;
}
virtual void fun2() const
{
cout << "A::fun2()" << endl;
}
};

class B : public A
{
public:
B()
{
cout << "B::B()" << endl;
}
~B()
{
cout << "B::~B()" << endl;
}
void fun1() const
{
cout << "B::fun1()" << endl;
}
void fun2() const
{
cout << "B::fun2()" << endl;
}
};

void Test1(const A * pA)
{
pA->fun1();
pA->fun2();
delete pA;
}

void Test2(const B * pB)
{
pB->fun1();
pB->fun2();
delete pB;
}

void main()
{
cout << "=====Test1====" << endl;
Test1(new B());
cout << "====Test2====" << endl;
Test2(new B());
system("pause");
}

// run out:
/*
=====Test1====
A::A()
B::B()
A::fun1()
B::fun2()
B::~B()
A::~A()
====Test2====
A::A()
B::B()
B::fun1()
B::fun2()
B::~B()
A::~A()
请按任意键继续. . .
*/


输出内容如上注释。

至于结果为什么是这样的,请参见《C++笔试题(基础题)》中第(6)条及第(74)条。

或参见随笔《重载、覆盖、隐藏》理论点。

(113)请写出下列程序的输出内容

代码如下:

#include <iostream>
using namespace std;

void main()
{
for (int i = 10; --i >= 0; i = i>>1)
{
cout << (i + 1) << endl;
}

system("pause");
}

// run out:
/*
10
4
1
请按任意键继续. . .
*/


输出内容如上注释。

(114)请写出下列程序的输出内容

代码如下:

输出内容如上注释。

Good Good Study, Day Day Up.

顺序 选择 循环 总结
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: