类的数据成员加前缀 m_(表示 member)

类的数据成员加前缀 m_(表示 member),这样可以避免数据成员与 成员函数的参数同名。

例如: void Object::SetValue(int width, int height) { m_width = width; m_height = height; }

 

 1 #include <iostream>
 2 #include <stdlib.h>
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 
 5 using namespace std;
 6 //定义timer类
 7 class timer{  
 8     long minutes;
 9 public:
10     //定义重载成员函数
11     settimer(char *m) { 
12         minutes = atoi(m);
13     };
14     //定义重载成员函数
15     settimer(int h, int m) { 
16         minutes = 60*h+m ;
17     };
18     //定义重载成员函数
19     settimer(double h) { 
20         minutes = (int) 60*h ;
21     };
22     long getminutes(void) { return minutes; };
23 };
24 
25 int main(int argc, char** argv) {
26       timer start,finish;   //创建对象
27 
28     //使用重载成员函数
29     start.settimer(8,30);
30     finish.settimer(9,40); 
31     cout<<"finish.settimer(9,40)-start.settimer(8,30):";
32     cout<<finish.getminutes()-start.getminutes()<<endl;  
33 
34     //使用重载成员函数
35     start.settimer(2.0);
36     finish.settimer("180"); 
37     cout<<"finish.settimer(\"180\")-start.settimer(2.0):";
38     cout<<finish.getminutes()-start.getminutes()<<endl;  
39     return 0;
40 }

 

posted @ 2018-08-03 12:23  borter  阅读(1576)  评论(0编辑  收藏  举报