面向对象复习(4)

2019-04-08 18:34

int i;

for (i=0;________ ) sum(i); } 30、 class A {

int a,b; public:

A(int aa=0,int bb=0)_____________{} //分别用39和bb对应初始化a和b }; main() {

___________; //定义类A的对象x并用5初始化数据成员a,同时定义y并用x初始化

数//据成员a

___________; //定义p指针,使之指向对象x }

31、仔细阅读下列求两个点之间距离的程序,根据程序的输出结果在划线处填入正确语句。 #include #include

class point {

float x,y; public:

point(float a,float b) { x=a; y=b; } float distance(point &p) {

float dx=_________________;

float dy=_________________; return (float) sqrt (dx*dx+dy*dy); } };

void main()

{

point p1(2,3),p2 (32,43);

cout<<___________________<

32、两个复数只有当它们的实部和虚部分别相等时,才被认为它们相等。在空格处填入合适的内容,以完成下面的程序,使其重载运算符“= =”,用以比较两个复数的相等。请在主函数中输出比较的结果。 #include class complex {

16

double real,imag; public:

complex(double r,double i) { real=r; imag=i; } bool operator= =(complex &); };

bool complex:: operator= =(complex &com)

{ return bool (_______________________________); }

void main( )

{

complex c1(12.3,32.5),c2(21.7,18.6); if( ___________ ) cout<<”true\\n”; else

cout<<”false\\n”; }

33、在下面程序的横线处填上适当字句,使该程序执行结果为6。 #include class base {

int X; public:

________________________________ // 为X置值 _________________________________ // 取X值 };

void main() {

base test; test.init(6);

cout<

34、设计可流的复数类,即要求在复数类中重载输入运算符“>>”和输出运算符“<<”。 #include class Complex {private:

double real,imag;

ostream& operator<<(ostream&,Complex&);

friend istream& operator>>(istream&,Complex&); };

ostream& operator<<(ostream& output,Complex& c) { <<\return output;}

17

istream& operator>>(istream& input,Complex& c) {cout<<\

>>c.real>>c.imag;

return ;} void main() {Complex c1,c2;

>>c1>>c2; \\\\输入c1和c2

<<\输出c1和c2 } 35、

此程序的功能是:构造一个类Box,含有3个数据成员,分别表示盒子的3条边长,还有一个成员函数,用来计算盒子的体积,请给程序填空。 #include class Box {private:

double line,width,height; double volume; :

Box(double a, double b, double c); void vol( ); };

(double a, double b, double c) { line=a;width=b;height=c; ; } void Box::vol( )

{ cout<

{ Box x(3.4, 4.5, 8.3),y(2.0, 4.0, 6.0); x.vol( ); y.vol( ); } 36、完成下面类的定义: #include #include class Person {public:

Person(char *nam)

{ ;//为name 申请内存空间 ;//给name初始化 cout<<\} ~ Person ( )

{ ;//释放内存空间 cout<<\

18

}

void show( ); private:

char *name; };

void ::show( ) {cout<

{Person student1(\

.show( );} 37、定义一个Point类,由它公有派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积。

#include const double PI=3.14159; class Point { protected: double x;

double y; public:

Point(double a,double b){x=a; ;} };

class Rectangle: {public:

Rectangle (double a,double b):Point(a,b){ } void area(); };

void Rectangle ::area( )

{ cout<<\Rectangle is:\ <

Circle (double a,double b,double c): { r=c;}

void area(); private:

double r; };

void Circle ::area( )

{ cout<<\ <

{ Rectangle r(5.5,6.0); Circle c(0,0,5.0); r.area( ); c.area( ); }

38、设计一个2行3列的矩阵类Matrix,重载流插入运算符 \和流提取符\,使之能用于该矩阵的输入和输出。

19

#include #include class matrix{ private: int a[2][3];

friend ostream& operator<<(ostream& output,matrix& m); friend istream& operator>>(istream& input,matrix& m); };

ostream& operator<<(ostream& output,matrix& m) {

for(int i=0;i<2;i++) {for(int j=0;j<3;j++)

<

istream& operator>>(istream& input,matrix& m) {

cout<<\输入6个数:\for(int i=0;i<2;i++) for(int j=0;j<3;j++)

>>m.a[i][j];

return ; }

void main() {matrix m;

>>m; <

39、设计一个包括姓名、学号和成绩的学生类;先把1个学生数据写入文件aaa.dat中,再从文件中读出该学生数据并显示在屏幕上。 #include #include class Student { public:

char name[20]; long number; int score;

Student(char *na = “0”, long nu = 0, int sc = 0)

20


面向对象复习(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:红血丝形成的原因红血丝怎么消除

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: