修改上面的代码并追加隐藏成就和解锁成就
时间: 2024-03-10 21:43:33 浏览: 64
好的,您可以修改Achievement类,添加隐藏成就和解锁成就的成员变量和函数,并在printAchievement函数中打印它们。以下是修改后的代码示例:
```
#include <iostream>
#include <string>
using namespace std;
class Achievement {
public:
Achievement(string name, int score, string description, bool isHidden) // 构造函数
{
this->name = name;
this->score = score;
this->description = description;
this->isHidden = isHidden;
this->isUnlocked = false;
}
void printAchievement() // 成就界面的打印函数
{
cout << "--------------------------" << endl;
cout << " Achievement Unlocked" << endl;
cout << "--------------------------" << endl;
cout << "Name: " << name << endl;
cout << "Score: " << score << endl;
cout << "Description: " << description << endl;
if (isHidden) {
cout << "*** Hidden Achievement ***" << endl;
}
if (isUnlocked) {
cout << "*** Unlocked ***" << endl;
} else {
cout << "*** Locked ***" << endl;
}
}
void unlockAchievement() // 解锁成就的函数
{
isUnlocked = true;
}
private:
string name;
int score;
string description;
bool isHidden;
bool isUnlocked;
};
int main()
{
Achievement achievement1("First Achievement", 100, "Complete the first level.", false);
Achievement achievement2("Second Achievement", 200, "Collect all the items in level 2.", true);
achievement1.printAchievement();
achievement2.printAchievement();
achievement1.unlockAchievement();
achievement1.printAchievement();
return 0;
}
```
在此示例中,我们将构造函数修改为接受一个新的bool参数isHidden,以指示成就是否为隐藏成就。我们还添加了一个bool变量isUnlocked,表示成就是否已解锁。我们修改了printAchievement函数,以打印隐藏成就和解锁成就的状态。我们还添加了一个unlockAchievement函数,用于将成就标记为已解锁。在主函数中,我们创建了两个Achievement对象,并在打印它们的成就界面后解锁了第一个成就并再次打印了成就界面,以显示解锁状态的变化。
阅读全文