如何从派生类中的方法调用基类中的方法?

时间:2014-04-16 23:08:58

标签: c++ class inheritance

我有一个名为sorcerer的基类,我试图在女巫或巫师类中调用函数takeStrength。我该怎么做呢?我以为它只是sorcerer::takeStrength(s.sorcerer);但我无法让它发挥作用。编译错误无效使用' sorcerer :: sorcerer'当我尝试使用sorcerer::takeStrength(s.sorcerer);从女巫类的战斗功能中调用takeStrength时。我认为它就像我调用函数的语法一样简单,但在尝试不同的方法之后我不再确定了。提前感谢您的帮助!

sorcerer.h:

class sorcerer
// models a sorcerer
{
public:
    sorcerer();
    // default constructor, sets name to empty string and strength to 0

    sorcerer(string initName, double initStrength);
    // constructor; sets name to the value of initName; if initStrength > 0
    // strength is set to the value of initStrength, otherwise strength is 
    // set to .1

    string getName();
    // returns the value of name
    // postcondition: the value of name is returned

    double getStrength();
    // returns the value of strength
    // postcondition: the value of strength is returned

    void   takeStrength(sorcerer & s);    
    // takes half the strength of the sorcerer represented by parameter s and 
    // adds it to strength
    // precondition:  s is passed a valid sorcerer object or an object from a  
    //                class that inherits class sorcerer
    // postcondition: strength is increased by half of the strength of the 
    //                object passed to s; the object passed to s has its 
    //                strength reduced by half

    virtual void fight(sorcerer & s) = 0;
    // pure virtual function, makes class sorcerer abstract so that class 
    // sorcerer must be inherited and function fight must be implemented
    //
    // postcondition: a fight has occurred between the calling sorcerer and the
    //                passed sorcerer

private:
    double strength;  // sorcerer strength; 0 or greater
    string name;      // sorcerer name
};

witch.h:

class witch : public sorcerer
// inherits class sorcerer in order to model a witch
{
public:
    witch();
    // default constructor
//    // calls default constructor of class sorcerer

    witch(string name, double initStrength);
    // constructor
    // calls parametrized constructor of classs sorcerer to initialize strength
    // and name

    void fight(sorcerer & s);
    // fights a sorcerer (an instance object of any class that inherits class 
    // sourcerer) on the witch's home turf
    //
    // postcondition:  A fight has occured between the witch and the passed
    //                 sourcerer.  A tie halts the function.  If there is a 
    //                 winner, the winner's strength is increased by 1/2 of the 
    //                 strength of the loser and the loser's strenght is 
    //                 decreased by half.
};

注意:wizard.h与女巫相同,但有一些细微差别

1 个答案:

答案 0 :(得分:0)

您没有显示您尝试拨打takeStrength()的位置,但我认为它位于您的witch::fight()wizard::fight()实施内部,在这种情况下您可以将其称为根据需要:

void witch::fight(sorcerer & s)
{
    ...
    // taking strength away from the other sorcerer
    // and giving it to this sorcerer
    takeStrength(s);
    ...
    // taking strength away from this sorcerer
    // and giving it to the other sorcerer
    s.takeStrength(*this);
    ...
}

void wizard::fight(sorcerer & s)
{
    ...
    // taking strength away from the other sorcerer
    // and giving it to this sorcerer
    takeStrength(s);
    ...
    // taking strength away from this sorcerer
    // and giving it to the other sorcerer
    s.takeStrength(*this);
    ...
}