------------------siwuxie095
多继承 和 多重继承,两个词差别不大,但是差之毫厘、谬以千里 …
多重继承
如果有这样三个类:人类、士兵类、步兵类,其中:士兵类继承了人类,
步兵类继承了士兵类,这三个类之间的关系称之为 多重继承
如果在继承时,这三个类都使用了 public 公有继承,那么它们也
存在着以下的关系:
士兵是一个人,步兵是一个士兵,步兵也是一个人
具体到代码上,可以这样来写:
多继承
如果有一个类,不仅继承了工人类,还继承了农民类,暂且把这个类
称之为 农民工类
即 一个子类同时有两个父类,或 一个派生类同时有两个基类,这样的
关系称之为 多继承
多继承和多重继承是完全不同的,在多继承的关系下,如果农民工类
在继承工人类和农民类时,都以 public 公有继承的方式继承,那么
它们还存在着这样一种关系:
农民工是一个工人,农民工是一个农民,但工人和农民本身是平行的
具体到代码上,可以这样来写:
注意:写法上,中间要加逗号隔开,在继承每一个类时,都要将继承
方式写出来,如果不写,系统默认继承方式为 private 私有继承
程序 1:多重继承
Person.h:
#include <string> using namespace std;
class Person { public: Person(string name = "Jim"); ~Person(); void play(); protected: string m_strName; }; |
Person.cpp:
#include "Person.h" #include <iostream> using namespace std;
Person::Person(string name) { m_strName = name; cout << "Person()" << endl; }
Person::~Person() { cout << "~Person()" << endl; }
void Person::play() { cout << "Person--play()" << endl; cout << m_strName << endl; } |
Soldier.h:
#include "Person.h" class Soldier:public Person { public: Soldier(string name = "James", int age = 20); ~Soldier(); void work(); protected: int m_iAge; }; |
Soldier.cpp:
#include "Soldier.h" #include <iostream> using namespace std;
Soldier::Soldier(string name, int age) { m_strName = name; m_iAge = age; cout << "Soldier()" << endl; }
Soldier::~Soldier() { cout << "~Soldier()" << endl; }
void Soldier::work() { cout << "Soldier--work()" << endl; cout << m_strName << "," << m_iAge << endl; } |
Infantry.h:
#include "Soldier.h"
class Infantry:public Soldier { public: Infantry(string name = "Jack", int age = 30); ~Infantry(); void attack();
}; |
Infantry.cpp:
#include "Infantry.h" #include <iostream> using namespace std;
Infantry::Infantry(string name, int age) { m_strName = name; m_iAge = age; cout << "Ifantry()" << endl; }
Infantry::~Infantry() { cout << "~Infantry()" << endl; }
void Infantry::attack() { cout << "Infantry--attack()" << endl; cout <<m_strName << "," << m_iAge << endl; } |
main.cpp:
#include <stdlib.h> #include "Infantry.h" #include <iostream> using namespace std;
void test1(Person p) { p.play(); }
void test2(Person &p) { p.play(); }
void test3(Person *p) { p->play(); }
//无论继承的层级有多少,只要保持着直接或间接的继承关系, //子类都可以与自己的直接父类或间接父类称为 is-a 关系 //并且能够通过指针对直接子类或间接子类的对象进行相应的操作 int main(void) { Infantry infantry; cout << endl;
infantry.play(); infantry.work(); infantry.attack(); cout << endl;
test1(infantry); cout << endl; test2(infantry); cout << endl; test3(&infantry); system("pause"); return 0; } |
运行一览:
程序 2:多继承
Farmer.h:
#include <string> using namespace std;
class Farmer { public: Farmer(string name = "Jack"); virtual ~Farmer();//虚析构函数 void sow(); protected: string m_strName; }; |
Farmer.cpp:
#include "Farmer.h" #include <iostream> using namespace std;
Farmer::Farmer(string name) { m_strName = name; cout << "Farmer()" << endl; }
Farmer::~Farmer() { cout << "~Farmer()" << endl; }
void Farmer::sow() { cout << "Farmer--sow()" << endl; cout << m_strName << endl; } |
Worker.h:
#include <string> using namespace std;
class Worker { public: Worker(string code = "001"); virtual ~Worker();//虚析构函数 void work(); protected: string m_strCode; }; |
Worker.cpp:
#include "Worker.h" #include <iostream> using namespace std;
Worker::Worker(string code) { m_strCode = code; cout << "Worker()" << endl; }
Worker::~Worker() { cout << "~Worker()" << endl; }
void Worker::work() { cout << "Worker--work()" << endl; cout << m_strCode << endl; } |
MigrantWorker.h:
#include "Farmer.h" #include "Worker.h"
class MigrantWorker :public Farmer, public Worker { public: MigrantWorker(string name, string code); virtual ~MigrantWorker(); }; |
MigrantWorker.cpp:
#include "MigrantWorker.h" #include <iostream> using namespace std;
//使用初始化列表初始化 MigrantWorker::MigrantWorker(string name, string code) :Farmer(name), Worker(code) { cout << "MigrantWorker()" << endl; }
MigrantWorker::~MigrantWorker() { cout << "~MigrantWorker()" << endl; } |
main.cpp:
#include <stdlib.h> #include "MigrantWorker.h" #include <iostream> using namespace std;
int main(void) { //从堆中实例化子类对象,先调用父类构造函数再调用子类构造函数 //调用父类构造函数的顺序和初始化列表中的顺序一样 //析构函数的调用顺序则相反 MigrantWorker *p = new MigrantWorker("Merry", "100"); cout << endl; p->sow(); p->work(); cout << endl; delete p; p = NULL; system("pause"); return 0; } |
运行一览:
【made by siwuxie095】