找考题网-背景图
问答题

【说明】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();

【参考答案】

[解析]
(A)Point &p
Point拷贝构造函数的形参必须是Point对象的引用。
(B)Linc::Line(int x,int y, int k):Point(x,y)
Line的构造函数必须先调用Point构造函数构造Line的基类Point。......

(↓↓↓ 点击‘点击查看答案’看完整答案 ↓↓↓)
热门试题

问答题【说明】本程序从正文文件text.in中读入一篇英文短文,统计该短文中不同单词及出现次数,并按词典编辑顺序将单词及出现次数输出到正文文件word.out中。 程序用一棵有序二叉树存储这些单词及其出现的次数,边读入边建立,然后中序遍历该二叉树,将遍历经过的二叉树上的结点的内容输出。 #include <stdio.h> #include <malloc.h> #include <ctype.h> #include <string.h> #define INF text.in #define OUTF wotd.out typedef struct treenode char *word; int count; struct treenode *left,*right; BNODE int getword (FILE *fpt,char *word) char c; c=fgetc (fpt); if ( c=EOF) return 0; while(!(tolower(c)>=’a’ && tolower(c)<=’z’)) c=fgetc (fpt); if ( c==EOF) return 0; *跳过单词间的所有非字母字符* while (tolower (c)>=’a’ && tolower (c)<=’z’) *word++=c; c=fgetc (fpt); *word=’ 0’; return 1; void binary_tree(BNODE **t,char *word) BNODE *ptr,*p;int compres; P=NULL; (1) ; while (ptr) *寻找插入位置* compres=strcmp (word, (2) ); *保存当前比较结果* if (!compres) (3) ;return; else (4) ; ptr=compres>0 ptr->right:ptr->left; ptr= (BNODE*) malloc (sizeof (BNODE)) ; ptr->left = ptr->right = NULL; ptr->word= (char*) malloc (strlen (word) +1) ; strcpy (ptr->word, word); ptr->count - 1; if (p==NULL) (5) ; else if (compres > 0) p->right = ptr; else p->left = ptr; void midorder (FILE **fpt, BNODE *t) if (t==NULL) return; midorder (fpt, t->left); fprintf (fpt, %s %d n , t->word, t->count) midorder (fpt, t->right); void main() FILE *fpt; char word[40]; BNODE *root=NULL; if ((fpt=fopen (INF, r )) ==NULL) printf ( Can’t open file %s n , INF ) return; while (getword (fpt, word) ==1 ) binary_tree (&root, word ); fclose (fpt); fpt = fopen (OUTF, w ); if (fpt==NULL) printf ( Can’t open file %s n , OUTF) return; midorder (fpt, root); fclose(fpt);