类(四、类的组合)

 
  
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 }

转载于:https://www.cnblogs.com/zourrou/archive/2011/04/16/2017651.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值