~Student( ); void show( ); };
Student::Student(char *nam,int n,float sco) { (4) ;//为name 申请内存空间 (5) (name,nam);//给name初始化 num=n; score=sco; }
Student::~Student( )
{ (6) ;//释放内存空间 }
20、已知有一个基类figure,要求建立两个继承figure的派生类square和isosceles_triangle,每一个派生类都定义一个函数area(),分别用来显示矩形与等腰三角形的面积。 #include
double height;
double width; };
class square: (7) figure {public:
square(double h,double w); void area(); };
class isosceles_triangle: (8) figure {public:
isosceles_triangle(double h,double w); void area(); };
square::square(double h,double w) { (9) } void square::area()
{cout<<\ (10) < void isosceles_triangle::area() {cout<<\ (12) < {square s(10.0,6.0); isosceles_triangle i(8.0,6.0); s.area(); i.area();} 21、下列程序定义了函数模板,实现了双精度和字符串各模板函数的调用。 11 #include { (14) } char *min(char *a,char *b) {return (15) } void main( ) {double a=3.56,b=8.23; char s1[]=\ cout<<\中较小者:\cout<<\中较小者:\} 22、下列程序是把数据输出到文件c:\\\\new.dat 中,然后读出并显示出来。 #include int no; char name[20]; }; void main( ) { int i; struct Person worker[M]={1,'Jonh',2, 'Rose'}; fstream file; file.open(?c:\\\\new.dat?, (16) ); if( (17) ) {cout< for(i=0;i file.open(?c:\\\\new.dat?, (19) ); for(i=0;i {file.read((char *)&worker[i],sizeof(Person)); cout< } (20) } 23、下列程序将100-200之间不能被3整除的数输出,每行输出5个数。 12 #include int i,count=0; for(i=100;i<=200;i++) { if( (1) ) continue; cout< 24、下面的类定义了拷贝构造函数,请完成该类的定义和实现。 #include public: MyClass(int xx=0,int yy=0){X=xx;Y=yy;} (4) ; private: int X,Y; }; MyClass:: (5) ; { X= (6) ; Y=p.Y; } 25、定义一个点类Point,用构造函数初始化类Point的对象,成员函数Distance计算平面上两点之间的距离。 #include double x; double y; (8) Point(double a,double b); void Distance(Point p); }; Point::Point(double a,double b) { (9) } void Point::Distance(Point p) 13 { double dis; dis= (10) ; cout<<\平面上这两点间距离为:\} void main( ) {Point p1(2.0,2.0),p2(4.0,4.0); P1.Distance(p2); } 26、定义类coord,用模板实现此类对象的比较,比较时使用了运算符的重载。 #include coord(int x1,int y1) { (11) } int getx(){return x;} int gety(){return y;} int operator<(coord &c); }; int coord::operator<(coord &c) {if(x (13_) obj &min(obj &o1,obj &o2) {if(o1 void main() { coord c1(5,11),c2(6,18); coord c3= (15) ; cout<<\较小者的坐标:(\ double d1=1.5,d2=5.6; cout<<\较小实数:\} 27、下面程序用于统计文件abc.txt中的字符个数,请填空。 #include 14 { fstream file; file.open(“abc.txt”,ios::in); if( (17) ) {cout<<“abc.txt cannot open”< abort(); } char ch; int i=0; while(!file.eof()) { (18) ; (19) ; } (20) ; } 28、在下面程序段横线处填上适当的内容。 class A { ________ int n; public: A(int nn=0):n(nn) { if(n= =0) a=0; else a=new int [n] ; } _____________________ // 定义析构函数,释放动态数组空间 }; 29、下面程序三次调用同一函数 sum ,在横线处填上适当内容,使输出结果为: S=2 S=5 S=9 程序如下: #include static int s; ____________; cout<< ″ S= ″ < void main (void) { 15