收藏 分销(赏)

2024年习题二C练习含有答案.doc

上传人:精**** 文档编号:8190642 上传时间:2025-02-07 格式:DOC 页数:29 大小:49.54KB 下载积分:10 金币
下载 相关 举报
2024年习题二C练习含有答案.doc_第1页
第1页 / 共29页
2024年习题二C练习含有答案.doc_第2页
第2页 / 共29页


点击查看更多>>
资源描述
习 题 二 一、填空题 1. 对运算符进行重载时,不能 变化结合性 ,不能 变化操作数个数 ,不能 变化优先级 。 2. 当++被重载为后置组员函数时需要 0 个参数。 3. 当++被重载为前置组员函数时需要 1 个参数。 4. 在C++中,运算符重载函数能够是 组员函数 ,也能够是 友元函数 ,还能够是 一般函数 。 5. 友元破坏了类的 封装性 特性。 6. 类的友元能够访问这个类的 所有 组员。 7. 类的静态数据组员的初始化是在 类外 进行的。 8. 类的静态组员函数 没有 this指针。 9. 类的静态组员函数访问该类的非静态组员能够通过 参数传递对象 来实现。 10. 不能被重载的类组员函数是 结构和析构函数 。 二、选择题 1. 已知类A有一个带double型参数的结构函数,且将运算符“+”重载为该类友元函数,若如下语句:A x(2.5),y(3.6),z(0); z=x+y; 能够正常运行,运算符重载函数operator+ 应在类中申明为( D )。 A. friend A operator+ (double , double) ; B. friend A operator+ ( double , A &); C. friend A operator+ ( A &, double); D. friend A operator+ ( A & , A &); 2. 下列有关运算符重载的描述中,正确的是( D )。 A. 运算符重载能够变化操作数的个数 B. 运算符重载能够变化优先级 C. 运算符重载能够变化结合性 D. 运算符重载不能够变化语法结构 3. 友元运算符体现式obj1>obj2被C++编译器解释为( A )。 A. operator>(obj1,obj2) B. >(obj1,obj2) C. obj2.operator>(obj1) D. obj1.operator>(obj2) 4. 下列有关C++运算符函数的返回类型的描述中,错误的是( C )。 A. 能够是类类型 B. 能够是int类型 C. 能够是void类型 D. 能够是float类型 5. 为了区分单目运算符的前置和后置运算,在后置运算符进行重载时,额外添加一个参数,其类型是( D )。 A. void B. char C. float D. int 6. 如下运算符中,不能在C++中重载的是( A )。 A. ?: B. + C. - D. <= 7. 在一个类中能够对一个操作符进行( D )重载。 A. 1种 B. 2种如下 C. 3种如下 D. 多个 8. 若类A被阐明成类B的友元,则( D )。 A. 类A的组员即类B的组员 B. 类B的组员即类A的组员 C. 类A的组员函数不能访问类B的组员 D. 类B不一定是类A的友元 9. 友元的作用是( A )。 A. 提升组员的运行效率 B. 加强类的封装性 C. 实现数据的隐藏性 D. 增加组员函数的种类 10. 下列有关静态数据组员的特性描述中错误的是( D )。 A. 阐明静态数据组员时前边要加static B. 静态数据组员要在类体外进行初始化 C. 引用静态数据组员时,要在静态数据组员名前加“类名”和作用域运算符 D. 静态数据组员不是所有对象所共用的 11. 有关友元的描述中,正确的是( C )。 A. 友元函数是组员函数的一个,它能够访问类的私有组员 B. 友元函数不能提升程序的运行效率 C. 友元函数要在类体内阐明 D. 友元函数增强了类的封装性和隐藏性 12. 用来阐明类的友元的是( D )。 A. private B. protected C. public D. friend 13. 已知f1和f2是同一个类的两个组员函数,但f1不能调用f2,下列选项中符合要求的是( B )。 A. f1和f2都是静态函数 B. f1是静态函数,f2不是静态函数 C. f1不是静态函数,f2是静态函数 D. f1和f2都不是静态函数 14. 下面对于友元函数描述正确的是( C )。 A. 友元函数的实现必须在类的内部定义 B. 友元函数是类的组员函数 C. 友元函数破坏了类的封装性和隐藏性 D. 友元函数不能访问类的保护组员 15. 一个类的友元函数或友元类能够通过组员操作符访问该类的( D ) A. 私有组员 B. 保护组员 C. 公有组员 D. 所有组员 16. 下面对静态数据组员的描述中,正确的是( D ) A. 静态数据组员能够在类体内进行初始化 B. 静态数据组员不能够被类的对象调用 C. 静态数据组员不受private控制符的限制 D. 静态数据组员能够直接用类名调用 17. C++语言中提供的( B )不是类的组员,但具备类组员的特权。 A. 结构函数 B. 友元函数 C. 虚函数 D. 重载函数 18. 下列有关静态组员函数的说法中不正确的是( C ) A. 静态组员函数不属于对象组员 B. 对静态组员函数的引用不需要使用对象名 C. 静态组员函数中能够直接引用类的非静态组员 D. 静态组员函数中能够直接引用类的静态组员 19. 下面对友元的错误描述是( D )。 A. 核心字friend用于申明友元 B. 一个类中的组员函数能够是另一个类的友元函数 C. 友元函数访问对象的组员不受访问特性影响 D. 友元函数通过this指针访问对象组员 20.下列有关一个类的友元函数的论述中,正确的( B ) A. 允许在类外访问类中除私有组员以外的所有组员 B. 允许在类外访问类中的任何组员 C. 友元也是该类的组员 D. 友元的申明必须被放在该类的公有部分 三、判断题 1、静态数据组员能够通过类名或对象名引用。 2、静态数据组员能够在类内或类外进行初始化。 3、在静态组员函数中能够访问本类中非静态的数据组员。 4、类的静态组员函数的初始化工作要放在类外进行。 5、若A是B的友元类,B是C的友元类,则A是C的友元类。 6、通过运算符重载,能够定义新的运算符。 7、C++中的所有运算符都能被重载。 8、运算符重载不能变化操作数的个数。 9、运算符重载不能变化其优先级与结合性。 10、在运算符重载函数中不能使用缺省的参数值。 答案: 1、√ 2、× 3、√ 4、× 5、× 6、× 7、× 8、√ 9、√ 10、√ 四、程序分析题(写程序运行成果) 1. #include <iostream.h> class A { int x,y,z; public: A(int vx,int vy,int vz) { x=vx;y=vy;z=vz;} A() { x=0;y=0;z=0;} A operator+(A t) { A te; te.x=x+t.z; te.y=y+t.y; te.z=z+t.z; return te; } A operator-(A t) { A te; te.x=x-t.z; te.y=y-t.y; te.z=z-t.z; return te; } A operator=(A t) { x=t.x; y=t.y; z=t.z; return *this; } void print() { cout<<x<<" "<<y<<" "<<endl; } }; void main() { A t1(10,10,10),t2(20,20,20),t3; t3=t1+t2; t3.print(); t3=t2=t1; t1.print(); t2.print(); t3.print(); } 答案: 30 30 10 10 10 10 10 10 2. #include <iostream.h> class A { public: A(int r,int c) { row=r;col=c; elem=new int[row*col]; } int& operator()(int x,int y) { return elem[col*x+y]; } int operator()(int x,int y)const { return elem[col*x+y]; } ~A() { delete[] elem; } private: int *elem; int row,col; }; void main() { A m(5,8); int i; for( i=0;i<5;i++) m(i,1)=i+5; for( i=0;i<5;i++) cout<<m(i,1)<<","; cout<<endl; } 答案: 5,6,7,8,9, 3. #include <iostream> using namespace std; class goods { private: static int totalweight; int weight; public: goods(int w) { weight=w; totalweight+=weight; } goods(goods &gd) { weight=gd.weight; totalweight+=weight; } ~goods() { totalweight-=weight; } static int gettotal() { return totalweight; } }; int goods::totalweight=0; void main() { goods g1(50); cout<<goods::gettotal()<<endl; goods g2(100); cout<<g2.gettotal()<<endl; } 答案: 50 150 4. #include <iostream> using namespace std; class A { int i,j; static int c; public: A(int a=0,int b=0) { i=a; j=b; c++; } ~A() { c--; } static void f() { cout<<"c="<<c<<endl; } }; int A::c=0; void main() { A a(4,8),b,d; { A z(3,6), f; A::f(); } A::f(); } 答案: c=5 c=3 5. #include <iostream> using namespace std; class point { public: void poi(int px=10,int py=10) { x=px; y=py; } friend int getpx(point a); friend int getpy(point b); private: int x,y; }; int getpx(point a) { return a.x; } int getpy(point a) { return a.y; } void main() { point p,q; p.poi();q.poi(15,15); cout<<getpx(p); cout<<getpy(p)<<endl; cout<<getpx(q); cout<<getpy(q)<<endl; } 答案: 1010 1515 6. #include <iostream.h> class B; class A { int data; A *prev; public: A(int d,A *n){data=d;prev=n;} friend class B; }; class B { A *top; public: B(){top=0;} void push(int i){A *n=new A(i,top);top=n;} int pop() { A *t=top; if(top) { top=top->prev; int c=t->data; delete t; return c; } return 0; } }; void main() { int c[10]={23,34,56,87,67,876,42,657,55,66}; B s; for(int i=0; i<10;i++)s.push(c[i]); for(i=0; i<10; i++)cout<<s.pop()<<","; cout<<endl; } 答案: 66,55,657,42,876,67,87,56,34,23, 7. #include<iostream> using namespace std; class TIME; class DATE { public: DATE(int a=,int m=9,int b=23) { year=a; month=m; day=b; } void DateTime(TIME &t); private: int year,month,day; }; class TIME { public: TIME(int h=0,int m=0,int s=0) { hour=h; minute=m; second=s; } friend void DATE::DateTime(TIME&t); private: int hour,minute,second; }; void DATE::DateTime(TIME&t) { cout<<"Now is"<<year<<'.'<<month<<'.'<<day<<t.hour<<':'<<t.minute<<':'<<t.second<<'.'<<endl; } int main() { DATE b(,9,23); TIME t(10,20,50); b.DateTime(t); return 0; } 答案: Now is.9.2310:20:50. 四、编程题 1. 定义一个处理日期的类TDate,它有3个私有数据组员:Month,Day,Year和若干个公有组员函数,并实现如下要求: (1)组员函数设置缺省参数; (2)定义一个友元函数来打印日期。 参考程序: #include <iostream> using namespace std; class TDate { public: TDate(int m=0,int d=0,int y=0):Month(m),Day(d),Year(y) {} ~TDate(){} friend void Show(TDate); private: int Month,Day,Year; }; void Show(TDate d) { cout<<d.Year<<"年"<<d.Month<<"月"<<d.Day<<"日"<<endl; } int main() { TDate d1,d2(4,8,); Show(d1); Show(d2); return 0; } 2. 设计一个类CTimeInfo,要求其满足下述要求。 (1)要求有一个无参数的结构函数,其初始的小时和分钟分别为:0,0; (2)要求有一个带参数的结构函数,其参数分别对应小时和分钟; (3)要求用一个组员函数实现时间的设置; (4)要求用一个友元函数实现以12小时的方式输出时间; (5)要求用一个友元函数实现以二十四小时的方式输出时间; 参考程序: #include <iostream> using namespace std; class CTimeInfo { public: CTimeInfo():Hour(0),Minute(0){} CTimeInfo(int h,int m):Hour(h),Minute(m) {} ~CTimeInfo(){} void Set(int h,int m) { Hour=h; Minute=m; } friend void Show_12(CTimeInfo); friend void Show_24(CTimeInfo); private: int Hour,Minute; }; void Show_12(CTimeInfo d) { if(d.Hour<=12) cout<<"am "<<d.Hour<<":"<<d.Minute<<endl; else { d.Hour-=12; cout<<"pm "<<d.Hour<<":"<<d.Minute<<endl; } } void Show_24(CTimeInfo d) { cout<<d.Hour<<":"<<d.Minute<<endl; } int main() { CTimeInfo d1,d2(21,8); Show_12(d1); Show_12(d2); Show_24(d2); return 0; } 3. 定义一个Student类,在该类定义中包括:一个数据组员score(分数)及两个静态数据组员total(总分)和count(学生人数) 。组员函数:scoretotalcount(float s)用于设置分数、求总分和累计学生人数;静态组员函数sum用于返回总分;静态组员函数average用于求平均分。在main函数中,创建三名学生,并调用上述函数求这三名学生的总分和平均分。 参考程序: #include <iostream.h> class Student { public: void scoretotalcount(float s) { score=s; total+=score; count++; } static float sum() { return total; } static float average() { return total/count; } private: float score; static float total; static int count; }; float Student::total=0; int Student::count=0; void main() { Student s1,s2,s3; s1.scoretotalcount(70); s2.scoretotalcount(80); s3.scoretotalcount(90); cout<<"total:"<<Student::sum()<<endl; cout<<"average:"<<Student::average()<<endl; } 4. 编写程序实现宾馆住宿客人管理。定义客人类Guest,包括客人编号Num(按先后次序自动生成)、姓名Name、住宿房号Room、房费fee,并定义静态组员Count,用于统计客人总数;提供结构函数,显示信息函数Display,以及返回客人总数函数GetCount等。在主函数中创建3个客人对象进行测试。 参考程序: #include <iostream> #include <string> using namespace std; class Guest { public: Guest(string n,string r,double f) { Num=++Count; Name=n; Room=r; fee=f; } void display() { cout<<Num<<" "<<Name<<" "<<Room<<" "<<fee<<endl; } static int GetCount() { return Count; } private: int Num; string Name; string Room; double fee; static int Count; }; int Guest::Count=0; int main() { Guest g1("zhang","101",200); Guest g2("li","201",300); Guest g3("wang","301",500); g1.display(); g2.display(); g3.display(); cout<<"Total="<<Guest::GetCount()<<endl; return 0; } 5. 生成一个复数类Complex。复数的实部和虚部分别为double 型x和y。重载运算符加、减、乘、除为类的组员函数,用来计算两个复数的加、减、乘、除运算。 参考程序: #include <iostream.h> class complex { private: double x,y; public: complex(double xx=0,double yy=0) { x=xx; y=yy; } void display() { cout<<"("<<x<<","<<y<<"i)"<<endl; } complex operator +(complex &c1); complex operator -(complex &c1); complex operator *(complex &c1); complex operator /(complex &c1); }; complex complex::operator +(complex &c1) { complex c; c.x=x+c1.x; c.y=y+c1.y; return c; } complex complex::operator -(complex &c1) { complex c; c.x=x-c1.x; c.y=y-c1.y; return c; } complex complex::operator *(complex &c1) { complex c; c.x=x*c1.x-y*c1.y; c.y=x*c1.y+y*c1.x; return c; } complex complex::operator /(complex &c1) { complex c; c.x=x*c1.x+y*c1.y; c.y=y*c1.x-x*c1.y; return c; } void main() { complex c1(3,4),c2(4,-5),c3,c4,c5,c6; c3=c1+c2; c4=c1-c2; c5=c1*c2; c6=c1/c2; cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1+c2=";c3.display(); cout<<"c1-c2=";c4.display(); cout<<"c1*c2=";c5.display(); cout<<"c1/c2=";c6.display(); } 6. 在date类(包括年,月,日)中重载“+=”,计算一个整数和一个date类对象之和,并且返回date类对象。例如在1997年2月20日,求出通过100天以后日期。 #include <iostream.h> static int day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; class date { public: date(){yy=0;mm=0;dd=0;} date(int y,int m,int d){yy=y;mm=m;dd=d;} void display(){cout<<yy<<":"<<mm<<":"<<dd<<endl;} friend date operator +=(date c,int i); private:int yy,mm,dd; }; date operator +=(date c,int i) { int a,b=c.mm; a=i+c.dd; while(a>day[b-1]) { a=a-day[b-1]; if(c.mm<12) c.mm++; else { c.yy++;c.mm=1;b=c.mm; } b++; c.dd=a; } return c; } void main() { date date1(1997,2,20),date2; cout<<"date1:"; date1.display(); date2=date1+=100; cout<<"date1+=100:"; date2.display(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服