继承以及保护的案例
1 /* 继承以及保护的案例 */
2
3 #include<iostream>
4
5 using namespace std;
6
7 const double PI = 3.141592657;
8
9 // 点
10 class point
11 {
12 public:
13
14 point(int a,int b):x(a),y(b)
15 {
16
17 }
18
19 void setxy(int a,int b)
20 {
21 x = a;
22 y = b;
23 }
24
25 int getx() const
26 {
27 return x;
28 }
29
30 int gety() const
31 {
32 return y;
33 }
34
35 protected:// 保护成员 外部不可访问 内部可以 继承的可以
36 int x;
37 int y;
38 };
39
40
41 class circle : public point
42 {
43 public:
44 circle(double length,int a,int b):point(a,b),length(length)
45 {
46
47 }
48
49 double getlength() const
50 {
51 return length;
52 }
53
54 void setlength(double db)
55 {
56 length = db;
57 }
58
59 protected:
60 double length;// 半径
61 };
62
63 class cylinder : public circle
64 {
65 public:
66 cylinder(double high,double length,int a,int b):circle(length,a,b),high(high)
67 {
68
69 }
70
71 double gethigh() const
72 {
73 return high;
74 }
75
76 void sethigh(double db)
77 {
78 high = db;
79 }
80
81 double tiji()
82 {
83 return (this->length)*(this->length)*PI*high;
84 }
85
86 double mianji()
87 {
88 return (this->length)*(this->length)*PI*2 + (this->length)*PI*2 *high;
89 }
90 protected:
91 double high;
92 };
93
94 void main()
95 {
96
97 cylinder cy1(100,100,450,560);
98 cin.get();
99 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-09 09:42 Dragon-wuxl 阅读(107) 评论(0) 收藏 举报
浙公网安备 33010602011771号