English 中文(简体)
C++ 继承。 物体指超级方法而不是自己的方法?
原标题:C++ Inheritance. Object calls super-class method instead of its own method?

因此,我有2个班级。

class Animal{
public:
    Animal(int age, int hairCount) {
        howOld = age;
        numOfHairs = hairCount;
    }

    void print(){
        cout << "Age: " << howOld << "	Number of Hairs: " << numOfHairs << endl;
    }

protected:
    int howOld;
    int numOfHairs;
};



class Bird: public Animal{
public:
    Bird(int age, int hairCount, bool fly) : Animal(age, hairCount) {
        canItFly = fly;
    }

    void print() {
        cout << "Age: " << howOld << "	Number of Hairs: " 
             << numOfHairs << "	Ability to fly: " << canItFly << endl;
    }
protected:
    bool canItFly;
};

If in the main program, I have something like this:

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<Animal> list;
    list.pushBack(Bird(5,10000,true));
    list.pushBack(Animal(14,1234567));

    for(int i = 0; i < list.size(); i++){
        list[i].print(); //Calls the super class for both outputs
    }
    return 0;
}

出于某种原因,我的代码(斜线)在这两种情况下都使用超级印刷方法。

最佳回答

您应宣布成员的职能。

void print()

a)

virtual void print()

此外,你还应创造动物点的病媒

vector<Animal *>

主要使用新的物体。 然后它将按预期开展工作。 这就是说,你的主要愿望是这样做。

vector<Animal *> list;
Animal *bird = new Bird(5,10000,true);
Animal *animal = new Animal(14,1234567);
list.push_back(bird);
list.push_back(animal);

如果你不再需要鸟类和动物,也不会忘记去除。

delete bird;
delete animal;

按照本杰明·林德的建议,你可以选择使用智能班。

问题回答

你们能够用固定型物体做多变。 你们需要使用点或参考点。 Try a vector<unique_ptr<Animal>>,以避免记忆管理头痛。

页: 1

http://en.wikipedia.org/wiki/Object_slicing”rel=“nofollow”>http://en.wikipedia.org/wiki/Object_slicing

正如其他人所说的那样,你可以静态地宣布一系列动物,因为只有足够空间来储存动物,像鸟类这样的东西才会被割裂。

多吗?

Bird bibo;
Crocodile claus;

Animal & a1 = bibo, & a2 = claus;

a1.print();
a2.print();

只添加一个通用的<代码>Animal a ,使你能够接触到任何多变行为,事实上,这甚至可能意味着: 由于每个动物都是某种类型的具体动物,因此,你的基类可能是抽象的。

现在,集装箱是什么? 由于来自基类的具体班级面积可能各不相同,因此,你无法直接将其放在集装箱内。 相反,你应将“pointer号”放在集装箱内的基类。

选择点为<代码>std:unique_ptr<Animal>,即轻度和最简单的终身管理形式。 它这样做:

#include <memory>
#include <vector>

typedef unique_ptr<Animal> AnimalPtr;
typedef std::vector<AnimalPtr> Zoo;

Zoo z;

z.push_back(AnimalPtr(new Bird));   // old-style
z.emplace_back(new Crocodile);      // new-style, better

Finally, a little obscure detail on how to make Animal abstract. We could declare print() to be pure virtual. Yet we also want a base implementation. We can do both:

struct Animal
{
  virtual void print() const = 0;
  // ...
};
void Animal::print() const
{
  out << "Age: " << howOld << "	Number of Hairs: " << numOfHairs << endl;
}

struct Bird
{
  virtual void print() const
  {
    Animal::print();  //  call base function first
    cout << "Ability to fly: " << canItFly << endl;
  }
  // ...
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签