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

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

第一题考查的重点:缺省值。当传入的参数和当前不匹配时,系统自动从后往前补充缺省值。

如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;
}

第二题考查的重点:构造函数复制构造函数所出现的时机。

构造函数   :  大多都是类似    Point  a(7,8)附上初始值这样出现。和以前的int a=3;异曲同工。

复制构造函数:1、初始化时         Point    b=a;            类似与    int    b=a;

                                2、实参传入形参时,调用复制构造函数,就是说在传参时系统会拷贝一份数据。

                                        例如fun1(b)

#include <iostream>
using namespace std;
class Base1{
public:
    Base1(int i){
        cout << "Constructing Base1 " << i << endl;
    }
};
class Base2{
public:
    Base2(int j){
        cout << "Constructing Base2 " << j << endl;
    }
};
class Base3{
public:
    Base3(){
        cout << "Constructing Base3 *" << endl;
    }
};
class Derived: public Base2, public Base1, public Base3{
public:
    Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }
private:
    Base1 member1;
    Base2 member2;
    Base3 member3;

};
int main(){
    Derived obj(1, 2, 3, 4);
                                //Constructing Base2 2
                                //Constructing Base1 1
                                //Constructing Base3 *
                                //Constructing Base1 3
                                //Constructing Base2 4
                                //Constructing Base3 *
    return 0;
}

第三题考查的是:继承组合的先后顺序

1、对于这一题来说首先执行的是(继承):
       class Derived: public Base2, public Base1, public Base3

这个语句是公有形式继承Base2,Base1,Base3.

*重点来了,寇老师说过千万不要和构造函数初始化表的顺序混淆

所说的正是

 Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }

这个构造函数初始化表只是单纯对    对象进行赋上初始值,它没有所谓的顺序可言。

对于有顺序可言的只是继承时的顺序即:

这个语句是公有形式继承Base2,Base1,Base3.

所以答案的前三句就是这样出来的,而且对于Base1,Base2,Base3,会赋值情况,所以要把对应的值填上

//Constructing Base2     2                Base2(b)
//Constructing Base1     1                Base1(a)
//Constructing Base3     *          
      

2、第二句执行的是    组合

//Constructing Base1     3                member1(c)
//Constructing Base2     4                member2(d)
//Constructing Base3     *


二、分析填空题。本题共 2 小题。满分 24 分。

#include<iostream>
using namespace std;
class Shape{
public:
    ____ virtual char *getName()=0;____________;
    ____ virtual double getArea()=0;____________;
};
class Triangle:public Shape{
    ____ double width,height;____________;
public:
    Triangle(double w,double h):width(w),height(h) {}
    char* getName(){
        return "三角形";
    }
    double getArea(){
        _ return width*height*0.5;_______________;
    }
};
class Rectangle:public Shape{
    double width,length;
public:
    Rectangle(double wid,double len):width(wid),length(len){}
    char* getName(){
        ___ return "三角形";___
    }
    double getArea(){
        ___ return width*length;_____________;
    }
};
int main()
{
    Shape* ps;
    Triangle t(10,5);
    Rectangle r(2,8);
    ps=&t;
    cout<<"形状:"<<ps->getName()<<",面积:"<<ps->getArea()<<endl;
    ps=&r;
    cout<<"形状:"<<ps->getName()<<",面积:"<<ps->getArea()<<endl;
    return 0;
}

程序填空题1:这个题考查的重点-纯虚函数和抽象类

后面4个空大多数同学都可以完成,

就好像英语的完形填空有一种就是根据结构来填即可。

需要注意的是:必须要看清楚题目所给的变量名字  和  变量类型;

好比第三个划线处:要注意题目要我们填    三角形类的私有数据成员。

我们首先要知道它的数据类型,我们通过构造函数    初始化列表    观察即可填出答案。

Triangle(double w,double h):width(w),height(h) {}

类型为 double        所缺的两个变量名叫    width,height;

2个空考查就是题目的要求了    纯虚函数和抽象类

本来是书本上一道原题:Shape    类    -    抽象类。

其本身不提供任何    函数实现就提供插口。

包比我们生活中的插座。只提供插口其本身不能带电。

它的定义是根据下面两个类中看出来它分别叫名字和返回类型。

char *getName()            double getArea()

认真看过书的同学都知道虚函数是在前面加一个关键词即可virtual

但是不细心可能还会漏,因为纯虚函数还需要在末尾加上=0;


#include <iostream>
using namespace std;
class Point{
public:
    Point(int x = 0, int y = 0) : x(x), y(y){
        _____count++;_____
    }
    Point(Point &p){
        x = p.x;
        y = p.y;
        _____count++;_____
    }
    ~Point(){
        _____count--;_____
    }
    int getX(){
        return x;
    }
    int getY(){
        return y;
    }
    void showCount(){
        cout << " Object count = " << count << endl;
    }
private:
    int x, y;
    static int count;
};
_____int Point::count=0;_______
int main()
{
    Point a(1, 2);
    cout << "Point A: " << a.getX() << ", " << a.getY();
    a.showCount();
    Point b(a);
    cout << "Point B: " << b.getX() << ", " << b.getY();
    b.showCount();
    //Point A: 1,2     Object count = 1
    //Point B: 1,2     Object count = 2
    return 0;
}

程序填空题二:考查的重点是    类的静态成员

题目需要我们在    构造函数    或者    复制构造函数    执行的时候    点的个数count++;

而在    析构函数调用时        点的个数count--;

简单易懂关键还是注意静态数据成员需要在类外面定义初始值。

格式    int    Point    ::    count    =0;

最后别忘了题目所要我们填写的程序输出结果。(注意格式即可)


三、程序设计题 (54 分)

1、(12 分)定义一个 Circle 类,有数据成员 Radius 表示圆的半径,成员函数 GetArea()
计算圆的面积,在主函数中构造一个 Circle 的对象进行测试。

#include<iostream>
using namespace std;
const double pi=3.1415926;
class Circle{
public:
    double GetArea();
    Circle(double R=1):Radius(R){}
private:
    double Radius;
};
double Circle::GetArea(){
    return pi*Radius*Radius;
}
int main()
{
    Circle  a(2);
    cout<<"a圆的面积为:"<<a.GetArea()<<endl;
    return 0;
}
2、(12 分)已知整型数组 int a[5]={5,6,7,8,9}; 请编写程序将数组 a 中数据用二进制
方式保存到磁盘文件 out.dat 中,然后再从该文件中读出全部数据显示到屏幕上。

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int a[5]={5,6,7,8,9};
    int b[5];
    ofstream fout("out.dat",ios::binary);
    if(fout.is_open()){
        fout.write( (char*)a  , sizeof(a) ) ;
    }else{
        cerr<<"open error!"<<endl;
        exit(1);    //return 1;
    }
    fout.close();
    ifstream fin("out.dat",ios::binary);
    if(fin.is_open()){
        fin.read(   (char*)b,sizeof(b) );
    }else{
        cerr<<"open error!"<<endl;
        exit(1);    //return 1;
    }
    for(int i=0;i<5;i++){
        cout<<b[i]<<'\t';
    }
    cout<<endl;
    return 0;
}


(15 分)按要求完成相应编程任务:
(1)请采用函数重载的方法,设计函数求两个数据之差的绝对值,
其中参与运算的两个数据分为同时为 int 型以及同时为
double 型两种情况;
(2)给出完成(1)中功能的函数模板实现形式;
(3)编写主函数对(1)、(2)中所设计的不同形式函数进行测试。


#include<iostream>
using namespace std;
double Fabs(double a,double b){
    double temp=a-b;
    return temp>0?temp:(-temp);
}
int Fabs(int a,int b){
    int temp=a-b;
    return (temp>0)?temp:(-temp);
}
template <typename T> T abs(T a,T b){
    T temp=a-b;
    return (temp>0)?temp:(-temp);
}
int main()
{
    int a1=10,b1=20;
    double a2=5.4,b2=9.6;
    cout<<"int类型的绝对值:\t"<<Fabs(a1,b1)<<endl;
    cout<<"double类型的绝对值:\t"<<Fabs(a2,b2)<<endl;
    cout<<"模板类的绝对值:\t"<<abs(a1,b1)<<endl;
    cout<<"模板类的绝对值:\t"<<abs(a2,b2)<<endl;
    return 0;
}
4、定义一个复数类,通过重载运算符:”+”、”-”和”*”为复数类的成员函数,直接
实现两个复数之间的运算。编写一个完整的程序包括主函数测试。

#include<iostream>
using namespace std;
class Complex{
private:
    int real,imag;
public:
    Complex(int R=0,int I=0):real(R),imag(I){}
    friend Complex operator +(const Complex &L,const Complex &R);
    friend Complex operator -(const Complex &L,const Complex &R);
    friend Complex operator *(const Complex &L,const Complex &R);
    void show()const;
};
Complex operator +(const Complex &L,const Complex &R){
    return Complex(L.real+R.real,L.imag+R.imag);
}
Complex operator -(const Complex &L,const Complex &R){
    return Complex(L.real-R.real,L.imag-R.imag);
}
Complex operator *(const Complex &L,const Complex &R){
    int r1=L.real,r2=R.real;
    int i1=L.imag,i2=R.imag;
    return Complex(r1*r2-i1*i2,r1*i2+i1*r2);
}
void Complex::show()const{
    cout<<"("<<real<<","<<imag<<")\n";
}
int main()
{
    Complex a(1,4),b(2,3);
    Complex c1=a+b;
    Complex c2=a-b;
    Complex c3=a*b;
    c1.show();
    c2.show();
    c3.show();
    return 0;
}





猜你喜欢

转载自blog.csdn.net/z_sea/article/details/80972715