productclassincrement.cpp

来自「C++ sample code, for the book: C++ blac」· C++ 代码 · 共 47 行

CPP
47
字号
#include <iostream>
#include <string>
using namespace std;

class product 
{     
    int number;        
public:         
    string name;              
    int get_number(){return number;}
    void set_number(int n){number = n;}
    product(string text, int count);
    product operator++(void);
    product operator++(int);
};

product::product(string text, int count)
{
    name = text;
    number = count;
}

product product::operator++(void)
{
    number++;
    return *this;           
}

product product::operator++(int)
{
    number++;
    return *this;           
}

main()
{
    product oranges("oranges", 200);

    oranges++;

    cout << "Number of " << oranges.name << ": " << oranges.get_number() << endl;

    return 0;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?