#include
Monster(int hp,int att,int def)
:hitpoint(hp),demage(att),defense(def) {} virtual ~Monster() {}
bool fight(Monster & other);
virtual void attack(Monster & other)=0; void ReduceHP(int harm)
{ hitpoint-=harm; if(hitpoint<0) hitpoint=0; } int GetHP() const {return hitpoint;} int GetDemage() const {return demage;} int GetDefense() const {return defense;} protected: int hitpoint; int demage; int defense; };
bool Monster::fight(Monster & other) {
while(true) { attack(other);
if(other.GetHP()<=0) return true; other.attack(*this);
if(hitpoint<=0) return false; } }
class Dog:public Monster {
public:
Dog(int hp,int att,int def):Monster(hp,att,def) {} virtual ~Dog() {}
virtual void attack(Monster & other) {
int harm=(demage-other.GetDefense()+5)*2; if (harm<2) harm=2; other.ReduceHP(harm); } };
class Cat:public Monster { public:
Cat(int hp,int att,int def):Monster(hp,att,def) {} virtual ~Cat() {}
virtual void attack(Monster & other) {
int harm=demage*2-other.GetDefense(); if (harm<1) harm=1; other.ReduceHP(harm); } };
void main() {
Dog d(100,10,7); Cat c(120,8,9); if(d.fight(c))
cout<<\ else
cout<<\
}
大学期末考试www.earthedge.cn
吉林大学面向对象程序设计
2006-2007期末考试试题
2006-2007学年第1学期2005级《C++面向对象程序设计》期末考试试题(A卷)
考试时间:2007年1月13日
班级 学号 姓名
2 本试卷满分100分;
2 请将答案写在答题纸上,写明题号,不必抄题,字迹工整、清晰;
2 请在答题纸和试题纸上都写上你的班级,学号和姓名,交卷时请将试题纸、答题纸和草纸
一并交上来。
一、单选题(共10分,每题1分)
1. C++中解决命名冲突的机制是:D
(A) 虚基类 (B) 虚函数 (C) 函数重载 (D) 名字空间
2. 若类A的一个对象所占的内存空间中包含虚函数表的入口地址,则: C
(A) 类A不能有静态数据成员 (B) 类A中公有的成员函数一定是虚的 (C) 类A中至少有一个成员函数是虚的 (D) 类A的析构函数一定是虚的
3.任意一个类,析构函数的个数最多是:B
(A) 不限个数 (B) 1 (C) 2 (D) 3
4. 下列关于this指针的说法,哪个是正确的: D
(A) this指针一定指向常量型数据 (B) this指向的数据不可更改
(C) 静态成员函数中也可以访问this指针 (D) this指针本身可直接作为成员函数的返回值
5.在类定义中,为说明成员的访问权限,private, protected, public 可以出现次数为:A
(A)次数没有具体限定 (B) 每种至多一次
(C) public 至少一次 (D) 每种至少一次
6.下面哪种定义方式是正确的,并且使得p可以作为函数void f( A* const pp);的实参:A
(A) A * p = new A; (B) A a; A* p = a; (C) const A* p = new A; (D) A a; const A* p = a;
7. obj是类A的一个对象,执行语句 const A& aA= obj; ,则下列说法正确的是: C (A) 类A的拷贝构造函数会被调用 (B) 类A的赋值函数会被调用
(C) &aA的值就是 &obj (D) 语句obj.f( );等价于语句aA.f( );
8.下面关于访问类A的私有数据成员的说法,错误的是: C (A) 类A的友元函数可以访问类A的私有成员。
(B) 类A的友元类中的非静态成员函数可以访问类A的私有成员。 (C) 类A的嵌套类中的非静态成员函数可以访问类A的私有成员。
(D) 类A中的非静态成员函数可以访问类A的私有成员。
9.类A中有唯一的一个成员函数f,且f是公有的静态或非静态成员函数,对于类A的一个对象a,执行语句 a.f(100);成功,那么f 的函数原型不可以是:B
(A) A& f( int, int=50 ); (B) void f(int& ) ;
(C) const A * f(const int ); (D) A f( const int&);
10. 下面关于类的成员函数描述不正确的是:A
(A) 静态成员函数内可以直接访问类的非静态成员数据
(B) 静态成员函数内可以直接访问类的静态成员数据
(C) 非静态成员函数可以直接访问类的非静态成员数据
(D) 非静态成员函数可以直接访问类的静态成员数据
二、判断正误,对于你认为错误的论述,说明原因或举出反例。(每题2分,共20分)
相关推荐: