面向对象程序设计a卷(编辑修改稿)内容摘要:

、问答题(共 30分) 1. ( 4分)画出下列程序产生的对象的内存映象。 includeiostream includestring using namespace std。 class Member { 得分 评卷人 第 3页 共 12 页 int age。 char *name。 public: Member( int a, char *str) { age = a。 name = new char[ strlen(str)+1 ]。 strcpy( name, str )。 } void print() { coutnameis age years old.endl。 } }。 int main() { Member obj1( 35, Tom), obj2( 57, Johnson)。 ()。 ()。 return 0。 } 2. ( 4分)阅读下面程序,写出运行结果。 includeiostream using namespace std。 class A { int a,b。 public: A(int x,int y){a=x。 b=y。 } 第 4页 共 12 页 void move(int xx,int yy){ a+=xx。 b+=yy。 } void print( ){cout(a,b)endl。 } }。 class B : public A { int x , y。 public: B( int i,int j,int k,int w):A(i,j),x(k),y(w) { } void print( ) { coutx,yendl。 } void f1( ){ move(3,5)。 } void f2( ){ A::print( )。 } }。 int main() 运行结果: { A a(1,2)。 ()。 B b(3,4,5,6)。 ()。 ::print()。 ::print()。 ()。 return 0。 } 3.( 4分) 已定义了 Demo类的静态成员函数 function,该函数无返回值,无参数。 请分别写出该函数在类 中的声明形式和在类外定义时的函数头部,并写出调用该函数的两种方法。 1) 在类中的声明形式: 2) 在类外定义时的函数头部: 3)调用 function函数的两种方法 : 第 5页 共 12 页 4.( 4 分)什么是虚函数。 虚函数与函数重载有哪些相同点与不同点。 5.( 4分)若已定义了 Date 类, myDate为 Date类对象,有如下表达式 myDate+=60。 ,则 1)将 “+=”运算符重载为成员函数,则其函数头为: 2)将 “+=”运算符重载为友员函数,则其函数头为: 6.( 4分) 构造函数完成对象初始化的工作,除了普通构造函数外,还可以定义拷贝构造函数和转换构造函数,请写出这两种构造函数的特点以及在什么情况下会调用它们。 7.( 2 分)设 GradeBookT类模板有一个名为 getGrade 的成员函数,该函数的返回值为 T 类型,没有参数,若在类外定义该函数,请写出该成员函数的函数头。 8.( 2分)函数模板与同名的非模板函数重载时,调用的顺序是怎样的。 第 6页 共 12 页 9.( 2分) 请给出当分别以参数 Li Ping, 32和 Li XiaoPing, 24创建对象时下面程序的输出。 includeiostream includestring using namespace std。 class Student { char *Name。 int Age。 public: Student(char *name, int age) { int length=strlen(name)。 Name=new char[length+1]。 strcpy(Name,name)。 Age= age。 if(length=0 || length10) throw 0。 if(age30) throw 1。 } }。 int main() { try{ Student(Li Ping,32)。 } catch(int size) { if (size==0) coutName is too longendl。 else coutAge is too oldendl。 } } 1) 当对象参数为 Li Ping,32时程序输出 : 2) 当对象参数为 Li XiaoPing ,24时程序输出 : 第 7页 共 12 页 三、程序填空题(每空 2分,共 30分) 1.( 18分)阅读下面程序, 并填空。 该程序的运行结果为: include iostream include string using namespace std。 class Employee { public: Employee(string na, string de) : { } virtual void print() const。 //输出职员信息 //析构函数 { } private: string name。 //姓名 string dept。 // 部门 }。 //在类外实现 print函数 { cout Name: name endl。 cout Dept: dept endl。 } class Manager : //Employee的派生类 { public: Manager(string na, string de, int le) : { } 得分 评卷人 第 8页 共 12 页 virtual void print() const //对基类的 print函 数进行重写 { cout Level: level endl。 } private: int level。 //职别 }。 int main() { //动态创建姓名为 Sally Smith,部门为 Sales,职别为 2的对象。 Employee* emp = //输出该对象相关 信息 return 0。 } 2.( 12分) 以下程序根据用户输入的日期, 从 文件中取出满足条件的记录。 文件每 4行存放一条记录,分别是日期 (day)、时间 (time)、价格 (price)、名称 (name),如左图所示,该文件共存放 5条记录。 运行结果如右图所示,用户输入日期,即可从文件中找到相应记录。 请填空。 第 9页 共 12 页 include iostream include string using namespace std。 void displayEvent( int )。 // display event information int main() { int date。 // event day cout \nSelect date (131) or 0 to exit:。 cin date。 while ( date != 0 ) { displayEvent( date )。 cout \nSelect date (131) or 0 to exit:。 cin date。 } cout \n。 return 0。 } 第 10 页 共 12 页 void displayEvent( int date ) { string time。 string price。 string name。 string contents。 // line of text from calendarFile int day。 // day of the month for an event // open file for input if ( ) { cout Error: File could not be opened. endl。 exit( 1 )。 } // read。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。