简单的题目,第一个答对有高分,100分,限时明天晚上
二、程序阅读题
1.写出下列程序的运行结果。
#include <iostream>
using namespace std;
void num( )
{ extern int x,y;
int a=15,b=10;
x=a-b; y=a+b;
}
int x,y;
int main(void)
{ int a=7,b=5;
x=a+b;
y=a-b;
num( );
cout<<x<<" "<<y<<endl;
return 0;
}
2. 写出下列程序的运行结果。
#include<iostream>
using namespace std;
class A
{friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
double total;
double rate;
};
double count(A&a)
{
a.total+=a.rate*a.total;
return a.total;
}
int main(void )
{
A x(100,0.5),y(50,0.1);
cout<<count(x)<<’,’<<count(y)<<’\n’;
cout<<count(x)<<’\n’;
return 0;
}
3. 写出下列程序的运行结果。
#include <iostream>
using namespace std;
class Point;
ostream & operator << (ostream &output, const Point &p);
class Point
{
private:
int _x, _y;
public:
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
Point() { _x = _y = 0; }
friend ostream & operator << (ostream &output, const Point &p);
};
Point& Point::operator++()
{
_x++;
_y++;
return *this;
}
Point Point::operator++(int)
{
Point temp = *this;
++*this;
return temp;
}
Point& Point::operator--()
{
_x--;
_y--;
return *this;
}
Point Point::operator--(int)
{
Point temp = *this;
--*this;
return temp;
}
ostream & operator << (ostream &output, const Point &p)
{
output<< "A的值为:" << p._x << " , " << p._y << endl;
return output;
}
void main()
{
Point A,B,C,D,E;
cout<<A;
A=B++;
cout<<A;
A=++C;
cout<<A;
A=D--;
cout<<A;
A=--E;
cout<<A;
}
4. 指出main变量i在每条赋值语句执行后的值(15) 。
int x=2, y=x+30;
struct A{
static int x;
int y;
public:
operator int( ){ return x-y; }
A operator ++(int){ return A(x++, y++); }
A(int x=::x+2, int y=::y+3){ A::x=x; A::y=y; }
int &h(int &x);
};
int &A::h(int &x)
{
for(int y=1; y!=1|| x<201; x+=11, y++) if(x>200) { x-=21; y-=2;}
return x-=10;
}
int A::x=23;
void main( ){
A a(54, 3), b(65), c;
int i, &z=i, A::*p=&A::y;
z=b.x; i=a.x; i=c.*p;
i=a++; i=::x+c.y; i=a+b;
b.h(i)=7;
}
5. 写出程序运行结果。
#include <iostream>
using namespace std;
class CT {
public :
CT () {
cout << "Default constructor called" << endl;
}
CT (const CT &rhs) {
cout << "Copy constructor called" << endl;
}
};
int main ()
{
CT ct; CT *p;
cout << "Step1" << endl;
p = new CT;
CT ct3 (*p);
cout << "Step2" << endl;
delete p;
return 0;
}