C++语言程序设计作业4
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
virtual void Print() const { cout<<"Instrument::Print"<<endl; }
};
class Piano: public Instrument
{
public:
void Print()const { cout<<"Piano::Print"<<endl; }
作业4
一、 选择题
1. 下列关于动态联编的描述中,错误的是_________。
A)动态联编是以虚函数为基础的
B)动态联编是在运行时确定所调用的函数代码的
C)动态联编调用函数操作是指向对象的指针或对象引用
D)动态联编是在编译时确定操作函数的
注:先期联编也称静态联编,迟后联编也称动态联编。
2关于虚函数的描述中,正确的是________。
2.假设Point类的坐标为整型,对它重载++(自增)、--(自减)运算符(包括前后缀)。
3.定义一个Shape基类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积。使用Rectangle类创建一个派生类Square。
4.定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( )函数计算对象的周长。
A)virtualintvf(int); B)voidvf(int)=0;
C)virtualvoidvf()=0; D)virtualvoidvf(int)()
14下面的描述中,正确的是_____。
A)virtual可以用来声明虚函数
B)含有纯虚函数的类是不可以用来创建对象的,因为它是虚基类
C)即使基类的构造函数没有参数,派生类也必须建立构造函数
A)一定使用动态联编 B)必须使用动态联编
C)一定使用静态联编D)不一定使用动态联编
5实现运行时的多态性要使用___________。
A)重载函数 B)构造函数 C)析构函数 D)虚函数
6要实现动态联编,必须通过____调用虚函数。
A)对象指针 B)成员名限定 C)对象名 D)派生类名
7在派生类中重新定义虚函数时,除了_____方面,其他方面都必须与基类中相应的
{
val2=val1/3.7854;
cout<<val1 <<" liters is "<<val2<<" gallons."<<endl;
}
};
//Fahrenheit to Celsius
class f_to_c:public convert
{
_____③______
};
void fun(___④______)
Base's des.
根据结果将程序补充完整。
#include <iostream.h>
class Base
{
public:
Base() { cout<<"Base's cons."<<endl; }
____①___{ cout<<"Base's des."<<endl; }
};
class Derived:public Base
A( ){ func();} //C
virtual voidfunc()=0;//D
};
17分析下面的程序,正确的输出结果是___
#include <iostream.h>
#include <string.h>
class Base
{
public:
virtual char *fun() const=0;
{
pute();
}
void main()
{
l_to_g lgobj(4);
f_to_c fcobj(70);
fun(lgobj);
fun(fcobj);
}
13根据不同的输出结果,在函数Tone中填入正确的语句。
#include <iostream.h>
class Instrument
{
public:
};
class Guitar: public Instrument
{
public:
void Print() const { cout<<"Guitar::Print"<<endl; }
};
void Tone(_____①_____)
{
___②_____
}
void main()
{
Guitar g;
Tone(g);
Piano p;
Tone(p);
}
(1)输出结果为:
Instrument: :Print
Instmment::Print
(2)输出结果为:
Guitar: :Print
Piano: :Print
14下列程序的运行结果如下:
Base's cons.
Derived's cons.
Derived's des.
A)虚函数是一个静态成员函数
B)虚函数是一个非成员函数
C)虚函数既可以在函数说明时定义,也可以在函数实现时定义
D)派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型
3在下面四个选项中,________是用来声明虚函数的。
A)virtual B)publicC)using D)false
4 对虚函数的调用________。
A)纯虚函数是一种特殊的虚函数,它没有具体的实现
B)抽象类是指具有纯虚函数的类
C)一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
D)抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出
10下列描述中,____是抽象类的特性。
A)可以说明虚函数B)可以进行构造函数重载
C)可以定义友元函数 D)不能说明其对象
#include <iostream.h>
class Base
{
public:
Base(int i) {b=i;}
___①________
protected:
int b;
};
class Derivel :public Base
{
public:
___②_____
void Print()
{
cout<<”Derive1’s Print() called.”<<endl;
9带有___①___的类称为抽象类,它只能作为___②____来使用。
10抽象类不能__①__,但可以__②__作为参数类型,函数返回类型或显式转换类型。
11 下列程序的运行结果如下:
Derivel's Print() called.
Derive2's Print() called.
根据结果将程序补充完整。
strcpy(ptr,Derivedl1::fun()),
strcat(ptr,Derivedl2::fun());
return ptr;
}
};
void main()
{
Base *pb;
pb=new Derivedl1;
cout<<pb->fun()<<endl;
pb=new Derivedl2;
cout<<pb->fun()<<endl;
{
public:
char* fun() const { return "Derivedl2"; }
};
class Derived2: public Derivedl1,public Derivedl2
{
public:
char* fun() const
{
char ,ptr;
ptr=new char[strlen(Derivedl1::fun())+strlen(Derivedl2::fun())+l];
11_______是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生
类都必须定义自己的版本。
A)虚析构函数 B)虚构造函数 C)纯虚函数 D)静态成员函数
12如果一个类至少有一个纯虚函数,那么就称该类为__。
A)抽象类 B)虚基类 C)派生类 D)以上都不对
13以下___成员函数表示纯虚函数。
{
public:
convert(double i) {vail=i;}
____①______
protected:
double val1;
double val2;
};
//liters to gallons
class l_to_g:public convert
{
public:
____②_____
void compute()
虚函数保持一致。
A)参数个数 B)参数类型 C)函数名称 D)函数体
8下面关于构造函数和析构函数的描述,错误的是__。
A)析构函数中调用虚函数采用静态联编
B)对虚析构函数的调用可以用动本联编
C)当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数
D)构造函数可以声明为虚函数
9关于纯虚函数和抽象类的描述中,错误的是__。
void main()
{
Derived d(10);
Base *pb;
pb=&d; //A
Base & cb=d;
Derived dd=*pb;
Derived &cd=cb; //C
Base bb=d; //D
}
16在下面程序中,A,B、C、D四句编译时出现错误的是___。
class A //A
{
public: //B
private:
int count;
};
class Derived: public Base
{
public:
Derived() :Base(0 ) { }
Derived(int c): Base(c) { }
void print()const { cout<<"Derived"<<endl; }
};
D)静态数据成员可以通过成员初始化列表来初始化
15在下面程序中,A、B、C、D四句编译时不会出现错误的是__。
#include<iostream.h>
class Base
{
public:
Base(){ }
Base(int c): count(c){ }
virtual void print() const=0;
系统使用__②__联编。
4动态联编是在__①___的支持下实现的,它通过___②___来调用该函数操作。
5在一个成员函数内调用一个虚函数时,对该虚函数的调用进行_______联编。
6在析构函数中调用虚函数时,采用________联编。
7C++中__①___虚构造函数,但___②___虚析构函数。
8在类定义中,将______置于虚函数的函数原型的末尾可以将该函数声明为纯虚函数
pb=new Derived2;
cout<<pb->fun()<<endl;
}
A) B)
Base Derivedl1
Base Derivedl2
Base Derivedl1Derivedl2
C)D)
Derivedl1Derivedl2
Derivedl1Derivedl2
Derivedl1Derivedl2Derivedl1Derivedl2
二、填空题
1 动态联编中直到__①__时才能确定调用哪个函数;而静态联编则是在__②___时
进行的。
2 静态联编所支持的多态性称为___①___多态性,动态联编所支持的多态性则称为
__②__多态性,动态多态性由___③____来支持。
3对虚函数使用对象指针或引用调用,系统使用__①__联编;使用对象调用时.
{
public:
Derived() { cout<<"Derived's cons."<<endl; }
~Derived() { cout<<"Derived's des."<<endl; }
};
void main()
{
Base *ptr=_____②______
delete ptr;
}
三、编程
1.在作业1编程1的Point类中完成赋值运算符=、插入运算符<<、比较运算符==、!=和加法运算符+、-的重载。
};
char* Base::fun() const{ return“Base”; }
class Derivedl1: virtual public Base
{
public:
char* fun()const { return"Derivedl1";}
};
class Derivedl2: virtual public Base
}
};
class Derive2:public Base
{
____③_____
};
void fun(___④______)
{
obj->Print();
}
void main()
{
___⑤______
fun(dl);
fun(d2);
}
12将下列程序补充完整。
#include <iostream.h>
class convert
};
class Piano: public Instrument
{
public:
void Print()const { cout<<"Piano::Print"<<endl; }
作业4
一、 选择题
1. 下列关于动态联编的描述中,错误的是_________。
A)动态联编是以虚函数为基础的
B)动态联编是在运行时确定所调用的函数代码的
C)动态联编调用函数操作是指向对象的指针或对象引用
D)动态联编是在编译时确定操作函数的
注:先期联编也称静态联编,迟后联编也称动态联编。
2关于虚函数的描述中,正确的是________。
2.假设Point类的坐标为整型,对它重载++(自增)、--(自减)运算符(包括前后缀)。
3.定义一个Shape基类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积。使用Rectangle类创建一个派生类Square。
4.定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( )函数计算对象的周长。
A)virtualintvf(int); B)voidvf(int)=0;
C)virtualvoidvf()=0; D)virtualvoidvf(int)()
14下面的描述中,正确的是_____。
A)virtual可以用来声明虚函数
B)含有纯虚函数的类是不可以用来创建对象的,因为它是虚基类
C)即使基类的构造函数没有参数,派生类也必须建立构造函数
A)一定使用动态联编 B)必须使用动态联编
C)一定使用静态联编D)不一定使用动态联编
5实现运行时的多态性要使用___________。
A)重载函数 B)构造函数 C)析构函数 D)虚函数
6要实现动态联编,必须通过____调用虚函数。
A)对象指针 B)成员名限定 C)对象名 D)派生类名
7在派生类中重新定义虚函数时,除了_____方面,其他方面都必须与基类中相应的
{
val2=val1/3.7854;
cout<<val1 <<" liters is "<<val2<<" gallons."<<endl;
}
};
//Fahrenheit to Celsius
class f_to_c:public convert
{
_____③______
};
void fun(___④______)
Base's des.
根据结果将程序补充完整。
#include <iostream.h>
class Base
{
public:
Base() { cout<<"Base's cons."<<endl; }
____①___{ cout<<"Base's des."<<endl; }
};
class Derived:public Base
A( ){ func();} //C
virtual voidfunc()=0;//D
};
17分析下面的程序,正确的输出结果是___
#include <iostream.h>
#include <string.h>
class Base
{
public:
virtual char *fun() const=0;
{
pute();
}
void main()
{
l_to_g lgobj(4);
f_to_c fcobj(70);
fun(lgobj);
fun(fcobj);
}
13根据不同的输出结果,在函数Tone中填入正确的语句。
#include <iostream.h>
class Instrument
{
public:
};
class Guitar: public Instrument
{
public:
void Print() const { cout<<"Guitar::Print"<<endl; }
};
void Tone(_____①_____)
{
___②_____
}
void main()
{
Guitar g;
Tone(g);
Piano p;
Tone(p);
}
(1)输出结果为:
Instrument: :Print
Instmment::Print
(2)输出结果为:
Guitar: :Print
Piano: :Print
14下列程序的运行结果如下:
Base's cons.
Derived's cons.
Derived's des.
A)虚函数是一个静态成员函数
B)虚函数是一个非成员函数
C)虚函数既可以在函数说明时定义,也可以在函数实现时定义
D)派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型
3在下面四个选项中,________是用来声明虚函数的。
A)virtual B)publicC)using D)false
4 对虚函数的调用________。
A)纯虚函数是一种特殊的虚函数,它没有具体的实现
B)抽象类是指具有纯虚函数的类
C)一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
D)抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出
10下列描述中,____是抽象类的特性。
A)可以说明虚函数B)可以进行构造函数重载
C)可以定义友元函数 D)不能说明其对象
#include <iostream.h>
class Base
{
public:
Base(int i) {b=i;}
___①________
protected:
int b;
};
class Derivel :public Base
{
public:
___②_____
void Print()
{
cout<<”Derive1’s Print() called.”<<endl;
9带有___①___的类称为抽象类,它只能作为___②____来使用。
10抽象类不能__①__,但可以__②__作为参数类型,函数返回类型或显式转换类型。
11 下列程序的运行结果如下:
Derivel's Print() called.
Derive2's Print() called.
根据结果将程序补充完整。
strcpy(ptr,Derivedl1::fun()),
strcat(ptr,Derivedl2::fun());
return ptr;
}
};
void main()
{
Base *pb;
pb=new Derivedl1;
cout<<pb->fun()<<endl;
pb=new Derivedl2;
cout<<pb->fun()<<endl;
{
public:
char* fun() const { return "Derivedl2"; }
};
class Derived2: public Derivedl1,public Derivedl2
{
public:
char* fun() const
{
char ,ptr;
ptr=new char[strlen(Derivedl1::fun())+strlen(Derivedl2::fun())+l];
11_______是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生
类都必须定义自己的版本。
A)虚析构函数 B)虚构造函数 C)纯虚函数 D)静态成员函数
12如果一个类至少有一个纯虚函数,那么就称该类为__。
A)抽象类 B)虚基类 C)派生类 D)以上都不对
13以下___成员函数表示纯虚函数。
{
public:
convert(double i) {vail=i;}
____①______
protected:
double val1;
double val2;
};
//liters to gallons
class l_to_g:public convert
{
public:
____②_____
void compute()
虚函数保持一致。
A)参数个数 B)参数类型 C)函数名称 D)函数体
8下面关于构造函数和析构函数的描述,错误的是__。
A)析构函数中调用虚函数采用静态联编
B)对虚析构函数的调用可以用动本联编
C)当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数
D)构造函数可以声明为虚函数
9关于纯虚函数和抽象类的描述中,错误的是__。
void main()
{
Derived d(10);
Base *pb;
pb=&d; //A
Base & cb=d;
Derived dd=*pb;
Derived &cd=cb; //C
Base bb=d; //D
}
16在下面程序中,A,B、C、D四句编译时出现错误的是___。
class A //A
{
public: //B
private:
int count;
};
class Derived: public Base
{
public:
Derived() :Base(0 ) { }
Derived(int c): Base(c) { }
void print()const { cout<<"Derived"<<endl; }
};
D)静态数据成员可以通过成员初始化列表来初始化
15在下面程序中,A、B、C、D四句编译时不会出现错误的是__。
#include<iostream.h>
class Base
{
public:
Base(){ }
Base(int c): count(c){ }
virtual void print() const=0;
系统使用__②__联编。
4动态联编是在__①___的支持下实现的,它通过___②___来调用该函数操作。
5在一个成员函数内调用一个虚函数时,对该虚函数的调用进行_______联编。
6在析构函数中调用虚函数时,采用________联编。
7C++中__①___虚构造函数,但___②___虚析构函数。
8在类定义中,将______置于虚函数的函数原型的末尾可以将该函数声明为纯虚函数
pb=new Derived2;
cout<<pb->fun()<<endl;
}
A) B)
Base Derivedl1
Base Derivedl2
Base Derivedl1Derivedl2
C)D)
Derivedl1Derivedl2
Derivedl1Derivedl2
Derivedl1Derivedl2Derivedl1Derivedl2
二、填空题
1 动态联编中直到__①__时才能确定调用哪个函数;而静态联编则是在__②___时
进行的。
2 静态联编所支持的多态性称为___①___多态性,动态联编所支持的多态性则称为
__②__多态性,动态多态性由___③____来支持。
3对虚函数使用对象指针或引用调用,系统使用__①__联编;使用对象调用时.
{
public:
Derived() { cout<<"Derived's cons."<<endl; }
~Derived() { cout<<"Derived's des."<<endl; }
};
void main()
{
Base *ptr=_____②______
delete ptr;
}
三、编程
1.在作业1编程1的Point类中完成赋值运算符=、插入运算符<<、比较运算符==、!=和加法运算符+、-的重载。
};
char* Base::fun() const{ return“Base”; }
class Derivedl1: virtual public Base
{
public:
char* fun()const { return"Derivedl1";}
};
class Derivedl2: virtual public Base
}
};
class Derive2:public Base
{
____③_____
};
void fun(___④______)
{
obj->Print();
}
void main()
{
___⑤______
fun(dl);
fun(d2);
}
12将下列程序补充完整。
#include <iostream.h>
class convert