初学者,大神报大腿,求教下面题目
三、下列的类声明程序片段是否有语法错误,如有请改正(每题5分, 共10分)1. class LINE {
public:
int draw();
int is_on_line(int x, int y);
private:
int start_x = 0, start_y = 0;
int end_x, end_y;
};
2. class SQUARE {
public:
void SQUARE(int x = 0, int y = 1, int len = 1);
~SQUARE();
int draw();
private:
int left_up_x, left_up_y;
int side_len;
};
四、写出程序运行结果(每题15分, 共30分)
1.
#include <iostream.h>
void fun(int a[],int n,int &s){
s=0;
for(int i=0;i<n;i+=2)
s+=a[i];
}
void main(){
int x[5]={1,2,3,4,5};
int y[10]={1,2,3,4,5,6,7,8,9,10};
int m,n;
fun(x,5,m);
cout<<"m="<<m<<endl;
fun(y,8,m);
cout<<"m="<<m<<endl;
fun(y+3,5,m);
cout<<"m="<<m<<endl;
}
2.
#include<iostream.h>
class Base
{
public:
virtual void fn()
{
cout<<"In Base class \n";
}
};
class SubClass:public Base
{
public:
virtual void fn()
{
cout<<"In SubClass \n";
}
};
void test(Base & b)
{
b.fn();
}
void main()
{
Base bc;
SubClass sc;
cout<<"Calling test(bc)\n";
test(bc);
cout<<"Calling test(sc)\n";
test(sc);
}
五、按照要求补充下面程序的空白处(每题15分,共15分)
#include <iostream.h>
class Point {
private:
int x, y;
public:
Point(int a=0, int b=0) {x=a; y=b; }
Point(Point &p);
int Getx( ) {return x; }
int Gety( ) {return y; }
void Show( ){cout<<"x="<<x<<", y="<<y<<endl; }
~point( ){ }
};
//定义复制构造函数
Point :: Point(Point &p)
{_________(1)__________}
//Point 类对象作为函数的形参
void display(__________(2)___________)
{___________(3)_______________}
//Point 类对象的引用作为函数的形参
void disp(___________(4)___________)
{__________(5)___________}
//函数的返回值为 Point 类的对象
Point fun( ) {
Point a(50, 20);
return a;
}
void main( ) {
Point a(3, 2);
Point b(a);
Point c(5, 8);
disp(b);
c=fun();
disp(c);
}
输出的结果如下:
x=3, y=2
x=50, y=20
六、设计一个平面几何中的位置类POSITION,该类至少提供移动、求到另一点距离(包括到原点距离)、求x坐标、求y坐标等操作(每题15分,共15分)