使用Accessors设置用户名 [英] Using Accessors to set a Users name
问题描述
我有一个继承自Bass类的Character类。在运行时,我希望用户输入字符名称。我想使用SetName访问器来完成此操作。我尝试过多种方法让它发挥作用。我创造了一个新的角色英雄;并尝试传递Hero.SetName(cin,sName);
哪个不起作用。调试器告诉我sName没有在main.cpp文件的范围内声明。我的问题是如何使用Set访问器获取用户名并保持封装的主要内容。我可以为My heros名称传递一个全局变量,但是我失去了封装的好处。我的Main.cpp文件和Charecter类发布在下面谢谢。
#include < iostream >
#include < string >
#include < cstdlib >
#include < cmath >
#include < ctime >
#includeCharacterClass.h使用namespace std
;
int main()
{
srand(time(0));
CharecterClass Hero;
cout < < 输入 您的 名称 挑战者: ;
< span class =code-attribute> // system(cls);
Hero.SetName(cin, sName);
Hero.SetHealth(5);
< span class =code-attribute>
Hero.SetAttack((rand()% 12) + 1);
cout << Hero.GetName() << endl;
cout < < Hero.GetHealth() << endl;
cout << Hero.GetAttack() << ; endl;
return 0;
}
#ifndef CHARACTERCLASS_H
#define CHARACTERCLASS_H
#include BaseClass。 h
#include< iostream>
class CharacterClass: public BaseClass
{
public :
CharacterClass();
virtual ~CharacterClass();
int GetArmor(){ return m_CharacterArmor; }
int GetAttack(){ return m_CharacterAttack; }
int GetDamage(){ return m_CharacterDamage; }
void SetArmor( int nCharacterArmor){m_CharacterArmor = nCharacterArmor; }
void SetAttack( int nCharacterAttack){m_CharacterAttack = nCharacterAttack; }
void SetDamage( int nCharacterDamage){m_CharacterDamage = nCharacterDamage; }
受保护:
私人:
int m_CharacterArmor;
int m_CharacterAttack;
int m_CharacterDamage;
};
#endif // CHARACTERCLASS_H
#ifndef BASECLASS_H
#define BASECLASS_H
#include< iostream>
class BaseClass
{
public :
BaseClass();
virtual ~BaseClass();
std :: string GetName(){ return m_Name; }
int GetHealth(){ return m_Health; }
void SetName(std :: string sName) {m_Name = sName; }
void SetHealth( int nHealth){m_Health = nHealth; }
受保护:
私人:
std :: string m_Name;
int m_Health;
};
#endif // BASECLASS_H
要使用我的访问器设置名称,我创建了一个带有本地字符串变量的函数,我将返回Hero.SetName()。这对我保持我的课程封装起来很有用。
string HeroName()
{
string sTemp;
cout<< 输入你的名字挑战者:;
cin>> STEMP;
返回sTemp;
}
这是它的实施
Hero .SetName(HeroName());
I Have a Character class that inherits from a Bass class. At run time I want the user to enter the characters name. I want to do this using the SetName Accessor. I''ve tried multiple ways to get this to work. I created a new Character Hero; and tried to pass Hero.SetName(cin, sName);
Which did not work. The debugger tells me that sName is not declared in the scope of my main.cpp file. My question is is how do I go about getting a users name using the Set accessor and keeping with the pricipal of encapsulation. I can pass a global variable for My heros name but I lose the benefit of encapsulation. My Main.cpp file and Charecter class are posted below thanks.
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "CharacterClass.h"
using namespace std;
int main()
{
srand(time(0));
CharecterClass Hero;
cout << "Enter your name Challenger: ";
//system("cls");
Hero.SetName(cin, sName);
Hero.SetHealth(5);
Hero.SetAttack((rand()% 12) + 1);
cout << Hero.GetName() << endl;
cout << Hero.GetHealth() << endl;
cout << Hero.GetAttack() << endl;
return 0;
}
#ifndef CHARACTERCLASS_H
#define CHARACTERCLASS_H
#include "BaseClass.h"
#include <iostream>
class CharacterClass : public BaseClass
{
public:
CharacterClass();
virtual ~CharacterClass();
int GetArmor() { return m_CharacterArmor; }
int GetAttack() { return m_CharacterAttack; }
int GetDamage() { return m_CharacterDamage; }
void SetArmor(int nCharacterArmor) { m_CharacterArmor = nCharacterArmor; }
void SetAttack(int nCharacterAttack) { m_CharacterAttack = nCharacterAttack; }
void SetDamage(int nCharacterDamage) { m_CharacterDamage = nCharacterDamage; }
protected:
private:
int m_CharacterArmor;
int m_CharacterAttack;
int m_CharacterDamage;
};
#endif // CHARACTERCLASS_H
#ifndef BASECLASS_H
#define BASECLASS_H
#include <iostream>
class BaseClass
{
public:
BaseClass();
virtual ~BaseClass();
std::string GetName() { return m_Name; }
int GetHealth() { return m_Health; }
void SetName(std::string sName) { m_Name = sName; }
void SetHealth(int nHealth) { m_Health = nHealth; }
protected:
private:
std::string m_Name;
int m_Health;
};
#endif // BASECLASS_H
To set the name using my accessor I created a function with a local string variable which I return to Hero.SetName(). This worked for me keeping my class encapsulated.
string HeroName()
{
string sTemp;
cout << "Enter your name challenger:";
cin >> sTemp;
return sTemp;
}
and here is it''s implementation
Hero.SetName(HeroName());
这篇关于使用Accessors设置用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!