C++问题
1 /***************** 2 *坐标类:Point 3 *****************/ 4 5 #ifndef POINT_H 6 #define POINT_H 7 class Point{ 8 public: 9 Point(){} 10 Point(const double& X,const double& Y):x(X),y(Y){} 11 Point(const Point&){} 12 Point& operator =(const Point&){} 13 ~Point(){} 14 double get_X() const {return x;} 15 double get_Y() const {return y;} 16 private: 17 double x,y; 18 }; 19 #endif//POINT_H 20 /***************** 21 *直线类:Line 22 *****************/ 23 #include<iostream> 24 #include"Point.h" 25 #include<cmath> 26 27 #ifndef LINE_H 28 #define LINE_H 29 30 class Line{ 31 public: 32 Line(){} 33 Line(const Point& po_1,const Point& po_2):point_1(po_1),point_2(po_2) 34 { 35 init(); 36 }//初始化函数 37 Line(const Line&){} 38 Line& operator =(const Line&){} 39 ~Line(){} 40 void init() 41 { 42 length = sqrt(pow(point_1.get_X()-point_2.get_X(),2)+ 43 pow(point_1.get_Y()-point_2.get_Y(),2)); 44 angle = atan((point_2.get_Y()-point_1.get_Y())/(point_2.get_X()-point_1.get_X())); 45 angle = angle*180/3.141592653; //弧度转换成角度。 46 } 47 void print_Line_Point_XY() const 48 { 49 std::cout<<"线段的两个端点的坐标分别是:"<<std::endl; 50 std::cout<<"("<<point_1.get_X()<<","<<point_1.get_Y()<<")"<<" " 51 <<"("<<point_2.get_X()<<","<<point_2.get_Y()<<")"<<std::endl; 52 } 53 void print_Length() const 54 { 55 std::cout<<"线段的长度是 "<<length<<std::endl; 56 } 57 void print_Angle() const 58 { 59 std::cout<<"线段与X轴的夹角是 "<<angle<<std::endl; 60 } 61 private: 62 Point point_1,point_2; 63 double length, angle; 64 }; 65 #endif//LINE_H 66 /****************************************** 67 *Rectangle 类 继承自 Line 68 *版本:2013-08-28-V1.0 编写人:zhengkailun 69 ******************************************/ 70 #include"Line.h" 71 #include"Point.h" 72 #ifndef RECTANGLE_H 73 #define RECTANGLE_H 74 75 class Rectangle : public Line{ 76 public: 77 Rectangle(){} 78 Rectangle(const Rectangle&){} 79 Rectangle& operator =(const Rectangle&){} 80 ~Rectangle(){delete line; } 81 Rectangle(const Point& pa,const Point& pb,const Point& pc,const Point& pd):Line(pa,pb) 82 { 83 84 line = new Line(pc,pd); 85 } 86 void print_Rectangle_Point() const 87 { 88 Line::print_Line_Point_XY(); 89 line->print_Line_Point_XY(); 90 } 91 92 private: 93 Line *line; 94 95 }; 96 #endif//RECTANGLE_H 97 /****************************************** 98 *Rectangle 测试程序 99 *版本:2013-08-28-V1.0 编写人:zhengkailun 100 ******************************************/ 101 102 #include<iostream> 103 #include"Point.h" 104 #include"Rectangle.h" 105 106 107 int main() 108 { 109 Point pa(1,1),pb(1,3),pc(3,1),pd(3,3); 110 //std::cout<<pa.get_X()<<std::endl; 111 Rectangle rect(pa,pb,pc,pd); 112 rect.print_Rectangle_Point(); 113 rect.print_Angle(); 114 rect.print_Length(); 115 116 return 0; 117 118 }
【要求】按以下描述和要求建立一个含有对象成员的类TeleBook,用类Record定义的数组是TeleBook的数据成员。写出所有定义成员函数的代码。执行主函数对其测试。 Record私有成员 string name; //姓名 char *telnum; //电话号码 公有成员 Record(){name=""; telnum=NULL; } string getname() ; //返回姓名 char* getnum(); //返回电话号码 void setdata(char *a,char *b);//赋值给表的末尾项 TeleBook私有成员 Record tnum[100]; //电话簿存储区 int number;//已存入数据的元素个数(与表尾下标有关) 公有成员 TeleBook(){number=0;} //电话簿类构造函数 void Insert(); //在表尾插入新数据项(输入一个新的姓名和电话号码) void Find(); //根据姓名查找电话号码,打印查找结果 void print(); //打印电话簿清单,最后打印电话号码个数 头文件包含语句和声明常变量语句为: #include <iostream> #include <string> using namespace std; const int LEN=13 ;//存储电话号码的数组长度 测试程序的主函数为: void main(){ int ch; TeleBook myfriend; ch=1; while(ch>0 && ch<4){ cout<<"1-输入, 2-查找, 3-打印清单, 4-退出, please choose!"<<endl; cin>>ch; if(ch==1) myfriend.Insert(); //插入新数据项 else if(ch==2) myfriend.Find(); //根据姓名查找电话号码 else if(ch==3) myfriend.print(); //打印清单 } } 【提示】插入新元素时需要为telnum动态分配内存,长度为常变量LEN。
/***************** *坐标类:Point *****************/ #ifndef POINT_H #define POINT_H class Point{ public: Point(){} Point(const double& X,const double& Y):x(X),y(Y){} /*Point(const Point&){}*/ Point& operator =(const Point&){} ~Point(){} double get_X() const {return x;} double get_Y() const {return y;} private: double x,y; }; #endif//POINT_H /***************** *直线类:Line *****************/ #include<iostream> #include"Point.h" #include<cmath> #ifndef LINE_H #define LINE_H class Line{ public: Line(){} Line(const Point& po_1,const Point& po_2):point_1(po_1),point_2(po_2) { init(); }//初始化函数 Line(const Line&){} Line& operator =(const Line&){} ~Line(){} void init() { length = sqrt(pow(point_1.get_X()-point_2.get_X(),2)+ pow(point_1.get_Y()-point_2.get_Y(),2)); angle = atan((point_2.get_Y()-point_1.get_Y())/(point_2.get_X()-point_1.get_X())); angle = angle*180/3.141592653; //弧度转换成角度。 } void print_Line_Point_XY() const { std::cout<<"线段的两个端点的坐标分别是:"<<std::endl; std::cout<<"("<<point_1.get_X()<<","<<point_1.get_Y()<<")"<<" " <<"("<<point_2.get_X()<<","<<point_2.get_Y()<<")"<<std::endl; } void print_Length() const { std::cout<<"线段的长度是 "<<length<<std::endl; } void print_Angle() const { std::cout<<"线段与X轴的夹角是 "<<angle<<std::endl; } private: Point point_1,point_2; double length, angle; }; #endif//LINE_H /****************************************** *Rectangle 类 继承自 Line *版本:2013-08-28-V1.0 编写人:zhengkailun ******************************************/ #include"Line.h" #include"Point.h" #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle : public Line{ public: Rectangle(){} Rectangle(const Rectangle&){} Rectangle& operator =(const Rectangle&){} ~Rectangle(){delete line; } Rectangle(const Point& pa,const Point& pb,const Point& pc,const Point& pd):Line(pa,pb) { line = new Line(pc,pd); } void print_Rectangle_Point() const { Line::print_Line_Point_XY(); line->print_Line_Point_XY(); } private: Line *line; }; #endif//RECTANGLE_H /****************************************** *Rectangle 测试程序 *版本:2013-08-28-V1.0 编写人:zhengkailun ******************************************/ #include<iostream> #include"Point.h" #include"Rectangle.h" int main() { Point pa(1,1),pb(1,3),pc(3,1),pd(3,3); //std::cout<<pa.get_X()<<std::endl; Rectangle rect(pa,pb,pc,pd); rect.print_Rectangle_Point(); rect.print_Angle(); rect.print_Length(); system("pause"); return 0; }