博客
关于我
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/

你可能感兴趣的文章
svn访问报错500
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.system 在 Python 中不起作用
查看>>