计数器:使用类

计数器:使用类

设计一个计数器类,当建立该类的对象时其初始状态为0,考虑为计数器定义哪些成员?

#include <iostream>
using namespace std; 
class Counter {
private:
    int count; // 成员变量,用于存储计数器的当前值
public:
    // 构造函数,初始化计数器为0
    Counter() : count(0) {}
    // 成员函数,用于增加计数器的值
    void increment() {
        count++;
    }
    // 成员函数,用于获取计数器的当前值
    int getCount() const {
        return count;
    }
    // 成员函数,用于重置计数器的值为0
    void reset() {
        count = 0;
    }
};
int main() {
    // 创建Counter对象
    Counter counter;
    // 获取并输出初始计数值
    cout << "Initial count: " << counter.getCount() <<endl;
    // 增加计数
    int n;
    while(cin>>n)//利用while可以进行多数据输入输出
{ 
	for(int i=1;i<=n;i++)//利用循环显示改变输出数据
{ 
	counter.increment();
	}
    // 获取并输出当前计数值
    cout << "Current count after incrementing"<<n<<"times " << counter.getCount() << endl;
    // 重置计数
    counter.reset();
    // 获取并输出重置后的计数值
   cout << "Count after reset: " << counter.getCount() << endl;
	}
    return 0;
}

 

计数器问题的C++代码实现

  1. 基本计数器实现

    • 创建一个类Counter,其中包含一个私有整型成员变量count作为计数器的基础值。
    class Counter {
    private:
        int count;
    public:
        Counter() : count(0) {}  // 初始化计数器为0
        void increment() { count++; }  // 增加计数
        void decrement() { if (count > 0) count--; }  // 减少计数(可以增加边界检查)
        int getCount() const { return count; }  // 获取当前计数值
    };
  2. 倒计时计数器

    • 倒计时计数器可以在基础计数器上扩展,增加一个设定目标值的功能,并在计数到0时触发某种事件(例如,函数回调)。
    class CountdownTimer : public Counter {
    private:
        int target;
        std::function<void()> onFinishCallback;
    public:
        CountdownTimer(int t, std::function<void()> callback) : target(t), onFinishCallback(callback) {
            count = target;
        }
        void tick() {
            increment();  // 模拟时间流逝,每tick一次减一
            if (count == 0) {
                onFinishCallback();
            }
        }
    };
  3. 线程安全计数器

    • 如果计数器要在多线程环境下使用,需要添加互斥锁或其他同步机制来保证线程安全。
    #include <mutex>
    class ThreadSafeCounter {
    private:
        std::mutex mtx;
        int count;
    public:
        ThreadSafeCounter() : count(0) {}
        void increment() {
            std::lock_guard<std::mutex> lock(mtx);
            count++;
        }
        void decrement() {
            std::lock_guard<std::mutex> lock(mtx);
            if (count > 0) count--;
        }
        int getCount() const {
            std::lock_guard<std::mutex> lock(mtx);
            return count;
        }
    };
  4. 高级功能

    计数器可以扩展更多的功能,如限次数计数、周期计数、条件计数等,根据实际应用场景进行设计和实现。
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的QT程序计数器,每1秒计数一次,每5秒停止计数: ```cpp #include <QtWidgets/QApplication> #include <QtWidgets/QWidget> #include <QtWidgets/QLabel> #include <QtCore/QTimer> class CounterWidget : public QWidget { public: CounterWidget(QWidget* parent = nullptr) : QWidget(parent) { countLabel = new QLabel("0", this); countLabel->setAlignment(Qt::AlignCenter); countLabel->setGeometry(0, 0, width(), height()); count = 0; countTimer = new QTimer(this); countTimer->setInterval(1000); connect(countTimer, &QTimer::timeout, this, &CounterWidget::onCountTimerTimeout); stopTimer = new QTimer(this); stopTimer->setInterval(5000); connect(stopTimer, &QTimer::timeout, this, &CounterWidget::onStopTimerTimeout); stopTimer->start(); } private: QLabel* countLabel; QTimer* countTimer; QTimer* stopTimer; int count; void onCountTimerTimeout() { count++; countLabel->setText(QString::number(count)); } void onStopTimerTimeout() { countTimer->stop(); stopTimer->stop(); } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); CounterWidget counterWidget; counterWidget.show(); return app.exec(); } ``` 在这个程序中,我们创建了一个计数器窗口,其中包含一个用于显示计数的标签。我们使用两个定时器,一个用于每秒钟增加计数器,另一个用于在5秒后停止计数器。我们使用`QTimer`来实现这两个定时器,使用`connect`函数来连接定时器的超时信号和我们自己定义的槽函数。在计数器超时槽函数中,我们增加计数器并更新标签。在停止计时器超时槽函数中,我们停止计数器定时器和停止定时器。最后,我们在`main`函数中创建一个应用程序对象,创建计数器窗口并显示它,然后启动应用程序事件循环。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值