C++程序设计试题和答案1 联系客服

发布时间 : 星期四 文章C++程序设计试题和答案1更新完毕开始阅读

void Simple::copy(Simple&s) {

x=s.x;y=s.y; }

void func(Simple s1.Simple&s2) {

s1.setxy(30,40); s2.setxy(70.80); }

void main() {

Simple obj1(1.2).obj2; obj2.copy(obj1); func(obj1.obj2); obj1.print(); obj2.print(); } x=1,y=2 x=70,y=80

53.给出下面程序的输出结果 #include\

. .

int main() {

int i=17; while(i>=10)

if(--i%4==3)continue; else

cout<<\; }

i=16 i=14 i=12 i=10 54.给出下面程序的输出结果 #include using namespace std; void main() {

int num=300; int &ref=num; cout<

cout<<\;

. .

}

300 200 150

六、程序设计题(本大题共1小题.共10分)

55.定义堆栈类模板Stack(先进后出).栈的大小由使用者确定。要求该类模板对外提供 如下二种基本操作:

(1)push入栈(2)pop出栈.用数组来实现 #include using namespace std; template class Stack{ T x[size]; int current; public:

Stack(){current=0;} ....push(....); ....pop(....); };

请写出两个函数的过程(如果需要形式参数.请给出形参类型和数量.以及返回值类型) void Satck::push(T t){ if (current= =size) {

cout<<“The Stack is full!”<

. .

else {x[current]=t;current++;} //新元素入栈并修改栈顶指针 }

T Stack::pop( ){ if (current= =0){

cout<<”There is no object in the Stack!”<

. .