usingnamespacestd;
classB
{
public:
virtual voiddisplay( ){cout<〈 "B:
:
display()"<〈endl;}
};
classC:
public B
{
public:
voiddisplay(){cout<<”C:
:
display()"〈〈endl;}
};
voidfun(B*p)
{
p—>display();
}
voidmain()
{
Bb。
*pb;
C c;
pb=&b;
fun(pb);
pb=&c;
fun(pb);
}
49。
下面程序得运行结果如下:
Thisisline1
Thisisline2
Thisisline3
在下划线处填上缺少得部分。
源程序如下:
#include〈iostream>
#include〈fstream或”fstream”
usingnamespacestd;
voidmain()
{
fstream fin,fout;
fout。
open("my、txt"、ios:
:
out);
if(!
fout、is_open())
return;
for(inti=0;i〈3;i=i+1)
fout<〈"Thisis line"〈
fout、close();
fin.open("my。
txt"。
ios:
:
in);
if(!
fin。
is_open())
return;
charstr[100];
while(!
fin。
eof())
{
fin。
getline(str。
100);
cout<<str<<endl;
}
fin、close();
}
50、在下划线处填上缺少得部分。
源程序如下:
#include<iostream>
usingnamespacestd;
template<class T>或template〈typename T〉
Tfun(T x)
{
Ty;
y=x*x-T(5);
return y;
}
voidmain()
{
float a=2;
cout<<fun(a);
}
五、程序分析题(本大题共4小题。
每小题5分。
共20分)
51、请写出myTextl.txt文本文件中得内容
#include<iostream>
#include〈string>
usingnamespacestd;
#include<fstream>
voidmain()
{
ofstream myFile1;
my(”myTextl.txt");
cout〈〈”Enterthe datainChineseformat(e.g。
、2008。
May25):
"<<endl;
string Date(”2008。
January1”);
stringYear=Date、substr(0.4);
intk=Date、find(”、");
inti=Date.find(””);
stringMonth=Date、substr(k+l、i—k-1);
string Day=Date.substr(i+1。
2);
stringNewDate=Day+” "+Month+" "+Year;
myFilel<<"originaldate:
”〈〈Date〈myFile1〈<"Converted date:
”<〈NewDate<〈endl;
my();
}
original date:
2008,January 1
Converted date:
1January2008
52.给出下面程序得输出结果
#include<iostream〉
using namespacestd;
classSimple
{
intx.y;
public:
Simple(){x=y=0;}
Simple(inti.intj){x=i;y=j;}
voidcopy(Simple&s);
void setxy(int i、intj){x=i;y=j;}
voidprint(){cout〈〈"x="<<x<〈"、y="〈<y<〈endl;}
};
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);
}
voidmain()
{
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”iostream.h”
intmain()
{
inti=17;
while(i>=10)
if(--i%4==3)continue;
else
cout〈<"i=”<
}
i=16 i=14 i=12 i=10
54.给出下面程序得输出结果
#include<iostream〉
using namespace std;
void main()
{
intnum=300;
int&ref=num;
cout<<ref;
ref=ref-100;
cout<<" "〈num=num-50;
cout〈<”"<<ref<〈endl;
}
300200150
六、程序设计题(本大题共1小题、共10分)
55、定义堆栈类模板Stack(先进后出)、栈得大小由使用者确定。
要求该类模板对外提供
如下二种基本操作:
(1)push入栈
(2)pop出栈。
用数组来实现
#include<iostream>
usingnamespacestd;
templateclass Stack{
Tx[size];
intcurrent;
public:
Stack(){current=0;}
。
、。
、push(、。
、.);
、。
、.pop(、、、、);
};
请写出两个函数得过程(如果需要形式参数。
请给出形参类型与数量、以及返回值类型)
voidSatck〈T,size>:
:
push(T t){
if(current==size){
cout<<“The Stack isfull!
"<<endl;exit(0);}//堆栈满则程序结束
else{x[current]=t;current++;}//新元素入栈并修改栈顶指针
}
TStack:
pop(){
if(current= =0){
cout<<”Thereisnoobjectin the Stack!
”<else{Ta=x[current—1];current--;returna;} //栈顶元素出栈并修改栈顶指针
}