使用C ++和修改基类的私有/受保护属性

时间:2018-05-07 21:01:33

标签: c++ oop inheritance methods derived-class

对不起,如果我在这里问过很多次问过的话。我是C ++的新手。我想知道如何使派生类继承其基类的私有属性的副本。要么是,要么能够通过基类的公共方法(如getter和setter)修改它们。

基本上,我有一个Person类,它继承自Creature类。我希望Person类型对象具有类似于Creature类的属性。我希望Creature类中的私有属性保持私有,因为我被告知只有类函数应该是公共的,而不是类变量。但是,我似乎无法在函数中调用Creature类的公共方法,并且Person类似乎没有继承它们或它们的副本。

我拒绝简单地公开私有属性的原因是因为我想学习正确的编程技巧。我不确定这种情况是否是规则的例外。

我有处理实现的cpp文件。希望这足以帮助您回答我的问题。 我的基类:

/***
File: Creature.h
Desc: Contains Creature Class Definitions.
This is the base class for the Animal, Person, and PlayerCharacter classes.

Author: LuminousNutria
Date: 5-7-18
***/

#include <string>

#ifndef _CREATURE_H_
#define _CREATURE_H_

class Creature
{
private:
    // General Information
    std::string speciesName;
    int hitpoints;
    int movement;

    // Physical Attributes
    int strength;
    int willpower;
    int intelligence;
    int leadership;

public:
    // setters
    void setSpeciesName(const std::string a);
    void setHitpoints(const int a);
    void setMovement(const int a);

    void setStrength(const int a);
    void setWillpower(const int a);
    void setIntelligence(const int a);
    void setLeadership(const int a);

    // getters
    std::string getSpeciesName();
    int getHitpoints();
    int getLoyalty();
    int getMovement();

    int getStrength();
    int getWillpower();
    int getIntelligence();
    int getLeadership();

    // modders
    void modHitpoints(const int a);
    void modMovement(const int a);

    void modStrength(const int a);
    void modWillpower(const int a);
    void modIntelligence(const int a);
    void modLeadership(const int a);
};

我的衍生课程:

/***
File: Person.h
Desc: Contains Person Class Definitions.
This is a derived class of the Creature class.

Author: LuminousNutria
Date: 5-7-18
***/


#include "Creature.h"

#include <string>

#ifndef _PERSON_H_
#define _PERSON_H_

class Person
{
protected:
    std::string personName;
    int loyalty;
    int experience;
    int level;
    int cash;

public:
    // constructors
    Person();
    Person(const std::string pName, const int loy, const int exp,
        const int lvl, const int c,
        const std::string sName, const int hp, const int mov,
        const int stre, const int will, const int intl,
        const int lead);

    // setters
    void setPersonName(std::string pName);
    void setLoyalty(const int loy);
    void setExperience(const int exp);
    void setLevel(const int lvl);
    void setCash(const int c);

    // getters
    std::string getPersonName();
    int getLoyalty();
    int getExperience();
    int getLevel();
    int getCash();

    // modders
    void modLoyalty(int a);
    void modExperience(int a);
    void modLevel(int a);
    void modCash(int a);
};

#endif

Creature Class的setSpeciesName方法的实现:

void Creature::setSpeciesName(const std::string a)
{
    speciesName = a;
}

2 个答案:

答案 0 :(得分:2)

在提供的代码中,Person不会从Creature继承,因此它不会继承Creature类的成员。

定义您的Person类,使其继承自Creature

class Person : public Creature
{
    // same as before
};

有关公共与私有继承的更多详细信息,请参阅Difference between private, public, and protected inheritance。通常你想要公共继承。

答案 1 :(得分:0)

基本上有两种解决方法: 1.明确地将基础构造函数调用到derieved构造函数中。 2.使用friend运算符来使用私有元素。

相关问题