《C++面向对象程序设计练习题.doc》由会员分享,可在线阅读,更多相关《C++面向对象程序设计练习题.doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、面向对象程序设计练习题一、单项选择题(1)若有以下类定义class MyClass public:MyClass() cout 1; ;则执行语句MyClass a,b2,*p2;后,程序的输出结果是( B )A)11B)111C)1111D)11111(2)下面程序的输出结果是( B )#include using namespace std;int i = 0;int fun(int n) static int a = 2;a+;return a+n;void main()int k = 5;int i = 2;k += fun(i);k += fun(i);cout k;A)13B)14
2、C)15D)16(3)下面程序的输出结果是( A )#include using namespace std;void swap1( int &v1, int &v2) int tmp = v2;v2 = v1;v1 = tmp;void swap1( int *v1, int *v2) int tmp= *v2;*v2 = *v1;*v1 = tmp;void main() int i = 10, j = 20; swap1(i,j); swap1(&i,&j); couti”,”jendl;A)10,20B)20,10C)10,10D)20,20(4)下面的程序段的运行结果为( D )ch
3、ar str = job, *p = str;cout *(p+2) endl;A)98 B)无输出结果 C)字符b的地址 D)字符b(5)下面程序的输出结果是( C )#include using namespace std;class A public: A (int i) x = i; void dispa () cout x “,”; private : int x ;class B : public A public: B(int i) : A(i+10) x = i; void dispb() dispa(); cout x endl; private : int x ;void
4、main()B b(2);b.dispb();A)10,2B)12,10C)12,2D)2,2(6)下面程序的输出结果是( C )#include using namespace std;class Base public: Base(int i) cout i; Base () ;class Base1: virtual public Base public: Base1(int i, int j=0) : Base(j) cout i; Base1() ;class Base2: virtual public Base public: Base2(int i, int j=0) : Bas
5、e(j) cout i; Base2() ;class Derived : public Base2, public Base1 public: Derived(int a, int b, int c, int d) : mem1(a), mem2(b), Base1(c), Base2(d), Base(a) cout b; private: Base2 mem2; Base1 mem1;void main() Derived objD (1, 2, 3, 4); A)B)C)D)(7)以下程序对一维坐标点类Point进行运算符重载,输出结果是( A )#include using name
6、space std;class Point public:Point (int val) x = val; Point operator +() x+; return *this; Point operator +(int) Point old = *this; +(*this); return old; Point operator +(Point a) x += a.x; return *this; int GetX() const return x; private:int x;int main()Point a(10); cout (+a).GetX(); cout a+.GetX()
7、;A)1111B)1011C)1112D)1010(8)下面程序的输出结果是( C )#include using namespace std;class Base public: virtual void f() cout “f0+”; void g() cout “g0+”; ;class Derived : public Base public: void f() cout “f+”; void g() cout f(); p-g(); A)f+g+B)f0+g+C)f+g0+D)f0+g0+(9)下面程序的输出结果是( C )#include using namespace std;i
8、nt countp=0;class Point int X,Y; public: Point(int x=0,int y=0) X=x; Y=y; Point(Point &p)X=p.X;Y=p.Y;countp+; friend Point myfun(Point p1 ,Point p2 ,const Point &p3); Point myfun(Point p1,Point p2,const Point &p3)Point tmp(p1.X+p2.X+p3.X,p1.Y+p2.Y+p3.Y);return tmp;void main() Point pp0,pp1(1,2),pp2(
9、1);myfun(pp0,pp1,pp2);std:coutcountpendl;A)0B)4C)3D)6(10)下面程序的输出结果是( C )#include using namespace std;class Samplefriend long fun (Sample s) if (s.x 2) return 1; return s.x * fun(Sample(s.x-1); public: Sample (long a) x = a; private: long x;void main() int sum = 0; for (int i=0; i6; i+) sum += fun(Sa
10、mple(i); cout sum; A)120B)16C)154D)34二、填空题(1)以下程序是用来计算小于16的整数的阶乘,请补充完整。#includeusing namespace std; const int ArSize =16;void main() double facArSize; fac1 =fac0 =1.0; for(int i=2;iArSize;i+) faci = i*faci-1 ;(2)下面是个Cat类的声明与使用,请补充完整。#include using namespace std; class Cat static int count; public: C
11、at() count+; cout Now cat number is count endl; Cat() count-; cout Now cat number is count endl; ; int Cat:count =0; int main() Cat a, b, c; return 0; (3)将下面的MyPoint类定义补充完整,使得程序的输出结果是(10,10)(5,5)#include class MyPoint public: MyPoint(int xx=5, int yy=5) X = xx; Y = yy; std:cout(X,Y) ; private: int X
12、, Y; ;void main() MyPoint a(10,10),b;(4)已知文件之间具有以下的包含关系(用#include指令):point.cpp 包含 point.h,point.cpp 包含 line.h,line.h包含 point.h。那么如下的point.h文件缺少什么语句,请补充完整。/ Point类的声明,point.h #ifndef _POINT_H_ #define _POINT_H_class Point .;#endif(5)下列函数的功能是判断字符串str是否对称,对称则返回true,否则返回false。请在横线处填上适当内容,实现该函数。bool fun
13、(char *str)int i=0, j=0;while (strj) j+;for(j-; i=j ;(6)请将下列程序补充完整,使得输出结果为“Destructor Derived Destructor Base”。#include using namespace std;class Base public: virtual Base () cout “Destructor Base” endl; ;class Derived : public Base public: Derived() cout “Destructor Derived” endl; ;void main () Bas
14、e *pBase = new Derived; delete pBase ; (7)如有下列程序: #include using namespace std;class Demopublic: Demo()coutdefault constructorn; Demo(const Demo &x)coutcopy constructorn;Demo userCode(Demo b)Demo c(b);return c;void main() Demo a,d; coutcalling userCode()n; d = userCode(a); 执行上面的程序的过程中,构造函数Demo()和Dem
15、o(const Demo &x)被调用的次数分别是 2 和 3 次。三、程序题(1)程序改错每个注释“/ ERROR”所在的一行语句存在错误。请改正这些错误,使程序的输出结果为: 00:00:0001:37:19注意:只需修改注释“/ ERROR”所在的那一行语句,不要改动程序中的其他内容。#include#includeusing namespace std;class StopWatch /秒表类 int hours; /小时 int minutes; /分钟 int seconds; /秒 public: StopWatch():hours(0), minutes (0), second
16、s(0) void reset()hours=minutes=seconds=0; /前进1秒StopWatch& operator+() / ERROR if(seconds=60) / ERROR seconds=0; if(+minutes=60) minutes=0; +hours; return *this; friend void show(StopWatch);void show(StopWatch watch) coutsetfill(*); / ERROR coutsetw(2)watch.hours: setw(2)watch.minutes: setw(2)watch.s
17、econdsendl;int main() StopWatch sw; show(sw); for(int i=0;i5839;i+) sw+; show(sw); return 0;解答: StopWatch& operator+(int) (4分): if(+seconds=60) (3分): coutsetfill(0); (3分)(2)语句填空下面的程序设计了一个宠物类,请你在空行上填入合适的语句,使程序完整。(每空2分)#include enum Pets_typedog,cat,bird,fish;class Petsprivate: char *name; Pets_type t
18、ype;public: Pets(const char *n=sonny,int type1 = 0); Pets(const Pets &s); Pets& operator=(const Pets &s); Pets();Pets:Pets(const char *n,int type1) /构造函数 name = new charstrlen(n) + 1; strcpy(name,n); type = Pets_type(type1); Pets:Pets(const Pets &s) /拷贝构造函数 name = new charstrlen(s.name) + 1 ; strcpy
19、(name,s.name); type = s.type;Pets:Pets() /析构函数 delete name ;Pets& Pets:operator =(const Pets &s) if (this=&s) /确保不要向自身赋值 return *this; delete name; name = new charstrlen(s.name) + 1; strcpy(name,s.name); type = s.type; return *this ;void main() Pets mypet1,mypet2(John,1),hispet(Danny,2); Pets youpet
20、(hispet); mypet1 = youpet;3)编写程序段 补充编制下列程序,其功能是从键盘读取任意长度的文本内容,将文本存放到Doc类的对象myDoc中。然后在显示器输出。 请在/* begin *和/* end *之间补充程序。#include#include#includeusing namespace std;class Doc private: static const int MaxLength; / 可能的最大文本字符串长度 char *str; / 文本字符数组首地址指针 int length; / 文本字符串长度 public: Doc(const char *s =
21、 ); / 构造函数 Doc(); / 析构函数 / 重载istream的提取运算符 friend istream& operator(istream& is, Doc& doc); / 重载ostream的插入运算符 friend ostream& operator(istream& is, Doc& doc)/提示:使用is.getline函数可以从is流提取字符串,包括空格。/* begin * char bufferDoc:MaxLength;is.getline(buffer, Doc:MaxLength);int inLen = (int)strlen(buffer);if (in
22、Len doc.length) delete doc.str;doc.str = new charinLen+1;strcpy(doc.str, buffer);doc.length = inLen;return is;/* end * ostream& operator(ostream& os, Doc& doc)os myDoc; cout myDoc;四、程序设计题1、简单的计算程序界面如下图所示,操作数1对应编辑框控件的ID为 IDC_EDIT1,对应值类型的成员变量m_op1(int);操作数2对应编辑框控件的ID为IDC_EDIT2,对应值类型的成员变量m_op2(int);计算结
23、果对应的编辑框控件的ID 为IDC_EDIT3,对应值类型的成员变量m_result(int)。 假设输入的数为整数,加法按钮的功能是将两操作数相加,并将结果在对应的编辑框IDC_EDIT3显示出来。写出程序的设计步骤,编辑框内容发生变化时对EN_CHANGE消息的响应函数及加法按钮对应的代码。2、某公司雇员(employee)包括经理(manager),技术人员(technician)和销售员(salesman)。开发部经理(developermanger),既是经理也是技术人员。销售部经理(salesmanager),既是经理也是销售员。以employ类为虚基类派生出manager,tec
24、hnician和salesman类;再进一步派生出developermanager和salesmanager类。employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。manager类派生属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。而developermanager类,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。salesamanager类,pay()函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。