首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include <iostream.h> using namespace std; class Stack { public: Stack(unsigned n=10):size(n){rep_=ne
有如下程序: #include <iostream.h> using namespace std; class Stack { public: Stack(unsigned n=10):size(n){rep_=ne
admin
2010-12-16
57
问题
有如下程序: #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++) rep_
=s.rep_
; top=s.top; } ~Stack(){delete[]rep_;} void push(int a){rep_[top]=a; top++;} int pop(){--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++) sl.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。
转载请注明原文地址:https://kaotiyun.com/show/RsVp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从高位到低位的顺序组成一个新的数,并作为函数值返回。例如,从主函数输入一个整数:27638496,函数返回值为:26846。请在程序的下划线处填入正确的内容并把下
若下列选项中的各变量均为整型且已有值,其中不正确的赋值语句是()。
以下程序:#includemain(){charstr[10];scanff"%s",&str);printf("%s\n".str);}运行上面的程序,输入字符串howare
有以下程序段:inti,n;for(i=0;i<8;i++){n=rand()%5;switch(n){case1:case3:printf("%d\n",n);break;case2:case4:printf("%d\n");conti
设变量均已正确定义,若要通过scanf(“%d%c%d%c”,&a1,&c1,&a2,&c2);语句为变量a1和a2赋数值10和20,为变量c1和c2赋字符X和Y。以下所示的输入形式中正确的是()。(注:口代表空格字符)
有以下程序:#include<stdio.h>main(){inti,t[][3]={9,8,7,6,5,4,3,2,1};for(i=0;i<3;i++)printf("%d",t[2-i][i]);}程序执行后的输出结果是()。
以下叙述正确的是()。
若在定义语句:inta,b,c,*p=&c;之后,接着执行以下选项中的语句,则能正确执行的语句是()。
以下不构成无限循环的语句或语句组是()。
有以下程序,其中%u表示按无符号整数输出()。main(){unsignedintx=0xFFFF;/*x的初值为十六进制数*/printf("%u\n",x);}程序运行后的输出结果是(
随机试题
简述在Windows中将文件设置为“隐藏”属性的步骤。
在下列甲状腺癌中,发病率最高的是()
甲公司因票据遗失向法院申请公示催告,下列说法正确的是:()
1年以下的农户贷款不得采用到期利随本清方式。()
投资项目的经营成本不应包括运营期间固定资产折旧费、无形资产摊销费和财务费用。( )
关于口腔Kaposi肉瘤的特点,哪项是错误的()。
Tile hardware are the parts of computer itself including the(71)(CPU)and related microchips and micro -circuitry, keyboards, mon
WhatWeTakefromAndGivetotheSeaAslongaswehavebeenonearth,wehaveusedtheseaaroundus.Wetakefromtheocean,
Dynamicpicturesofthebraincouldshow______.
Shouldweonlyrewardthebeststudents?Thishasbeenintenselydiscussedforyears.Thefollowingarethesupporters’andoppo
最新回复
(
0
)