试题与答案

有下列程序:#include<iosteram.h>using namespace

题型:单项选择题

题目:

有下列程序:
#include<iosteram.h>
using namespace std;
class Stack

public:
Stack(unsignedn=10):size(n)rep_=new int [size];top=O;
Stack(Stack&s):size(s.size)

rep_=new int[size];
for(int i=0;i<size;i++)
rep_[i]=s.rep_[i];
top=s.top;

~Stack()<delete[]rep_;
void push(int

A.rep_[top]=a;top++;
intpop()--top;return rep_[top];
bool isEmpty()const return top==0;
private:
int*rep_;
unsigned size,top;
;
int main()

Stack s1;
for(int i=1;i<5;i++)
s1.push(i);
Stack s2(s1);
for(int i=1;i<3;i++)
cout<<s2.pop()<<’,’;
s2.push(6);
s1.push(7);
while(!s2.isEmpty())
cout<<s2.pop()<<’,’;
return 0;

执行上面程序的输出结果是( )。A) 4,3,2,1,

B.4,3,6,7,2,1,

C.4,3,6,2,1,

D.1,2,3,4,

答案:

参考答案:C

解析: 此题综合考查了类与对象、循环语句、指针等知识点。在主函数main中,先定义了类Stack的对象s1,通过循环将1、2、3、4压入堆栈内;然后定义对象s2,并用对象s1来初始化,所以s2的大小也是10。第二个循环将4、3弹出并输出,然后将6压入s2的堆栈,然后将s2中剩下的值全部弹出,即6、2、1。

试题推荐
微信公众账号搜索答案