[英]Multiple inheritance
I've read multiple inheritance from a book known as "C++ Primer" but I've only received a header file for the example written in the book which makes it little difficult for me to understand without source.cpp. 我已经从一本名为“ C ++ Primer”的书中阅读了多重继承,但是我只收到了书中编写的示例的头文件,这使得没有source.cpp对我来说很难理解。 So, my question is, what is an
Endangered
class and how do i define the member functions highlight
, cuddle
, onExhibit
etc? 所以,我的问题是,什么是
Endangered
类,我如何定义的成员函数highlight
, cuddle
, onExhibit
等?
This header file can also be downloaded from here . 该头文件也可以从此处下载。
// Multiple Inheritance.h
#include <string>
#include <iostream>
class Endangered {
public:
virtual ~Endangered();
virtual std::ostream& print(std::ostream&) const;
virtual void highlight() const;
// ...
};
class ZooAnimal;
extern std::ostream&
operator<<(std::ostream&, const ZooAnimal&);
class ZooAnimal {
public:
ZooAnimal();
ZooAnimal(std::string animal, bool exhibit,
std::string family): nm(animal),
exhibit_stat(exhibit),
fam_name(family) { }
virtual ~ZooAnimal();
virtual std::ostream& print(std::ostream&) const;
virtual int population() const;
// accessors
std::string name() const { return nm; }
std::string family_name() const { return fam_name; }
bool onExhibit() const { return exhibit_stat; }
// ...
protected:
std::string nm;
bool exhibit_stat;
std::string fam_name;
// ...
private:
};
class Bear : public ZooAnimal {
enum DanceType { two_left_feet, macarena, fandango, waltz };
public:
Bear();
Bear(std::string name, bool onExhibit=true,
std::string family = "Bear"):
ZooAnimal(name, onExhibit, family),
ival(0), dancetype(two_left_feet) { }
virtual std::ostream &print(std::ostream&) const;
virtual int toes() const;
int mumble(int);
void dance(DanceType) const;
virtual ~Bear();
private:
int ival;
DanceType dancetype;
};
class Panda : public Bear, public Endangered {
public:
Panda();
Panda(std::string name, bool onExhibit=true);
virtual ~Panda();
virtual std::ostream& print(std::ostream&) const;
void highlight();
virtual int toes();
virtual void cuddle();
// ...
};
Panda::Panda(std::string name, bool onExhibit)
: Bear(name, onExhibit, "Panda") { }
inline
std::ostream& Panda::print(std::ostream &os) const
{
Bear::print(os); // print the Bear part
Endangered::print(os); // print the Endangered part
return os;
}
class PolarBear : public Bear { /* . . . */ };
Just have functions with the same name, return type, argument number, const-ness, and argument types (known collectively as the function signature ) in your class that you're deriving. 在要派生的类中具有相同名称,返回类型,参数编号,常数和参数类型(统称为函数签名 )的函数 。
For instance, 例如,
// Me.h
class Me : public Endangered, public ZooAnimal {
// notice that these don't have virtual before them
std::ostream& print(std::ostream&) const;
void highlight() const;
std::ostream& print(std::ostream&) const;
int population() const;
/* etc */
};
// Me.cpp
std::ostream& Me::print(std::ostream& stream) const { /* stuff */; return stream; }
void Me::highlight() const { /* stuff */ }
/* etc */
Note that the destructors for those classes must be defined for each class, not for derived classes. 请注意,必须为每个类(而不是派生类)定义这些类的析构函数。 So you'd need
所以你需要
ZooAnimal::~ZooAnimal() {
/* stuff */
}
not Me::~Me
(although you'd need that too if Me
had one). 不是
Me::~Me
(尽管如果Me
有一个, Me
也需要)。
somewhere. 某处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.