1、习 题 二 一、填空题 1. 对运算符进行重载时,不能 变化结合性 ,不能 变化操作数个数 ,不能 变化优先级 。 2. 当++被重载为后置组员函数时需要 0 个参数。 3. 当++被重载为前置组员函数时需要 1 个参数。 4. 在C++中,运算符重载函数能够是 组员函数 ,也能够是 友元函数 ,还能够是 一般函数 。 5. 友元破坏了类的 封装性 特性。 6. 类的友元能够访问这个类的 所有 组员。 7. 类的静态数据组员的初始化是在 类外 进行的。 8. 类的静态组员函数
2、 没有 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 operato
3、r+ ( 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)
4、 B. >(obj1,obj2) C. obj2.operator>(obj1) D. obj1.operator>(obj2) 4. 下列有关C++运算符函数的返回类型的描述中,错误的是( C )。 A. 能够是类类型 B. 能够是int类型 C. 能够是void类型 D. 能够是float类型 5. 为了区分单目运算符的前置和后置运算,在后置运算符进行重载时,额外添加一个参数,其类型是( D )。
5、 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.
6、 类A的组员函数不能访问类B的组员 D. 类B不一定是类A的友元 9. 友元的作用是( A )。 A. 提升组员的运行效率 B. 加强类的封装性 C. 实现数据的隐藏性 D. 增加组员函数的种类 10. 下列有关静态数据组员的特性描述中错误的是( D )。 A. 阐明静态数据组员时前边要加static B. 静态数据组员要在类体外进行初始化 C. 引用静态数据组员时,要在静态数据组员名前加“类名”和作用域运算符 D. 静态数据组员不是所有对象所共用的 11. 有
7、关友元的描述中,正确的是( 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
8、是静态函数,f2不是静态函数 C. f1不是静态函数,f2是静态函数 D. f1和f2都不是静态函数 14. 下面对于友元函数描述正确的是( C )。 A. 友元函数的实现必须在类的内部定义 B. 友元函数是类的组员函数 C. 友元函数破坏了类的封装性和隐藏性 D. 友元函数不能访问类的保护组员 15. 一个类的友元函数或友元类能够通过组员操作符访问该类的( D ) A. 私有组员 B. 保护组员 C. 公有组员 D. 所有组员 16. 下面对静态数据组员的描述中,正确的是( D ) A.
9、静态数据组员能够在类体内进行初始化 B. 静态数据组员不能够被类的对象调用 C. 静态数据组员不受private控制符的限制 D. 静态数据组员能够直接用类名调用 17. C++语言中提供的( B )不是类的组员,但具备类组员的特权。 A. 结构函数 B. 友元函数 C. 虚函数 D. 重载函数 18. 下列有关静态组员函数的说法中不正确的是( C ) A. 静态组员函数不属于对象组员 B. 对静态组员函数的引用不需要使用对象名 C. 静态组员函数中能够直接引用类的非静态组员 D. 静态组员函数
10、中能够直接引用类的静态组员 19. 下面对友元的错误描述是( D )。 A. 核心字friend用于申明友元 B. 一个类中的组员函数能够是另一个类的友元函数 C. 友元函数访问对象的组员不受访问特性影响 D. 友元函数通过this指针访问对象组员 20.下列有关一个类的友元函数的论述中,正确的( B ) A. 允许在类外访问类中除私有组员以外的所有组员 B. 允许在类外访问类中的任何组员 C. 友元也是该类的组员 D. 友元的申明必须被放在该类的公有部分 三、判断题 1、静态数据组员能够通过类名或对象名引用。 2、静态数据组员能够在类
11、内或类外进行初始化。
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
12、ass 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;
13、
return te;
}
A operator=(A t)
{
x=t.x;
y=t.y;
z=t.z;
return *this;
}
void print() { cout< 14、
答案:
30 30
10 10
10 10
10 10
2. #include 15、
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< 16、t;
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< 17、< 18、main()
{
A a(4,8),b,d;
{
A z(3,6), f;
A::f();
}
A::f();
}
答案:
c=5
c=3
5. #include 19、
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< 20、{
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;
21、 }
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< 22、ATE
{
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,m 23、inute,second;
};
void DATE::DateTime(TIME&t)
{
cout<<"Now is"< 24、Day,Year和若干个公有组员函数,并实现如下要求:
(1)组员函数设置缺省参数;
(2)定义一个友元函数来打印日期。
参考程序:
#include 25、 cout< 26、二十四小时的方式输出时间;
参考程序:
#include 27、ow_24(CTimeInfo);
private:
int Hour,Minute;
};
void Show_12(CTimeInfo d)
{
if(d.Hour<=12)
cout<<"am "< 28、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函数中,创建三名学生,并调用上述函数求这三名学生的总分和平均分。
参考程序:
29、
#include 30、re;
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:"< 31、"average:"< 32、ring n,string r,double f)
{
Num=++Count;
Name=n;
Room=r;
fee=f;
}
void display()
{
cout< 33、t 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="< 34、减、乘、除运算。
参考程序:
#include 35、plex &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. 36、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), 37、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类(包括年,月,日)中重载“+=”,计算一个整数和一个d 38、ate类对象之和,并且返回date类对象。例如在1997年2月20日,求出通过100天以后日期。
#include 39、rator +=(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();
}






