试题与答案

【说明】Point是平面坐标系上的点类,Line是从Point派生出来的直线类。 #

题型:问答题

题目:

【说明】Point是平面坐标系上的点类,Line是从Point派生出来的直线类。 #include <iostream.h> class Point {public:Point (int x, int y) ;Point (Point &p) ;~Point();void set (double x, double y) ;void print(); private:double X,Y; }; Point::Point (int x, int y) //Point 构造函数 {X=x; Y=y; } Point::Point ( (1) ) //Point 拷贝构造函数 {X=p.X; Y=p.Y;} void Point::set (double x, double y) {X=x; Y=y; } void Point::print() {cout<<’ (’<<X<<","<<Y<<") "<<endl; } Point::~Point() {cout<<"Point 的析构函数被调用! "<<endl; class Line: public Point {public: Line (int x, int y, int k) ; Line (Line &s) ; ~Line(); void set (double x, double y, double k) void print(); private:double K; }; (2) //Line 构造函数实现 { K=k;} (3) //Line 拷贝构造函数实现 {K=s.K;} void Line::set (double x, double y, double k) { (4) ; K=k; } void Line::print() {cout<<" 直线经过点"; (5) ; cout<<"斜率为: k="<<K<<endl; } Line: :~Line() {cout<<"Line 析构函数被调用! "<<endl; } void main() {Line 11 (1,1,2) ; 11 .print(); Linel2 (11) ; 12.set (3,2,1) ; 12.print(); }

答案:

参考答案:

解析: (1)Point &p Point拷贝构造函数的形参必须是Point对象的引用。 (2)Linc::Line(int x,int y, int k):Point(x,y) Line的构造函数必须先调用Point构造函数构造Line的基类Point。 (3)Line::Line(Line &s):Point(s) Line的拷贝构造函数必须先调用Point拷贝构造函数来构造并复制Line对象的基类 Point部分。 (4)Point::set(x, y) Line的set成员函数必须通过Point的set成员函数才能访问基类的私有成员。而且在 set名前必须加成员名限定Point::,以区别Line的set函数。 (5)Point::print() Line的print成员函数必须通过Point的print成员函数才能访问打印基类的私有成员。而且在print名前必须加成员名限定Point::,以区别Line的print函数。

试题推荐
微信公众账号搜索答案