博客
关于我
21.04.01实现计算器类的成员函数
阅读量:531 次
发布时间:2019-03-08

本文共 982 字,大约阅读时间需要 3 分钟。

#include 
using namespace std;class counter {public: counter(int number); // 构造函数 void increment(); // 给value原值加1 void decrement(); // 给value原值减1 void setvalue(int V); // 设置计数器值 int getvalue(); // 取得计数器值 void print(); // 显示计数值private: int value; // 数据成员};// 计数器类的构造函数实现counter::counter(int number) { value = number;}// 给value原值加1的成员函数实现void counter::increment() { value += 1;}// 给value原值减1的成员函数实现void counter::decrement() { value -= 1;}// 设置计数器值的成员函数实现void counter::setvalue(int V) { value = V;}// 取得计数器值的成员函数实现int counter::getvalue() { return value;}// 显示计数值的成员函数实现void counter::print() { cout << value << endl;}int main() { // 定义counter类的几个对象并调用有关成员函数 counter cntr(3); // 测试成员函数 cntr.increment(); cntr.print(); cntr.decrement(); cntr.print(); cntr.setvalue(7); cout << cntr.getvalue() << endl; return 0;}

以上代码定义了一个简单的计数器类及其相关成员函数,并通过主函数进行了测试。该类通过构造函数初始化计数器值,提供了增量、减量、设置和获取值以及打印功能,便于对计数器进行操作和监控。

转载地址:http://mlfiz.baihongyu.com/

你可能感兴趣的文章
Pinia:$patch的使用场景
查看>>
Pinia:$subscribe()的使用场景
查看>>
Pinpoint对Kubernetes关键业务模块进行全链路监控
查看>>
Pinterest 大规模缓存集群的架构剖析
查看>>
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>
PIP
查看>>
pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
查看>>
pip install mysqlclient报错
查看>>
pip install 出现报asciii码错误的解决
查看>>
pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
查看>>
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip/pip3更换国内源
查看>>