第八章多态性和虚函数

判断题

1.虚函数是用virtual 关键字说明的成员函数。(T)
2.动态绑定是在运行时选定调用的成员函数的。(T)
3.构造函数可以声明为虚函数。(F)
4.构造函数可以声明为纯虚函数。(F)
5.虚函数不能是类的静态成员。(T)
6.重定义虚函数的派生类必须是公有继承的。(T)
7.作为虚函数隐含参数的this指针,决定了虚函数调用时执行的代码。(T)

选择题

1.关于纯虚函数和抽象类的描述中,( )是错误的。(C)

A.纯虚函数是一种特殊的虚函数,它没有具体的实现
B.抽象类是指具有纯虚函数的类
C.一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
D.抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出

2.虚析构函数的作用是。(C)

A.虚基类必须定义虚析构函数
B.类对象作用域结束时释放资源
C.delete动态对象时释放资源
D.无意义

3.在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值。(A)

A.相同
B.不同
C.相容
D.部分相同

5.假设 Aclass为抽象类,下列正确的说明语句是。(B)

A.Aclass fun( int ) ;
B.Aclass * p ;
C.int fun( Aclass ) ;
D.Aclass Obj ;

抽象类的两可以,三不能
两可以:
1.可以声明指向抽象类的指针:A *p;
2.可以声明抽象类的引用:A &h(A,s);
三不能:
1.不能建立抽象类对象:A a;
2.不能作为函数的返回类型:A f();
3.不能作为函数的参数类型:A g(A,s);

6.关于动态绑定的下列描述中,( )是错误的。(D)

A.动态绑定是以虚函数为基础的
B.动态绑定在运行时确定所调用的函数代码
C.动态绑定调用函数操作是通过指向对象的指针或对象引用来实现的
D.动态绑定是在编译时确定操作函数的

3.在C++语言中设置虚基类的目的是( ) 。(C)

A.简化程序代码
B.提高程序的运行效率
C.解决多继承造成的二义性问题
D.缩短程序的目标代码

函数题

1.汽车收费
现在要开发一个系统,管理对多种汽车的收费工作。 给出下面的一个基类框架

class Vehicle
{
protected:
string NO;
public:
Vehicle(string n){
    NO = n;
}
virtual int fee()=0;//计算应收费用
};

以Vehicle为基类,构建出Car、Truck和Bus三个类。

Car的收费公式为: 载客数8+重量2
Truck的收费公式为:重量5
Bus的收费公式为: 载客数
3

生成上述类并编写主函数

主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数

输入格式:第一行输入测试用例数。接着每个测试用例占一行,每行给出汽车的基本信息,第一个数据为当前汽车的类型:1为car,2为Truck,3为Bus。第二个数据为它的编号,接下来Car是载客数和重量,Truck要求输入重量,Bus要求输入载客数。

要求输出各车的编号和收费。

裁判测试程序样例:

#include<iostream>
#include <string>
using namespace std;
class Vehicle
{
protected:
    string NO;//编号
public:
    Vehicle(string n){        NO = n;  }
    virtual int fee()=0;//计算应收费用
};

/* 请在这里填写答案 */

int main()
{
    Car c("",0,0);
    Truck t("",0);
    Bus b("",0);
    int i, repeat, ty, weight, guest;
    string no;
    cin>>repeat;
    for(i=0;i<repeat;i++){
        cin>>ty>>no;
        switch(ty){
            case 1: cin>>guest>>weight; c=Car(no, guest, weight); cout<<no<<' '<<c.fee()<<endl; break;
            case 2: cin>>weight; t=Truck(no, weight); cout<<no<<' '<<t.fee()<<endl; break;
            case 3: cin>>guest; b=Bus(no, guest); cout<<no<<' '<<b.fee()<<endl; break;
        }
    }
    return 0;
}

输入样例:

4
1 002 20 5
3 009 30
2 003 50
1 010 17 6

输出样例:

002 170
009 90
003 250
010 148
class Car:public Vehicle
{
private:
	int guest,weight;
public:
	Car(string n,int g,int w):Vehicle(n)
	{
		guest=g;
		weight=w;
	}
	int fee()
	{
		return guest*8+weight*2;
	}
};
class Truck:public Vehicle
{
private:
	int weight;
public:
	Truck(string n,int w):Vehicle(n)
	{
		weight=w;
	}
	int fee()
	{
		return weight*5;
	}
};
class Bus:public Vehicle
{
private:
	int guest;
public:
	Bus(string n,int g):Vehicle(n)
	{
		guest=g;
	}
	int fee()
	{
		return guest*3;
	}
};

2.抽象类Shape

请编写一个抽象类Shape,包括两个纯虚函数,分别为计算面积getArea()和计算周长getPerim()。通过Shape类派生出矩形类Rectangle和圆类Circle,并计算各自的面积和周长。

测试用例具体要求:输入1表示测试矩形类,之后输入矩形长和宽。输入2表示测试圆类,之后输入圆半径。

Shape类定义如下:

class Shape {
    public:
        virtual double getArea()=0;
        virtual double getPerim()=0;
};

裁判测试程序样例:

#include <iostream>
using namespace std;
const double PI=3.14;

class Shape {
    public:
        virtual double getArea()=0;
        virtual double getPerim()=0;
};

/* ------请在这里填写答案 ------*/

int main() {
    Shape *p;
    int n;
    double w,h,r;
    scanf("%d",&n);
    switch(n) {
        case 1: {
            cin>>w>>h;
            Rectangle rect(w,h);
            cout<<"area="<<rect.getArea()<<endl;
            cout<<"perim="<<rect.getPerim()<<endl;
            break;
        }
        case 2: {
            cin>>r;
            Circle c(r);
            cout<<"area="<<c.getArea()<<endl;
            cout<<"perim="<<c.getPerim()<<endl;
            break;
        }
    }

    return 0;
}

输入样例1:

1
4 5

输出样例1:

area=20
perim=18

输入样例2:

2
5

输出样例2:

area=78.5
perim=31.4

#include <iostream>
using namespace std;
const double PI=3.14;

class Shape {
    public:
        virtual double getArea()=0;
        virtual double getPerim()=0;
};

/* ------请在这里填写答案 ------*/
class Rectangle:public Shape
{
private:
	double w,h;
public:
	Rectangle(double w1,double h1)
	{
		w=w1;
		h=h1;
	}
	double getArea()
	{
		return w*h;
	}
	double getPerim()
	{
		return 2*(w+h);
	}
};

class Circle:public Shape
{
private:
	double r;
public:
	Circle(double r1)
	{
		r=r1;
	}
	double getArea()
	{
		return PI*r*r;
	}
	double getPerim()
	{
		return 2*PI*r;
	}
};

int main() {
    Shape *p;
    int n;
    double w,h,r;
    scanf("%d",&n);
    switch(n) {
        case 1: {
            cin>>w>>h;
            Rectangle rect(w,h);
            cout<<"area="<<rect.getArea()<<endl;
            cout<<"perim="<<rect.getPerim()<<endl;
            break;
        }
        case 2: {
            cin>>r;
            Circle c(r);
            cout<<"area="<<c.getArea()<<endl;
            cout<<"perim="<<c.getPerim()<<endl;
            break;
        }
    }

    return 0;
}

3.虚函数的应用
补充下列代码,使得程序的输出为:
A:3
A:15
B:5
3
15
5
类和函数接口定义:
参见裁判测试程序样例中的类和函数接口。
裁判测试程序样例:

#include <iostream>
using namespace std;
class CMyClassA {
    int val;
public:
    CMyClassA(int);
    void virtual print();
};
CMyClassA::CMyClassA(int arg) {
    val = arg;
    printf("A:%d\n", val);
}
void CMyClassA::print() {
    printf("%d\n", val);
    return;
}

/* 在这里填写代码 */

int main(int argc, char** argv) {
    CMyClassA a(3), *ptr;
    CMyClassB b(5);
    ptr = &a;
    ptr->print();
    a = b;
    a.print();
    ptr = &b;
    ptr->print();
    return 0;
}

输入样例:

None

输出样例:

A:3
A:15
B:5
3
15
5
//
class CMyClassB:public CMyClassA{
    int val;
public:
    CMyClassB(int val0):CMyClassA(val0*3){
        val=val0;
        cout<<"B:"<<val<<endl;
    }
    void print(){
        cout<<val<<endl;
    }
};
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值