C ++:使用抽象方法创建抽象类,并覆盖子类中的方法 [英] C++: Create abstract class with abstract method and override the method in a subclass

查看:208
本文介绍了C ++:使用抽象方法创建抽象类,并覆盖子类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中创建一个抽象类与一些抽象方法,我想在子类中重写?文件应如何显示? .h 有没有 .cpp ,如果是这样,它应该怎么样?

How to create in C++ an abstract class with some abstract methods that I want to override in a subclass? How should the .h file look? Is there a .cpp, if so how should it look?

在Java中它看起来像这样: / p>

In Java it would look like this:

abstract class GameObject
{
    public abstract void update();
    public abstract void paint(Graphics g);
}

class Player extends GameObject
{
    @Override
    public void update()
    {
         // ...
    }

    @Override
    public void paint(Graphics g)
    {
         // ...
    }

}

// In my game loop:
List<GameObject> objects = new ArrayList<GameObject>();
for (int i = 0; i < objects.size(); i++)
{
    objects.get(i).update();
}
for (int i = 0; i < objects.size(); i++)
{
    objects.get(i).paint(g);
}

将这段代码翻译成C ++对我来说已经足够了。

Translating this code to C++ is enough for me.

编辑

我创建了代码,但是当我尝试遍历对象时, :

I created the code but when I try to iterate over the objects I get following error:

Game.cpp:17: error: cannot allocate an object of abstract type ‘GameObject’
GameObject.h:13: note:   because the following virtual functions are pure within ‘GameObject’:
GameObject.h:18: note:         virtual void GameObject::Update()
GameObject.h:19: note:         virtual void GameObject::Render(SDL_Surface*)
Game.cpp:17: error: cannot allocate an object of abstract type ‘GameObject’
GameObject.h:13: note:   since type ‘GameObject’ has pure virtual functions
Game.cpp:17: error: cannot declare variable ‘go’ to be of abstract type ‘GameObject’
GameObject.h:13: note:   since type ‘GameObject’ has pure virtual functions

使用此代码:

vector<GameObject> gameObjects;

for (int i = 0; i < gameObjects.size(); i++) {
    GameObject go = (GameObject) gameObjects.at(i);
    go.Update();
}


推荐答案

virtual 默认情况下,除非您声明他们 final 。在C ++中,它是另一种方法:你需要显式声明你的方法 virtual 。要使它们成为纯虚拟,你需要将它们初始化为0 :-)如果你的类中有一个纯虚方法,它会自动变成抽象的 - 没有明确的关键字。

In Java, all methods are virtual by default, unless you declare them final. In C++ it's the other way around: you need to explicitly declare your methods virtual. And to make them pure virtual, you need to "initialize" them to 0 :-) If you have a pure virtual method in your class, it automatically becomes abstract - there is no explicit keyword for it.

在C ++中,你应该(几乎)总是为你的基类定义析构函数 virtual ,以避免棘手的资源泄露。所以我添加到下面的例子:

In C++ you should (almost) always define the destructor for your base classes virtual, to avoid tricky resource leaks. So I added that to the example below:

// GameObject.h

class GameObject
{
public:
    virtual void update() = 0;
    virtual void paint(Graphics g) = 0;
    virtual ~GameObject() {}
}

// Player.h
#include "GameObject.h"

class Player: public GameObject
{
public:
    void update();

    void paint(Graphics g);
}

// Player.cpp
#include "Player.h"

void Player::update()
{
     // ...
}

void Player::paint(Graphics g)
{
     // ...
}

这篇关于C ++:使用抽象方法创建抽象类,并覆盖子类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆