一、程序填空
1、完成下面类的定义: #include
Person(char *nam)
{ (1) ;//为name 申请内存空间 (2) ;//给name初始化 cout<<\}
~ Person ( )
{ (3) ;//释放内存空间 cout<<\}
void show( ); private:
char *name; };
void Person::show( ) {cout< {Person student1(\ (4) .show( ); } 2、定义一个Point类,由它公有派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积。 #include double x,double y; public: Point(double a,double b){x=a; (5) ;} }; class Rectangle: (6) {public: square(double a,double b): (7) { } void area(); }; void Rectangle ::area( ) { cout<<\ (8) < 1 Circle (double a,double b,double c): (9) { r=c;} void area(); private: double r; }; void Circle ::area( ) { cout<<\ (10) < { Rectangle r(5.5,6.0); Circle c(0,0,5.0); r.area( ); c.area( ); } 3、利用友元函数实现比较两个点离原点的距离。 #include Point(double xx,double yy) { (11) ; (12) ;} void Getxy( ) ; (13) int compare(Point &a, Point &b); private: double x,y; } ; void (14) Getxy( ) {cout<<\ int compare(Point &a, Point &b)//若a比b更近,则返回0,否则返回1 { (15) ; double db=b.x*b.x+b.y*b.y ; return (16) ; } void main( ) { Point p1(1.0,3.0),p2(2.0,4.0); cout<<\ cout<<\ int d= (17) ; if(!d) cout<<\ else cout<<\ } 4、实现复数类的输入和输出重载。 #include 2 double real,imag; public: (18) ostream& operator<<(ostream&,Complex&); friend istream& operator>>(istream&,Complex&); }; ostream& operator<<(ostream& output,Complex& c) {output<<\ (19) ; } istream& operator>>(istream& input,Complex& c) {cout<<\input>>c.real>>c.imag; (20) ; } void main() {Complex c1,c2; cin>>c1>>c2; cout<<\ cout<<\} 5、以下程序在M行N列的二维数组中,找出每一行上的最大值,显示最大值的行号、列号和值,请填空。 #include (1) ; int x[M][N]=(1,5,6,4,2,7,4,3,8,2,3,1); for( (2) ;i for( (3) ;j cout< 6、下列程序求两点之间的距离,请填空。 #include double x,double y; public: Point(double a,double b){ (6) ;} double distance(Point &p) ; }; 3 double Point :: distance(Point &p) { double dx= (7) ; double dy= (8) ; return sqrt(dx*dx+dy*dy); } void main( ) { Point p1(2,3),p2(5,6) ; cout<< (9) < 7、定义一个关于日期的类,然后声明对象,判断该日期是否为闰年并输出。 #include public: void SetDate(int y,int m,int d); int isLeapYear( ); void Print( ); (10) int year,month,day; }; void TDate::SetDate(int y,int m,int d) { year=y; (11) ;day=d;} int TDate::is LeapYear( ) { return ( (12) )||(year@0==0); } void TDate::Print() {cout< {TDate date1,date2; date1.SetDate(2006,8,8); date2.SetDate(2008,8,8); int leap= (13) ; date1.Print(); if( (14) ) cout< cout< 8、将文件内容保存在另一文件中,并将内容显示到屏幕上。 #include 4 void main( ) { fstream file1; file1.open(?x1.dat?, (15) ); if( (16) ) {cout< fstream file2; file2.open(?x2.dat?,ios::out|ios::trunc); if( (17) ) {cout< while( (18) ) { file1.read(&ch,1); cout< (19) ; } (20) ; file2.close( ); } 9、下列程序的功能是对10个整数用选择法排序。 #include #define SIZE (sizeof(data)/(sizeof(data[0])) void main( ) {int datra[ ]={12,23,9,34,45,7,78,-33,59,3}; int i,j,k,t; cout< for(i=0;i { (1) ; for(j= (2) ;j<= SIZE;j++) if(data[j] cout< for(i=0;i Person(char *nam) { (4) ;//为name 申请内存空间 5