以下对File类的public boolea isFile()方法的描述,哪个是正确的?
A.判断该File对象所对应的是否是文件
B.判断该File对象所对应的是否是目录
C.返回文件的最后修改时间
D.在当前目录下生成指定的目录
A.判断该File对象所对应的是否是文件
B.判断该File对象所对应的是否是目录
C.返回文件的最后修改时间
D.在当前目录下生成指定的目录
试题五(共 15分)
阅读以下说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:
void addElement(Object): 在列表尾部添加一个对象;
Object lastElement(): 返回列表尾部对象;
int numberOfElement(): 返回列表中对象个数;
void removeLastElement(): 删除列表尾部的对象。
现需要借助LinkedList来实现一个Stack栈类,C++代码1和C++代码2分别采用继承和组合的方式实现。
【C++代码 1】
class Stack :public LinkedList{
public:
void push(Object o){ addElement(o); }; //压栈
Object peek(){ return (1) ; }; //获取栈顶元素
bool isEmpty(){ //判断栈是否为空
return numberOfElement() == 0;
};
Object pop(){ //弹栈
Object o = lastElement();
(2) ;
return o;
};
};
【C++代码 2】
class Stack {
private:
(3) ;
public:
void push(Object o){ //压栈
list.addElement(o);
};
Object peek(){ //获取栈顶元素
return list. (4) ;
};
bool isEmpty(){ //判断栈是否为空
return list.numberOfElement() == 0;
};
Object pop(){//弹栈
Object o = list.lastElement();
list.removeLastElement();
return o;
};
};
【问题】
若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)
(33 )有如下程序:
#include<iostream>
using namespace std;
class Pair{
int m;
int n;
public:
Pair (int i , int j ) : m (i ) , n (j ) {}
boot operator > (pair p ) const; // 须在类体外给出定义
} ;
int main () {
Pair Al (3,4 ) , p2 (4,3 ) ; p3 (4,5 ) ;
Cout<< (pl>p2 ) << (P2>P1 ) << (p2>p3 ) << (p3>p2 ) ;
return 0;
}
运算符函数 。 operator> 的功能是比较两个 Pair 对象的大小 , 当左边对象大时 , 返回 true , 否则返 回false 。 比较规则是首先比较两对象的 m 成员 , m 大者为大 ; 当 m 相等时比较 n , n 大者为大 。 程序输出 0101 ,下列对运算符重载函数的正确定义是
A ) bool Pair::operator> (Pair p ) const
{if (m!=p.m ) return m>p.m; return n>p.n;}
B ) bool Pair:;operator> (Pair p )
{if (m!=p.m ) return m>p.m; return n>p.n;}
C ) bool Pair::operator> (Pair p ) const
{if (m>p.m ) return true; return n>p.n;}
D ) bool Pair:;operator> (Pair p )
{if (m>p.m ) return true; return n>p.n;}
阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
本程序用于评选优秀教师和学生。当输入一系列教师或学生的记录后,将优秀学生及教师的姓名列出来。其类结构如下图所示:
【程序】
include <iostream.h>
include <stdio. h>
class base
{
protected:
char name[8];
public:
void getname(){cout<<"name:"; cin>>name;}
void printname(){cout<<"name:"<<name<<endl;}
(1)
};
class student: (2)
{
int num;
public:
void getnum()
{cout<<"score:"; cin>>num;}
bool isgood()
{return (3) }
};
class teacher: (2)
{
int num;
public:
void getnum()
{cout<<"paper:"; cin>>num;}
bool isgood()
{return (num>3)?true:false;}
void main()
{
base *p[50];
student *pstud;
teacher *ptech;
char ch;
int count=0;
do{
cout<<"input teacher(t) or student(s):";
cin>>ch;
if(ch=='s')
{
pstud=new student;
pstud->getname();
pstud->getnum();
p[count++]=pstud;
}
else if(ch=='t')
{
(4)
ptech->getname();
ptech->getnum();
p[count++]=ptech;
}
else
cout<<"input is wrong"<<endl;
cout<<"continue to iput(y/n)?";
cin>>ch;
}while(ch=='y');
for(int i=0;i<count;i++)
if((5))
p[i]->printname();
}
●试题五
阅读下列程序说明,将应填入(n)处的字句写在答卷纸的对应栏内。
【程序说明】
对于一个公司的雇员来说,无非有3种:普通雇员、管理人员和主管。这些雇员有共同的数据:名字、每小时的工资,也有一些共同的操作:数据成员初始化、读雇员的数据成员及计算雇员的工资。但是,他们也有不同。例如,管理人员除有这些共同的特征外,有可能付固定薪水,主管除有管理人员的共同特征外,还有其他物质奖励等。3种雇员中,管理人员可以看作普通雇员的一种,而主管又可以看作管理人员的一种。我们很容易想到使用类继承来实现这个问题:普通雇员作为基类,管理人员类从普通雇员类中派生,而主管人员类又从管理人员类中派生。
下面的程序1完成上述各个类的定义,并建立了3个雇员(一个普通雇员、一个管理人员和一个主管)的档案,并打印出各自的工资表。将"程序1"中的成员函数定义为内联函数,pay成员函数定义为虚函数,重新完成上述要求。
【程序1】
//普通雇员类
class Employee
{
public:
Employee(char*theName,float thePayRate);
char*getName()const;
float getPayRate()const;
float pay(float hoursWorked)const;
protected:
char*name;//雇员名称
float payRate;//薪水等级
};
Employee::Employee(char*theName,float thePayRate)
{
name=theName;
payRate=thePayRate;
}
char*Employee::getName() const
{
return name;
}
float Employee::getPayRate()const
{
return payRate;
}
float Employee::pay(float hoursWorked)const
{
return hoursWorked*payRate;
}
//管理人员类
class Manager∶public Employee
{
public:
//isSalaried付薪方式:true付薪固定工资,false按小时付薪
Manager(char*theName,float thePayRate,bool isSalaried);
bool getSalaried()const;
float pay(float hoursWorked)const;
protected:
bool salaried;
};
Manager::Manager(char*theName,float thePayRate,bool isSalaried)
∶Employee(theName,thePayRate)
{
salaried=isSalaried;
}
bool Manager::getSalaried() const
{
return salaried;
}
float Manager::pay(float hoursWorked)const
{
if(salaried)
return payRate;
/*else*/
return Employee::pay(hoursWorked);
}
//主管人员类
class Supervisor:public Employee
{
public:
Supervisor(char*theName,float thePayRate,float theBouns):
Employee(theName,thePayRate, (1) ),bouns(theBouns){}
float getBouns()const{return bouns;}
float pay(float hoursWorked)const
return (2) ;
}
protected:
float bouns;
}
#include"iostream.h"
void main()
{
Employee e("Jack",50.00);
Manager m("Tom",8000.00,true);
Supervior s("Tanya",8000.00,8000.00);
cout<<"Name:"<<e.getName()<<endl;
cout<<"Pay:"<<e.pay(80)<<endl;//设每月工作80小时
cout<<"Name:"<<m.getName()<<endl;
cout<<"Pay:"<<m.pay (40) <<endl;
cout<<"Name:"<<s.getName()<<endl;
cout<<"Pay:"<<s.pay (40) <<endl;//参数40在这里不起作用
}
【程序2】
#include"employee.h"
//普通雇员类
class Employee
{
public:
//构造函数
Employee(string theName,float thePayRate):
name(theName),payRate(thePayRate){}
//取雇员姓名
string getName() const{returnname;}
//取雇员薪水等级
float getPayRate()const{return payRate;}
//计算雇员薪水
virtual float pay(float hoursWorked)const{return (3) ;}
protected:
string name;//雇员名称
float payRate;//薪水等级
};
//管理人员类
//继承普通雇员类
class Manager:public Employee
{
public:
//构造函数
//isSalaried标识管理人员类的付薪方式
//true 按阶段付薪(固定工资)
//false按小时付薪
Manager(string theName,float thePayRate,bool isSalaried):
Employee(theName,thePayRate),salaried(isSalaried){}
//取付薪方式
bool getSalaried()const{return salaried;}
//计算薪水
virtual float pay(float (4) )const;
protected:
bool salaried;
};
float Manager::pay(float hoursWorked)const
{
if(salaried)//固定付薪方式
return payRate;
else//按小时付薪
return (5) ; }
//主管人员类
class Supervisor: (6)
{
public:
//构造函数
Supervisor(string theName,float thePayRate,float theBouns):
Manager(theName,thePayRate,true),bouns(theBouns){}
//取奖金数额
float getBouns()const{return bouns;}
//计算薪水
virtual float pay(float hoursWorked)const
{
retum payRate+bouns;
}
(7)
float bouns;
}
#include"employee.h"
#include"iostream.h"
void main()
{
(8) *ep[3];ep[0]=new Employee("Jack","50.00");
ep[1]=new Manager("Tom","8000.00",true);
ep[2]=new Supervior("Tanya","8000.00","8000.00");
for(int i=0;i<3;i++){
cout<<"Name:"<< (9) <<endl;
cout<<"Pay:"<< (10) <<endl;//设每月工作80小时
}
}
阅读以下说明和C++程序,将应填入(N)处的字句写在对应栏内。
[函数8.1说明]
现考虑编写一个扑克游戏:只用一副无大小王的扑克,扑克的花色(suit)分为Spade、Heart、Diamond和Club,每门花色的牌共13张,面值(rank)分别为2、3、4、5、6、7、8、9、10、Jack、Queen、King和Ace,每张扑克牌应包含如下信息:惟一的ID号(1~52)、花色、面值、背面图案的编号等。每张扑克牌的操作有:判断两张扑克牌是相同花色吗?相同面值吗,判断一张扑克牌是给定的花色吗?是给定的面值吗?请补充扑克牌类Card类的声明和实现代码,要求选取适当形式的数据成员描述每张扑克牌的信息,同时以成员函数的形式实现指定的操作。
[C++程序]
const enum SUIT {SPADE=0,HEART,DIAMOND,CLUB};
const enum RANK{TWO=0 THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING,ACE};
class Card。
{
public:
(1) (int id):mID (id),mSuit((id 1)/13),mRank((id-1)%13) {}
bool IsSameSuit (const Card& rhs)
{retum ((this ==&rhs)? (2)));}
bool IsSankRank (const Card& rhs)
{return ((this==&rhs)?false: mRank==rhs. mRank);}
bool IsSuit (int suit)
{return (3);}
bool IsRank(int rank)
{return mRank==rank;}
private:
static int nBackImg;//背面图案
const int mID;
const int mSuit;
const int mRank;
};
[函数8.2说明]
输入一字符串,将其中所有的小写字母转换为大写字母,大写字母转换为小写字母,然后显示输出转换后的字符串。
[C++程序]
include<iostream. h>
class vector
{
int a;
int b;
public:
vector (int x=0,int y=0):a(x),b(y){}
double operator*((4))
{
double c;
(5);
return c;
}
void input (int x, int y)
{a=x;
b=y;
}
void output ()
{cout<<'('<<a<<',' <<b<<") "<<end1;
}
};
void main()
{
vector x(10,20),y; //定义x, y, z三个矢量类对象,并将x置初值(10,20)
doubled;//定义实数d以存放点乘结果
y. input (2,3); //给y赋值为(2,5)
d=x*y;//两矢量点乘
x. output (); //输出矢量x
y. output (); //输出矢量y
cout<<d<<end1; //输出点乘结果
}
阅读下列说明和c++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6—7所示:
【c++代码】
include<1ist>
include
include
using namespace std;
class AbstractFile{
protected:
string name;//文件或目录名称
public:
void printName(){cout<*getChildren()=0; //获得一个目录的子目录或文件
};
class File:public AbstractFile{
public:
File(string name){ (1) =name;)
void addChild(AbstractFile*file){return ;)
void removeChiid(AbstractFile*file){return;}(2) getChildren(){return (3 ) ;}
};
class Folder:public AbstractFile{
private:
listchildList; //存储子目录或文件
public:
Folder(string name){ (4) =name;}
void addChild(AbstractFile*file){childList.push back(file);}
void removeChiid(AbstractFile*file)(chiidList.remove(file);}
list*getChildren(){return (5) ;)
};
voidmain(){
//构造一个树形的文件/目录结构
AbstractFile*rootFolder=new Folder(“C:\\”);
AbstractFile*compositeFolder=flew Folder(”composite”);
AbstractFile*windowsFolder=new Folder(”windows”);
AbstractFile*file=new File(”TestComposite.java”);
rootFolder->addChild(compositeFolder);
rootFolder->addChild (windowsFolder);
compositeFolder->addChiid(file);
)
有如下程序:
}}}}include<iostream>
using namespace std;
class Pair{
int m;
int n;
public:
Pair(int i,int J):m(i),n(J){}
bool operator>(Pair P)const; //需在类体外给出定义
};
int main(){
Pair pl(3,4),p2(4,3),p3(4,5);
COUt<<(pl>p2)<<(p2>p1)<<(p2>p3)<<(p3>p2);
return 0;
{
运算符函数operator>功能是比较两个Pair对象的大小,当左边对象大时,返回true,否则返回false。比较规则是首先比较两对象的m成员,m大者为大;当m相等时比较n.n大者为大。程序输出0101,下列对运算符重载函数的正确定义是
A.bool Pair::operator>(Pair P)const {if(m!=P.m)return m>P.m;return n>P.n;)
B.bool Pair::operator>(Pair P) {if(m!=P.m)return m>P.m;return n>P.n;)
C.bool Pair::operator>(Pair P)const {if(m>P.m)return true;return 11>P.n;)
D.bool Pair::operator>(Pair P) {if(m>P.m)return true;return 11>P.n;}
class Matrix {
public:
Matrix(int row,int col); // 构造一个具有row 行col 列的矩阵
~Matrix() {delete []mem;} // 析构函数
friend bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3);
// 定义Multiply() 为友元函数,该函数把m1×m2的值赋给m3
// 其他成员函数从略
private:
int *mem; // 动态申请矩阵空间
const int rows,cols; // 矩阵的行数和列数
};
Matrix::Matrix(int row,int col):rows(row),cols(col)
{
mem = new int[row*col];
}
bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3)
{
// 确定矩阵是否能够进行相乘
if(m1.rows != m3.rows ||m2.cols != m3.cols || m1.cols != m2.rows) return false;
// 定义sum变量,用于计算乘积矩阵m3中每个元素的值
int sum;
// 请在下面编写剩余部分
}
阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。
【说明】
以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
【C++程序】
ifndef H_MiniComplex
define H_MiniComplex
include <iostream>
using namespace std;
class MiniComplex{
public: //重载流插入和提取运算符
(1) ostream&operator<<(ostream &osObject,const MiniComplex&complex){
osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")";
return osObject;
}
(2) istream&operator>>(istream&isObject, MiniComplex&complex){
char ch;
isObject >>complex.realPart>>ch>>complex.imagPart>>ch;
return isObject;
}
MiniComplex(double real=0,double imag=0); //构造函数
MiniComplex operator+(const MiniComplex&otherComplex)const; //重载运算符+
MiniComplex operator-(const MiniComplex&otherComplex)const; //重载运算符-
MiniComplex operator*(const MiniComplex&otherComplex)const; //重载运算符*
MiniComplex operator/(const MiniComplex&otherComplex)const; //重载运算符/
bool perator==(const MiniComplex&otherComplex)const; //重载运算符==
private :
double (3);
double imagPart;
};
end if
include "MiniComplex.h"
bool MiniComplex::operator==(const MiniComplex&otherComplex)const{
return(realPart==otherComplex.realPart&&imagPart==ortherComplex.imagPart);
}
MiniComplex::MiniComplex(double real,double imag){
realPart== real; imagPart==imagPart;
}
MiniComplex MiniComplex::operator+(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = realPart+ortherComplex. realPart;
temp.imagPart = imagPart +ortherComplex. imagPart;
return temp;
}
(4)
{ MiniComplex temp;
temp.realPart= realPart-ortherComplex. realPart;
temp.imagPart = imagPart-ortherComplex. imagPart;
return temp;
}
MiniComplex MiniComplex::operator*(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart);
temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart);
return temp;
}
MiniComplex MiniComplex::operator/(const MiniComplex&otherComplex)const{
MiniComplex temp;
float tt;
tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart);
temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt;
temp.imagPart =((imagPart *ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt;
return temp;
}
include <iostream>
include <MiniComplex.h>
using namespace std;
int main(){
MiniComplex numl(23, 34),num2(56, 35);
cout<<"Initial Value of num1="<<num1<<"\n Initial Value of num2="<<num2<<end1;
cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符
cout<<num1<<"-"<<num2<<"="<<num