2016年考试题部分答案

#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(){                                        程序执行结果为:The result is:60

Calculate(10,20,30);                                                          The result is:33

Calculate(10,20);                                                               The result is:15

Calculate(10);                                                                    The result is:6

Calculate();

return 0;

}

2、(8 分)

#include <iostream>

using namespace std;

class Point {

public:

Point(int xx = 0, int yy = 0) {x=xx; y=yy; out << "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()

{                                                          程序 2 执行结果为:The constructor is called

Point a(1, 2);                                                                     The copy constructor is called

return a;                                                                             7

}                                                                                         The copy constructor is called

int main() {                                                                         7

Point a(7, 8);                                                                      The copy constructor is called

Point b = a;                                                                      1

cout << b.getX() << endl;

fun1(b);

b = fun2();

cout << b.getX() << endl;

return 0;

}

3、(6 分)

#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);

return 0;

}

程序 3 执行结果为:

Constructing Base2 2

Constructing Base1 1

Constructing Base3 *

Constructing Base1 3

Constructing Base2 4

Constructing Base3 *

1、(12 分)下面程序段通过抽象类 Shape 实现运行时多态。已知 Shape 类中有两个纯虚函数,名称分别为 getName()和 getArea(),shape 基类派生出直角三角形 Triangle 类和矩形 Rectangle 类,且程序运行结果为:

 请填空完成程序。

#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) / 2;

 } 

};

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;

 }

猜你喜欢

转载自blog.csdn.net/qq_25368751/article/details/80661909