类(四、类的组合)

1 #include<iostream>
2 #include<cmath> //sqrt()函数的头文件;c++的类应当先定义后使用,在两个类互相引用时(循环依赖),可用前向引用声明:class ***;
3 using namespace std;
4 class Point {
5 public:
6 Point(int xx=0,int yy=0) {
7 x=xx;
8 y=yy;
9 }
10 Point(Point &p) {
11 x=p.x;
12 y=p.y;
13 cout<<"calling the copy constructor of Point..."<<endl;
14 }
15 int getX();
16 int getY();
17 private:
18 int x,y;
19 };
20 int Point::getX() {
21 return x;
22 }
23 int Point::getY() {
24 return y;
25 }
26 /*------------------------类的组合-------------------*/
27 class Line {
28 public:
29 Line(Point pp1,Point pp2);
30 Line(Line &l);
31 double getLen();
32 private:
33 Point p1,p2;
34 double len;
35 };
36 /*---------------------组合类的构造函数---------------*/
37 Line::Line(Point pp1,Point pp2):p1(pp1),p2(pp2) { //p1(pp1)表示用pp1来初始化p1,注意中间用逗号隔开;
38 double x=static_cast<double> (pp1.getX()-pp2.getX()); //static_cast为强制类型转换;
39 double y=static_cast<double> (pp1.getY()-pp2.getY());
40 len=sqrt(x*x+y*y);
41 }
42 double Line::getLen() {
43 return len;
44 }
45 /*----------------------组合类的复制构造函数-------------*/
46 Line::Line(Line &l):p1(l.p1),p2(l.p2) {
47 len=l.getLen();
48 cout<<"calling the copy constructor of Line..."<<endl;
49 }
50 int main() {
51 Point mypt1(1,1),mypt2(4,5);
52 Line line(mypt1,mypt2);
53 Line line1(line);
54 cout<<"the first line'lengh is :"<<endl;
55 cout<<line.getLen()<<endl;
56 cout<<"teh second line'lengh is :"<<endl;
57 cout<<line1.getLen()<<endl;
58 return 0;
59 }
posted @ 2011-04-16 00:19  左手写诗  阅读(1254)  评论(0编辑  收藏  举报