一、程序阅读题(22 分)
#include <iostream>
using namespace std;
void Calculate(int x=1,int y=2,int z=3)
{
int t=x+y+z;
cout<< "The result is:"<<t<<endl;
}
int main()
{
Calculate(10,20,30); //The result is:60
Calculate(10,20); //The result is:33
Calculate(10); //The result is:15
Calculate(); //The result is:6
return 0;
}
//The result is:60
Calculate(10,20); //The result is:33
Calculate(10); //The result is:15
Calculate(); //The result is:6
return 0;
}
第一题考查的重点:缺省值。当传入的参数和当前不匹配时,系统自动从后往前补充缺省值。
如Calculate(10),实参传入形参X=10,其余y=2,z=3.返回的值t=10+2+3=15
还有,这个题还需要把提示词输出
#include <iostream>
using namespace std;
class Point
{
public:
Point(int xx = 0, int yy = 0){
x=xx;
y=yy;
cout << "The constructor is called " << endl;
}
Point(Point &p);
int getX(){
return x;
}
int getY(){
return y;
}
private:
int x, y;
};
Point::Point(Point &p)
{
x = p.x;
y = p.y;
cout << "The copy constructor is called " << endl;
}
void fun1(Point p)
{
cout << p.getX() << endl;
}
Point fun2()
{
Point a(1, 2);
return a;
}
int main()
{
Point a(7, 8); //The constructor is called
Point b = a; //The copy constructor is called
cout << b.getX() << endl; //7
fun1(b); //The copy constructor is called
//7
b = fun2(); //The constructor is called
cout << b.getX() << endl; //1
return 0;
}
//The constructor is called
Point b = a; //The copy constructor is called
cout << b.getX() << endl; //7
fun1(b); //The copy constructor is called
//7
b = fun2(); //The constructor is called
cout << b.getX() << endl; //1
return 0;
}
第二题考查的重点: