C++考试(2016-2017)题解

本文详细解析了一次C++考试的题目,包括程序阅读、填空和设计题。程序阅读部分涉及缺省值、构造函数与复制构造函数、继承与组合的顺序。填空题考察了纯虚函数和抽象类以及类的静态成员。设计题涵盖了Circle类的实现、二进制文件操作以及函数重载和复数运算。
摘要由CSDN通过智能技术生成

一、程序阅读题(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;
}

第二题考查的重点:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值