范磊 C++ 第6章 面向对象

  1 // section_6.cpp : Defines the entry point for the console application.
  2 //范磊C++ 第6章 面向对象
  3 
  4 #include "stdafx.h"
  5 #include "iostream"
  6 
  7 
  8          
  9 //6.3.7 对象只能调用类中存在的方法(函数)
 10 using std :: cout;
 11 class Human
 12 {
 13 public:
 14     void walk()
 15     {
 16         cout << "我是人,我会走路.
";
 17     }
 18     void speak()
 19     {
 20         cout << "我是人,我会说话.
";
 21     }
 22     void run()
 23     {
 24         cout << "我是人,我会跑步.
";
 25     }
 26     void eat()
 27     {
 28         cout << "我是人,我会吃饭.
";
 29     }
 30     void sleep()
 31     {
 32         cout << "我是人,我会睡觉.
";
 33     }
 34 };
 35 //这里的成员函数的声明和定义都放在类里面.
 36 
 37 void fun1()
 38 {
 39     Human Tom;        //Tom 是 Human 这个类创建的对象.
 40     Tom.eat();        //Tom这个对象,可以使用类Human里面的void eat() 这public个函数
 41     Tom.run();      //Tom这个对象,可以使用类Human里面的void run() 这个公public函数
 42     Tom.sleep();    //Tom这个对象,可以使用类Human里面的void run() 这个公public函数
 43     Tom.speak();    //Tom这个对象,可以使用类Human里面的void speak() 这个public函数
 44     Tom.walk();        //Tom这个对象,可以使用类Human里面的void walk() 这个public函数
 45     
 46 }
 47 
 48 //---------------------------------------------------
 49 //6.4 公有_publi
 50 class Human1
 51 {
 52 public:
 53     int weight;
 54 };
 55 //这里的成员函数的声明和定义都放在类里面.
 56 
 57 void fun2()
 58 {
 59     Human1 Tom;
 60     Tom.weight = 5;        //因为int weight 是public的.所以在 类外面也能使用.
 61     cout << "Tom的体重为" << Tom.weight << ".
";
 62 
 63     Human1 Mike;
 64     Mike.weight = 6;
 65     cout << "Mike的体重为" << Mike.weight << ".
";
 66 }    
 67 
 68 //---------------------------------------------------
 69 //6.5 私有_private
 70 class Human2
 71 {
 72 public:
 73     void set(int w)        //这个public函数存在的目的就是为了能让外界改变 weight 这个私有变量值.
 74     {
 75         weight = w;
 76     }
 77     int print()            //这个public函数存在的目的就是为了能让外界打印出 weight 这个私有变量的值.
 78     {
 79         return weight;
 80     }
 81 private:
 82     int weight;        //说明了 weight 这个变量是私有的.只有类里面的函数才能改变或者使用 weight.
 83 };                    //所以要上面建立了 void set(int w) 和 int print() 这两个 public function.
 84 //这里的成员函数的声明和定义都放在类里面.
 85 
 86 void fun3()
 87 {
 88     Human2 Tom;
 89     Tom.set(10);    //通过函数Tom.set(10)改变了 weight 的值为 10. 
 90     cout << "Tom的体重为" << Tom.print() << ".
";        //通过函数Tom.print()获得了 weight 的值.
 91 
 92     Human2 Mike;
 93     Mike.set(20);    //通过函数Mike.set(20)改变了 weight 的值为 20. 
 94     cout << "Mike的体重为" << Mike.print() << ".
";    //通过函数Mike.print()获得了 weight 的值
 95 }
 96 
 97 //---------------------------------------------------
 98 //6.5.1 私有_private
 99 class Human3
100 {
101 private:
102     int weight;
103 public:
104     void set(int w)        //直接改变set函数的内容,就可以改变了所有对象里面set的内容,修改程序很方便.
105     {
106         if(w > 0 && w <100)
107         {
108             weight = w;
109         }
110         else
111         {
112             cout <<"请输入一个大于0而小于100的数字,否则默认为0. 
";
113             weight  = 0;
114         }
115     }
116     int print()
117     {
118         return weight;
119     }
120 };
121 //这里的成员函数的声明和定义都放在类里面.
122 
123 void fun4()
124 {
125     Human3 Tom;
126     Tom.set(123);
127     cout << "Tom 的体重为: " << Tom.print() << "
" ;
128 
129     Human3 Mike;
130     Mike.set(345);
131     cout << "Mike 的体重为: " << Mike.print() << "
" ; 
132 }
133 
134 //---------------------------------------------------
135 //6.6 成员函数的声明和定义
136 class Human4
137 {
138 private:
139     int weight;
140 public:
141     void set(int w);        //只对成员函数void set(int w)进行声明.没有进行定义.
142     int print()                //对成员函数int print()进行声明同时也进行定义.
143     {
144         return weight;
145     }
146 };
147 
148 void Human4::set(int w)        //void set(int w) 在这里进行定义. 注意:
149 {                            //1.因void set(int w) 属于类Human4的成员,所属 Human4,要在 Human4 后面加区域符 :: 
150     if(w > 0 && w < 100)    //2.注意格式是: 函数类型 + 类名字 + :: + 函数名字
151     {
152         weight = w;
153     }
154     else
155     {
156         cout <<"请输入一个大于0而小于100的数字,否则默认为0. 
";
157         weight  = 0;
158     }
159 }
160 
161 
162 void fun5()
163 {
164     Human4 Tom;
165     Tom.set(111);
166     cout << "Tom 的体重为: " << Tom.print() << "
" ;
167 
168     Human4 Mike;
169     Mike.set(-05);
170     cout << "Mike 的体重为: " << Mike.print() << "
" ; 
171 
172 }
173 
174 //---------------------------------------------------
175 //6.10 构造函数
176 class rectangle
177 {
178 private:
179     int length;
180     int width;
181 public:
182     rectangle(int l, int w)        //类 rectangle 的构造函数,创建rectangle的对象时,会自动调用该函数
183     {
184         length = l;
185         width = w;
186     }
187     int area()
188     {
189         return length * width ;
190     }
191 };
192 void fun6()
193 {
194     rectangle a(3,4);            //创建rectangle的对象a,在创建a同时调用构造函数 rectangle(int l, int w)
195     cout << a.area() << "
" ;
196 }
197 
198 //---------------------------------------------------
199 //6.11 默认构造函数
200 //当我们创造了一个类时不提供构造函数,系统会自动创建一个构造函数
201 //系统生成的构造函数没有参数,不执行功能,只用于构造一个对象 如:rectangle()  { }
202 class rectangle1
203 {
204 private:
205     int length;
206     int width;
207 public:
208     rectangle1()
209     {
210         cout << "构造一个长方形.
" ;
211     }
212     rectangle1(int l, int w)
213     {
214         length = l;
215         width = w;
216     }
217     int area()
218     {
219         return length * width ;
220     }
221 };
222 
223 void fun7()
224 {
225     rectangle1 a(5,6);            
226     rectangle1 b;        
227     cout << a.area() << "
" ;
228 }
229 
230 //---------------------------------------------------
231 //6.12 析构函数(类前面 + ~)
232 //析构函数作用:清楚由构造函数创建的内存. 用于对象被销毁后清除所占用的内存空间.
233 //一个类只有一个析构函数
234 class A
235 {
236 public:
237     A();        //声明构造函数
238     ~A();        //声明析构函数
239 };
240 A::A()        //定义构造函数
241 {
242     cout << "构造函数执行完毕." <<"
" ;  
243 }
244 A::~A()        //定义析构函数
245 {
246     cout << "析构函数执行完毕." << "
" ;
247 }
248 
249 void fun8()
250 {
251     A a;
252 }
253 
254 //---------------------------------------------------
255 //6.113 析构对象数组
256 class A1
257 {
258 public:
259     A1();
260     ~A1();
261 };
262 A1::A1()
263 {
264     cout << "构造函数执行完毕." << "
" ; 
265 }
266 A1::~A1()
267 {
268     cout << "析构函数执行完毕." << "
" ;
269 }
270 
271 void fun9()
272 {
273     A1 a[2];    //分别执行了2次构造函数以后再执行了2次析构函数.因为这里建立了2个对象.
274 }
275 
276 
277 
278 int main(int argc, char* argv[])
279 {
280     //fun1();        //6.3.7 独享只能调用类中存在的方法
281     //fun2();        //6.4 公有_public
282     //fun3();        //6.5 私有_private
283     //fun4();        //6.5.1 私有_private
284     //fun5();        //6.6 成员函数的声明和定义
285     //fun6();        //6.10 构造函数
286     //fun7();        //6.11 默认构造函数
287     //fun8();        //6.12 析构函数
288     fun9();            //6.113 析构对象数组
289 
290     return 0;
291 }
原文地址:https://www.cnblogs.com/adalovelace/p/4011814.html