c++类与对象练手小游戏

这是一个用C++编写的简单角色扮演文字小游戏,涉及角色、朋友和伴侣三个类的设计。代码分为角色类、伴侣类和朋友类,包含各自的实现。作者在学习过程中完成此项目,欢迎优化建议。
摘要由CSDN通过智能技术生成

一个简单的角色扮演文字小游戏(练练手)


闲着在老师给了一个简单的练习后自己拓展出来的,代码量不大,也很简单,所以拿出来分享分享

一、 简单说明:

通过创建角色,生成角色。
创建角色:输入角色的一些关键信息,使角色生成。
可以通过自己生成一些朋友,或者相亲对象,相亲对象符合基本要求可以结尾伴侣。

二、类创建

1、角色类(role)
2、朋友类(pal)
3、伴侣类(spouse)

三、代码区

1、角色类:
#pragma once
#include<string>
#include<vector>

//主角类
class Spouse;
class Pal;
class Role
{
   
public:
	Role();
	~Role();
	void setInfo();                            //创建角色
	std::string getName() const;			
	int getAge() const;
	std::string getSex() const;
	std::string description()const;
	void marrySpouse(Spouse& spouse);           //结婚
	void divorceSpouse(Spouse& spouse);	        //离婚
	void makePal(Pal& pal);				        //交朋友
	void palList(Pal& pal);					    //朋友列表
	void showPallist();							//查看朋友列表
	void severPal(Pal& pal);				    //断绝关系
	bool isAsking(const char* str);			    //是否询问问题
private:
	std:: string name;
	int age;
	int sex;
	Spouse* spouse;    //伴侣
	Pal *pal;		   //朋友
	std::vector<Pal>pals;
	static int palCount;
};

.cpp

#include<iostream>
#include<sstream>
#include "Role.h"
#include"Pal.h"
#include"Spouse.h"
using namespace std;

int Role::palCount = 0;

Role::Role(){
   
	name = "unknow";
	age = 0;
	sex = 0;
	spouse = NULL;
	pal = NULL;
}

Role::~Role(){
   

}

void Role::setInfo() {
   
	system("cls");
	string name;
	int age;
	int sex;
	cout << endl;
	cout << "——————进入角色创建模式——————" << endl;
	cout << endl;
	cout << "请输入你的名字:";
	cin >> name;
	cout << "请输入你的年龄:";
	cin >> age;
	cout << "请输入你的性别:【输入 1 为 ♂;输入 2 为 ♀】";
	cin >> sex;
	cout << "创建成功!!" << endl;
	this->name = name;
	this->age = age;
	this->sex = sex;
}

string Role::getName() const{
   
	return name;
}

int Role::getAge() const{
   
	return age;
}

string Role::getSex() const{
   
	if (sex == 1) {
   
		return "♂";
	}else if (sex == 2) {
   
		return "♀";
	}
	
}
string Role::description() const{
   
	string condition_mari;
	if (spouse) {
   
		condition_mari = "已婚";
	}else {
   
		condition_mari = "未婚";
	
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

圆圆图圈圈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值