有如下程序: #include <iostream.h> using namespace std; class Stack { public: Stack (unsigned n=10):size (n) {re

admin2013-02-27  16

问题 有如下程序:    #include  <iostream.h>    using namespace std;    class  Stack    {    public:      Stack (unsigned n=10):size (n) {rep_=new int [size]; top=0;}      Stack (stack&s}:  size  (s.size)      {        rep_new int[size];        for (int i=0;i<size;i++          rip_-s.rep_;        top=s.top;      }      ~Stack() {delete[]rep_;}      void poush (int a) {rep_[topj=a;  top++;}      int pep() {  --top;  return rep_[top];}      bool  isEmpty()  cons5  [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(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

解析 本题是一个综合应用考题,考核知识点包括类与对象的应用(包括构造函数、拷贝构造函数),循环语句的使用、指针的使用。 分析程序:类Stack的构造函数中默认参数为 10,即构造大小为10的堆栈,成员函数push用于将数据压入堆栈中,pop用于将数据弹出堆栈。主函数main中,先定义了类Stack的对象引,初始大小为10,通过for循环先后将值1,2,3、4压入堆栈内,然后定义对象s2,并用对象s1来初始化,所以对象s2的堆栈大小也为10,同时也已压入数值1、 2、3、4,第二个for循环将4、3弹出并输出,然后将数值6压入s2的堆栈,最后将s2堆栈中所剩下的值全部弹出,即6、2、1。
转载请注明原文地址:https://kaotiyun.com/show/NeVp777K
0

最新回复(0)