如何在接口类中创建超类对象?

时间:2019-05-09 15:09:56

标签: c++ oop inheritance interface

为我提供了一个接口(game_manager),我可以编写该方法的主体,但不能向其添加任何内容。我需要从界面获取我的超类输入。

我有一个名为Game的超类,它是从game_manager继承的。

game_manager.h :(界面)

class game_manager
{
public:
    void add_team_A_goalkeeper(int stamina, int dribble, int pass, int defend);
    void add_team_A_defender(int stamina, int dribble, int pass, int defend);
    void add_team_A_striker(int stamina, int dribble, int pass, int defend);
    void add_team_B_goalkeeper(int stamina, int dribble, int pass, int defend);
    void add_team_B_defender(int stamina, int dribble, int pass, int defend);
    void add_team_B_striker(int stamina, int dribble, int pass, int defend);
    void play();
    string get_result();
private:

};

类游戏:

class Game : public game_manager
{
private:
    bool Awin;
    bool Bwin;

    std::string result;

    GoalKeeper AGoalKeeper;
    Defender ADefender;
    Striker AStriker;
    GoalKeeper BGoalKeeper;
    Defender BDefender;
    Striker BStriker;

public:

    void add_team_A_goalkeeper(int stamina, int dribble, int pass, int defend);
    void add_team_A_defender(int stamina, int dribble, int pass, int defend);
    void add_team_A_striker(int stamina, int dribble, int pass, int defend);
    void add_team_B_goalkeeper(int stamina, int dribble, int pass, int defend);
    void add_team_B_defender(int stamina, int dribble, int pass, int defend);
    void add_team_B_striker(int stamina, int dribble, int pass, int defend);
    void play();
    std::string get_result();
    void handle_encounter();
};

主要:

    #include "game_manager.h"

    int main()
    {
        game_manager game = game_manager();
        game.add_team_A_goalkeeper(100, 10, 20, 65);
        game.add_team_A_defender(100, 20, 60, 80);
        game.add_team_A_striker(100, 70, 50, 30);
        game.add_team_B_goalkeeper(100, 50, 40, 50);
        game.add_team_B_defender(100, 85, 20, 90);
        game.add_team_B_striker(100, 50, 20, 10);
        game.play();
        std::cout << game.get_result();
    }

当我创建一个game_manager对象时,我希望它从Game中创建一个对象,并从Game类中调用重写的方法。 我不知道如何实现。

现在我收到此错误:

 /tmp/ccN3ZkwD.o: In function `main':
game.cpp:(.text+0x1635): undefined reference to `game_manager::add_team_A_goalkeeper(int, int, int, int)'
collect2: error: ld returned 1 exit status

3 个答案:

答案 0 :(得分:1)

您应该为类game_manager中的方法定义一个伪主体,以避免出现未定义的引用错误。可能是这样的:

void game_manager::add_team_A_goalkeeper(int stamina, int dribble, int pass, int defend) {}

void game_manager::add_team_A_defender(int stamina, int dribble, int pass, int defend) {}

void game_manager::add_team_A_striker(int stamina, int dribble, int pass, int defend) {}

void game_manager::add_team_B_goalkeeper(int stamina, int dribble, int pass, int defend) {}

void game_manager::add_team_B_defender(int stamina, int dribble, int pass, int defend) {}

void game_manager::add_team_B_striker(int stamina, int dribble, int pass, int defend) {}

如@KorelK所述,而不是创建game_manager对象,您应该创建一个Game对象并将其存储在game_manager变量中:

game_manager game = Game();

但是,当您调用game.add_team_A_goalkeeper(100, 10, 20, 65);或其他game_manager方法时,它将执行game_manager的伪方法。为了调用Game的方法,您需要将game_manager对象存储到Game指针,并从该指针调用函数:

game_manager game = Game();
Game * game_ptr = (Game *) &game;
game_ptr->add_team_A_goalkeeper(100, 10, 20, 65);

这是一个相当棘手的方法,但是由于分配要求game变量类型为game_manager,所以这是我能想到的唯一方法。 :)

答案 1 :(得分:1)

我认为他们希望您做的是

game_manager *game = new Game();

,然后进行一系列game->add_XXX的呼叫。这是通过其接口使用具体类的教科书示例。但是,只有在virtual类中将函数声明为game_manager时,这才起作用。他们不是。

答案 2 :(得分:1)

您需要做的就是在界面中使用virtual函数。

class inter {
public:
    virtual void interface_function() = 0; // Pure virtual function
    virtual void interface_function2() { // Virtual function
        cout << "I am ***inter***::interface_function2" << endl;
    }
};

class use_inter : public inter {
public:
    void interface_function() { // Override inter::interface_function
        cout << "I am use_inter::interface_function" << endl;
    }
    void interface_function2() { // Override inter::interface_function2
        cout << "I am use_inter::interface_function2" << endl;
    }
};

class use_inter2 : public inter {
public:
    // Because we don't override a pure virtual function, this class is abstract too.
};

class use_inter3 : public inter {
public:
    void interface_function() { // Override inter::interface_function
        cout << "I am use_inter3::interface_function" << endl;
    }
    // Here we don't override a regular virtual function, so in call from this object type to "interface_function2", the implementation of inter::interface_function2 will be execute.
};

int main() {
    use_inter ui1;
    //use_inter2 ui2; // Compiler error: use_inter2 is an abstract class.
    use_inter3 ui3;
    ui1.interface_function(); // Prints: I am use_inter::interface_function
    ui3.interface_function(); // Prints: I am use_inter3::interface_function
    ui1.interface_function2(); // Prints: I am use_inter::interface_function2
    ui3.interface_function2(); // Prints: I am ***inter***::interface_function2

    cout << endl << "==============================" << endl << endl;

    inter *interface = new use_inter();

    interface->interface_function(); // Prints: I am use_inter::interface_function
    interface->interface_function2(); // Prints: I am use_inter::interface_function2

    delete interface;

    cout << endl << "==============================" << endl << endl;

    interface = new use_inter3();

    interface->interface_function(); // Prints: I am use_inter3::interface_function
    interface->interface_function2(); // Prints: I am ***inter***::interface_function2

    delete interface;
    return 0;
}

----- Output:

I am use_inter::interface_function
I am use_inter3::interface_function
I am use_inter::interface_function2
I am ***inter***::interface_function2

==============================

I am use_inter::interface_function
I am use_inter::interface_function2

==============================

I am use_inter3::interface_function
I am ***inter***::interface_function2