Dotcpp  /  试卷列表  /  C++

C++

第781题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为8。

#include<iostream.h>
template<class T>
void fun(___________(1)___________)
{
t=x[0];
for(int i=0;i<n;i++)
if(a[i]>t)
t=a[i];
}
void main()
{
int a[]={3,5,8,4,6};
int m;
fun(a,5,__(2)____);
cout<<m<<endl;
}
第782题

下面程序通过把类Distance声明为类Point的友元类来实现计算两点之间的距离。请在下面程序的横线处填上适当字句,以使程序完整。

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
double X,Y;
public:
Point(double x,double y)
{X=x;Y=y;}
____________(1)____________;
};
class Distance
{
public:
double Dis(Point& p1,Point& p2);
{
double t;
____________________(2)___________________;
return t;
}
}
void main()
{
Point p(10,10),q(20,20);
Distance d;
cout<<d.Dis(p,q)<<endl;
}
第783题

程序阅读题

1、

#include <iostream>
using namespace std;
int& element(int x[],int index)
{
return x[index+1];
}
void main()
{
int a[]={3,7,2,1,5};
element(a,3)=9;
for(int i=0;i<5;i++)
cout<<a[i]<<’’;
}

2、

#include<iostream>
using namespace std;
class A
{
int num;
public:
A()
{
num=0;cout<<"A default constructor"<<endl;}
A(int n)
{num=n;cout<<”A constructor,num=”<<num<<endl;}
~A()
{cout<<"A destructor,num="<<num<<endl;}
};
void main()
{
A a,*p;
p=new A(2);
a=*p;
delete p;
cout<<”Exiting main”<<endl;
}

3、

#include<iostream>
using namespace std;
class Format
{
public:
virtual void header()
{cout<<"This is a head"<<endl;
}virtual void footer()
{cout<<"This is a footer"<<endl;}
virtual void body()
{cout<<"This is a body"<<endl;}
void display()
{header();body();footer();}
};
class MyFormat:public Format
{
public:
void header()
{cout<<"This is my header"<<endl;}
void footer()
{cout<<"This is my footer"<<endl;}
};
void main()
{
Format * p;
p=new Format;
p->display();
delete p;
p=new MyFormat;
p->display();
delete p;
}

4、从键盘输入8 4 3 1 9 6 1 1 2 5,给出程序的输出结果。

#include<iostream>
#include<vector>
#include<stack>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
stack<int> s;
int i,n,x;
cin>>n;
for(i=0;i<n;i++)
{
cin>>x;
v.push_back(x);
}
for(vector<int>::iterator p=v.begin();p!=v.end();p++)
if(*p%2==0)
s.push(*p);
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}

5、

#include<iostream>
#include<exception>
using namespace std;
class Fract
{
int den,num;
public:
Fract(int d=0,int n=1)
{
if(n==0)
throw exception("divided by zero");
den = d;num=n;
}
int& operator[](int index)
{
if(index<0||index>1)
throw exception("index out of range");
if(index==0)
return den;
if(index==1)return num;
}
void show()
{ cout<<den<<"/"<<num<<endl;}
};
void main()
{
try
{
Fract f(0,2);
f.show();
cout<<f[1]<<"/"<<f[2]<<endl;
}
catch(exception e)
{
cout<<e.what()<<endl;
}
}
第784题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:Hello

hELLO

#include <iostream.h>
#include <string.h>
class Words
{char *str;
public:
Words(char *s)
{str=new char[strlen(s)+1];
strcpy(str,s);
}
void disp(){cout<<str<<endl;}
char operator[](int i)
{if(str[i]>='A'&&str[i]<='Z')
return char(str[i]+32);
else if(str[i]>='a'&&str[i]<='z')
return char(str[i]-32);
else 
return str[i];
}
};
int main()
{int i;char *s="Hello";
Words word(s);
______________(1)____________;
for (i=0;i<strlen(s);i++)
______________(2)____________;
cout<<endl;
}
第785题

利用函数模板,设计求一个数组元素之和的函数sum和两个数组元素之和的函数sum,请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

数组a之和:15

数组b之和:55

两数组之和:70

#include <iostream.h>
template <class T>
T sum (T a[],int n)
{int i;
T s=0;
for(i=0;i<n;i++)
s+=a[i];
return s;
}
template <class T>
T sum(______________(1)____________)
{
return ______________(2)____________;
}
int main()
{int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,10};
int s1=sum(a,5);
int s2=sum(b,10);
int s3=sum(a,5,b,10);
cout<<"数组a之和:"<<s1<<endl;
cout<<"数组b之和:"<<s2<<endl;
cout<<"两数组之和:"<<s3<<endl;
}
第786题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为

(1,2)

5,6

(6,9)

#include <iostream.h>
class A
{
public:
A(int i,int j){a=i;b=j;}
void move(int x,int y){ ____(1)______;b+=y;}
void show(){cout<<"("<<a<<","<<b<<")"<<endl;}
private:
int a,b;
};
class B:private A
{
public:
B(int i,int j,int k,int l):A(i,j){x=k;y=l;}
void show(){cout<<x<<","<<y<<endl;}
void fun(){move(3,5);}
void fl(){____(2)______;}
private:int x,y;
};
int main()
{A e(1,2);
e.show();
B d(3,4,5,6);
d.fun();
d.show();
d.fl();
}
第787题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

10+10i

#include <iostream.h>
class complex
{
int x,y;
public:complex(){}
complex(int i,int j)
{x=i; y=j;}
complex operator+(complex &p)
{________(1)_______;}
void show()
{cout<<x<<"+"<<y<<"i";}
};
int main()
{complex p1(5,8),p2(5,2),p3;
________(2)_______;
p3.show();
第788题

程序阅读题

1、

#include <iostream.h>
class B
{
public:
B(){}
B(int i,int j){a=i;b=j;}
void printb()
{cout<<"a="<<a<<",b="<<b<<endl;}
private:
int a,b;
};
class A
{
B c;
public:
A(){}
A(int i,int j):c(i,j){}
void printa(){c.printb();}
};
int main()
{A a(7,8);
a.printa();
}

2、

#include <iostream.h>
class CArray
{int *m_pArray;
int m_iSize;
public:
CArray(int iArray[],int iSize)
{m_pArray=iArray;
m_iSize=iSize;
}
int GetSize()
{return m_iSize;}
int &operator[](int i)
{return m_pArray[i];}
};
int main()
{int s[]={3,7,2,1,5};
CArray oArray(s,5);
oArray[0]=9;
for(int i=0;i<5;i++)
cout<<oArray[i]<<" ";
cout<<endl;
}

3、

#include <iostream.h>
#include <math.h>
template <class T>
class TAdd
{T x,y;
public:
TAdd(T a,T b){x=a,y=b;}
T add(){return x+y;}
};
int main()
{TAdd<int> A(5,6);
TAdd<double> B(2.4,5.8);
cout<<"s1="<<A.add()<<endl;
cout<<"s2="<<B.add()<<endl;
}

4、

#include <iostream.h>
class base
{
public:
void who(){cout<<"base class"<<endl;}
};
class derive1:public base
{
public:
void who(){cout<<"derive1 class"<<endl;}
};
class derive2:public base
{
public:
void who(){cout<<"derive2 class"<<endl;}
};
int main()
{base obj1,*p;
derive1 obj2;
derive2 obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();
p=&obj3;
p->who();
obj2.who();
obj3.who();
}

5、

#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
double area(double a, double b, double c) {
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("the side length should be positive");
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("the side length should fit the triangle inequality");
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;cin >> a >> b >> c; //输入1 2 4
try {
double s = area(a, b, c);
cout << "Area: " << s << endl;
} catch (exception &e) {
cout << "Error: " << e.what() << endl;
}
}
第789题

有如下程序:

#include <iostream>
using namespace std;
class GA{
public:
    virtual int f(){return 1;}
};
class GB: public GA{
public:
    virtual int f(){return 2;}
};
void show(GA g){cout<<g.f();}
void display(GA &g){cout<<g.f();}
int main(){
    GA a; show(a); display(a);
    GB b; show(b); display(b);
    Return 0;
}

运行时的输出结果是( )。

第790题

下列语句分别是不同程序中的第一个输入输出语句,若去掉其中的“<

第791题

有如下定义: 

int a[5]= 1,3,5,7,9),* p=a; 

下列表达式中不能得到数值5的是( )。

第792题

已知类Array的定义如下:

class Array {
public :
    int data[ 2 ];
    Array( int n = 0 ) {
        int start ;
        if ( n <= 1 ) start = n - 1; else start = n;
        data[ 0 ] = start ;
        data[ 1 ] = start + 1 ;
    }
};

且有如下程序段:

Array arr(2);
cout<<arr.data[0]<<arr.data[1];

则执行这个程序段的输出是( )。

第793题

已知类Time的定义如下:

class Time { //时间类
private:
    int hour , minute , second ;//时、分、秒
public :
    Time ( int h , int m , int s ) : hour( h ), minute( m ), second( s ) { }
    ____________{
        switch ( index ) {
            case 0 :
                return hour;
            case 1:
                return minute;
            default:
                return second;
        }
    }
};

其中横线处应为下标访问运算符[]的重载函数的函数头,横线处应填入 的代码是( )。

第794题

类Base及其派生类Derived的定义如下:

class Base {
private :
    int a ;
public :
    int b ;
    friend class Derived ;
} ;
class Derived : public Base {
public :
    void foo( ) {
        a = 0;//①
        b = 0;//②
    }
} ;

则下列说法正确的是( )。

第795题

已知类Base、Middle和Derived的定义如下:

class Base {
public :
    virtual void foo ( ) { cout << 'a'; }
} ;
class Middle : public Base {
public :
    void foo ( ) { cout << 'b'; }
} ;
class Derived : public Middle {
public :
    void foo ( ) { cout << 'c'; }
} ;

且有如下程序段:

Base * p1;
Middle * p2;
Derived d;
p1 = &d;
p1 -> foo( );
p2 = &d;
p2 -> foo( );

运行这个程序段的输出是( )。

第796题

已知类AA和BB的定义如下:

class AA
{
public:
    AA(){cout<<'0';}
    ~AA(){cout<<'1';}
};
class BB:public AA
{
public:
    BB(){cout<<'2';}
    ~BB(){cout<<'3';}
};

且有如下主函数定义:

int main(){
    BB b;
    return 0;
}

运行这个main函数的输出是( )。

第797题

已知类CC和函数test的定义如下:

class CC {
public :
    CC( ) { cout << '1'; }
    ~CC( ) { cout << '0'; }
};
void test ( CC c ) { cout << '2'; }

且有如下main函数:

int main(){
    CC c;
    test(c);
    return 0;
}

运行这个main函数的输出是( )。

第798题

已知类Test的定义如下:

class Test{
private :
    static int num ;
public :
    void print ( int num ) {
        cout << ++ num ;
        cout << ++ this -> num;
    }
};
int Test::num=0;

且有如下程序段:

int main(){
    Test t1,t2;
    t1.print(0);
    t2.print(1);
    return 0;
}

运行这个程序段的输出是( )。

第799题

已知类MyClass的定义如下:

class MyClass {
private :
    int num ;
public :
    MyClass ( int d ) : num ( d ) { }
    MyClass ( const MyClass& original ) : num ( original . num ) { }
    friend void print ( MyClass c ) { cout << c.num }
} ;

且有如下程序段:

MyClass c(5) ;
MyClass *p1;
p1=&c;
print(*p1) ;
print(c) ;

运行这个程序段的过程中,对类MyClass的复制构造函数的调用次数是 ( )。

第800题

有如下输出语句:

cout<<setw(5)<<fixed<<setprecigion(3)<<3.1415926;

此语句的输出结果是( )。