• 类的数据成员加前缀 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 }
  • 相关阅读:
    exit()和_exit()函数(转)
    C语言struct内存占用问题 (转)
    C语言中memset函数(转)
    STDIN_FILENO与stdin区别(转)
    stderr,strerror,errno,perror,论坛大神的回答!
    C++ 函数模版
    C++ 内置函数
    offsetof
    LockSupportDemo
    读写锁,await和signa
  • 原文地址:https://www.cnblogs.com/borter/p/9413443.html
Copyright © 2020-2023  润新知