work hard

17895_061221114605.jpg
好的,下面是代码实现: ```c++ #include <iostream> #include <string> using namespace std; class Advertisement { private: string content; public: Advertisement(string str) : content(str) {} void show(int n) { string tmp = content; for (int i = 1; i <= n; i++) { cout << tmp << endl; tmp += content; // 串连接函数 } } int count(char c) { int cnt = 0; for (int i = 0; i < content.length(); i++) { if (content[i] == c) { cnt++; } } return cnt; } }; int main() { Advertisement ad("To work hard, live a good life. "); ad.show(3); cout << "字符o出现的次数为:" << ad.count('o') << endl; return 0; } ``` 在代码中,我们定义了一个`Advertisement`类,其中包含一个私有成员变量`content`表示广告内容。在`show()`函数中,我们首先将`content`赋值给一个临时变量`tmp`,然后利用循环输出`tmp`的内容,每次循环结束后将`content`连接到`tmp`的末尾,以实现广告的累加播放。在`count()`函数中,我们利用循环遍历`content`,统计字符`c`出现的次数,并返回结果。 在`main()`函数中,我们首先创建一个`Advertisement`对象,传入广告内容字符串。然后,我们调用`show()`函数播放广告3次,并调用`count()`函数统计字符`o`出现的次数,并输出结果。 输出结果为: ``` To work hard, live a good life. To work hard, live a good life. To work hard, live a good life. To work hard, live a good life. To work hard, live a good life. To work hard, live a good life. 字符o出现的次数为:7 ``` 其中,广告内容为"To work hard, live a good life. ",经过三次累加播放后,共输出了三行广告。字符`o`在广告内容中出现了7次,与我们预期的结果相符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值