上述程序段运行结果为         。10.设x为整型变量,表达数学关系式:13 < x < 19的C++语言表达式是      。     11.设有代码“static int a[3][4]={{1,2}, {3}, {4,5,6}};”则a[1][1]值为      。 12.若有定义:char  u;  int  b;  float  v;  double  d;
则表达式u * b + d - v的值的类型为               。
13.假定所有变量均已正确定义,下列程序段运行后x的值是       。     a = b = c = 0; x = 35;
if(!a)x--;else if(b);if(c)x = 3;else x = 4;   14.类的构造函数的主要作用是           。 15.C++将类继承分为        和         两种。
16.建立类对象时,为节省内存,系统一般只分配用于   _内存。
17.派生类可以定义其_ _____中不具备的数据和操作。
18.operator是C++的一个关键字,它经常和C++的一个运算符连用,构成一个运算符函数名。运算符函数的返回类型不能是       类型。 19.在继承机制下,当对象消亡时,编译系统先执行________的析构函数,然后再执行_______的析构函数。
20.C++文件的两种类型,分别是           和                  。
21.在保护继承关系下,基类有公有成员和保护成员将成为派生类中的            成员,它们只能由派生类的               来访问。
三.写出下列程序运行结果(每题4分,共24分)
1. #includevoid main( ) {int I;
for(I=1;I<=5;I++)   switch(I%2)
{case 0:I++; cout<<“#”;break;       case 1:I+=2; cout<<“*”;  default: cout<<“\\n”;}}
2.  #include void fun( ); int n=5;
void main(  ) { int n=10;
cout<void fun( )      {     cout<3. #include int b=5;
int  fun(int n); void main(  ) {   int i;
for(i=0;i<3;i++)
{ cout<int  fun(int n){     static   a=1;                    int c=10;                     a=a+100;       b=b+100;       c=c+100;
return a+b+c; }
4.  #include int (int m,int n)
{   if(m%n==0)   return  n;
else return  (n,m%n);
}
void main(  )
{       int x=40,y=116;                                cout<<(y,x)<}                                         5. #include #include  class Point{        int x,y;    public:Point(){x=1;y=1;}      Point(int tx,int ty)    {x=tx;y=ty;}
~Point(){cout<<\"Point \"<void main() { Point obj1;Point obj2(10,20); } 6. #include class count{   static int n; public:    count()   {n++;}   void show()
{cout<{cout<int count::n=0; void main() {  count  a;     a.show();    {count b[4];     b[3].show();    }a.show(); }
四.改正错误(每题有二处错误,在错误语句或表达式画线,并写出正确语句。)(每题 4
分,共16分)
1.下列程序求字符串长度。
#includeint fun(char s)                 { int i=0;
while(s[i]!='\\n')         {i++;} return i; }
void main()
{char s[80];
cin.getline(s,80);    //读一行字符
cout<<\"length:\"<2.下列程序功能是求x的y次方(缺省是x的2次方)   #includedouble fun(double x,double y)     {int i;
double s=1;
for(i=1;ireturn  s; }void main()
{ double x=2.5,y=3;
cout<<\"pow(2.5,2):\"<3.#includeclass Point {private:
int x,y;
void init(int a,int b)       { x=a;y=b;} void show()
{ cout<<\"x=  \"<}; void main(){ Point  a(24,50);
a.show();
}
4.#include class Amplifier{
float invol,outvol; public:
Amplifier(float vin,float vout)      {invol=vin;outvol=vout;}       float gain();
};
Amplifier::float gain()
{ return outvol/invol;} void main()
{  Amplifier amp(5.0,10.0);
cout<<\"\\n\\nThe gain is =>\"<五.程序填空(每空2分,共10分)1.有一个一维数组,存放10个职工的年龄,别写两个函数求职工的最大年龄和最小年龄。
#include  void max_age(int arr[  ]);int  max , min;           void main(  )  {  int i;
int array[10];
cout<<\"input 10 data:\";  for(i=0;i<10;i++)  {  cin>>array[i];}
max_age(array); min_age(array);
cout<<\"max_age is :\"<}void max_age(int arr[  ]) { int j;
max=arr[0];          //使用全局变量max      for(j=1;j<10;j++)     if (         )       max=arr[j];   //使用全局变量max }
void min_age(int arr[  ]) {int k;
min=arr[0];          //使用全局变量min for(k=1;k<10;k++)     if (min>arr[k])               ;    //使用全局变量min }
2.下列程序将0——100之间的整数写入D盘上的write1.dat文件。
# include < iostream.h > # include < stdlib.h > # include < fstream.h > void main ( )
{  ofstream  file1   ;
file1.open ( \"             \" ) ;      if  ( ! file1 )
{ cerr << \"\\n D:\\\\write1.dat not open \" << endl ;        exit (-1) ; }
for (int k=0 ; k<=100 ; k++ )        file1 << k << \"  \" ;                    ;   }
《C++程序设计》上机考试模拟(50分)
1.根据π/4 =1-1/3+1/5-1/7+…求π的近似值,直到最后一项的值小于0.000001为止。(15分)
2.有一行字符,要求将每个单词中的第一个字母改成大写字母(如果已经是大写则不变)。(提示读一行字符用cin.getline( )  ) (15分)
3.定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类有指针来调用派生类中的虚函数,计算不同形状的面积。(20分)
C++笔试参
一.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B C C B D C D C D B
16 17 18 19 20 21 22 23 24 25 C A A D B B C C D A 二.填空:
1.main()   2.4.5   3.x6. x>z || y>z    7.strlen(str)   8.  2,2,2    9. 34   10. 111911. 0   12. double    13. 4   14. 对对象进行初始化   15。单一继承,多重继承
B B B C A  16.对象  17.基类     18. void   19.派生类,基类   20.字符文件,二进制文件  21.保护,成员 三.看程序写结果:
1.运行结果:
*
#
2.运行结果: 10 5
3.运行结果:
316 516
716
4.运行结果:
4
5.运行结果:
Point 10,20 is deleted. Point 1,1 is deleted.
6.运行结果:
1 5 5 4 3 2 1 1
四.改正错误(每题有二处错误,在错误语句或表达式画线,并写出正确语句。)(每题 4分,共16分)
1.下列程序求字符串长度。 #includeint fun(char s)                //*s or s[] { int i=0;
while(s[i]!='\\n')          //\\0 {i++;} return i; }
void main()
{char s[80];
cin.getline(s,80);    //读一行字符 cout<<\"length:\"<2.下列程序功能是求x的y次方(缺省是x的2次方)   #includedouble fun(double x,double y)   // fun(double x,double y=2) {int i;
double s=1;
for(i=1;ireturn  s; }void main()
{ double x=2.5,y=3;
cout<<\"pow(2.5,2):\"<3.#includeclass Point {private:
int x,y;
public:                //插入 void init(int a,int b)     //  Point { x=a;y=b;} void show()
{ cout<<\"x=  \"<}; void main(){ Point  a(24,50);
a.show();
}
4.#include class Amplifier{
float invol,outvol; public:
Amplifier(float vin,float vout)      {invol=vin;outvol=vout;}       float gain();
};
Amplifier::float gain()                       // float   Amplifier:: gain()
{ return outvol/invol;
}
void main()
{  Amplifier amp(5.0,10.0);
cout<<\"\\n\\nThe gain is =>\"<五.程序填空(每空2分,共10分)1. (1)void min_age(int arr[ ]);   (2) max(3) min=arr[k] ;2.(1)\"d:\\\\write.dat\" ) ;       (2) file1.close ;
《C++程序设计》上机考试模拟(50分)
1.根据π/4 =1-1/3+1/5-1/7+…求π的近似值,直到最后一项的值小于0.000001为止。
(15分)
#include #includeconst double eps=0.000001; void main()
{ double pi=0.0,temp,s=1;   int i=1;   temp=1.0;
while(fabs(temp)>=eps)   { pi+=temp;
i=i+2;    s=-s;     temp=s/i;   }
cout<<\"\\nPI=\"<2.有一行字符,要求将每个单词中的第一个字母改成大写字母(如果已经是大写则不变)。(提示读一行字符用cin.getline( )  ) (15分)#include #include void main() { char ch[81];   int i,flag=0;cout<<\"输入一行字符\\n\";   cin.getline(ch,81);   i=0;
while(ch[i]!='\\0')
{while(ch[i]!='\\0'&& ch[i]==' ') {i++;flag=0;}    while(ch[i]!='\\0'&& ch[i]!=' ')    {
if(ch[i]>='a' && ch[i]<='z' && flag==0)        {ch[i]=ch[i]-32; flag=1;}     i++;      }   }
cout<3.定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类有指针来调用派生类中的虚函数,计算不同形状的面积。(20分)#include  #define PI 3.1415926 #include class base           //抽象基类B0声明 {public:       //外部接口     virtual void display( )= 0;  //纯虚函数成员 };
class circle:public base     //公有派生 {  protected:  double r,s; public:
circle(double x=0)  {  r=x;}
void display( )
{cout<<\"圆的面积 \"<class cfx:public  base     //公有派生 { double a,b,s; public:cfx(double x=0,double y=0){a=x;b=y;}     void display( )  {s=a*b;
cout<<\"长方形面积\"<class tx:public  base     //公有派生 { double a,b,h,s; public:
tx(double a1=0,double b1=0,double h1=0)  {a=a1;b=b1;h=h1;}     void display( )  {s=(a+b)*h/2;
cout<<\"梯形面积\"<void fun(base *ptr)      //普通函数 {
ptr->display( ); }
void main( )      //主函数 {base  *p;          //声明抽象基类指针 circle c1(10);         //声明派生类对象 cfx    c2(9,10);     //声明派生类对象 tx     c3(6,10,5);  p=&c1; fun(p);       p=&c2; fun(p);       p=&c3; fun(p); }