5-1 多继承和多重继承
多重继承:
多继承:
公有继承下:
上图,如果不写public继承,则系统默认为private继承;
5-2 多重继承代码演示
Person.h
#include <string>
using namespace std;//class也在std里面
class Person
{
public:
Person(string name = "Jim");
virtual~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<<m_strName<<endl;
cout<<"Person - play()"<<endl;
}
Soldier.h
#include "Person.h"
class Soldier:public Person
{
public:
Soldier(string name = "James",int age = 20);
virtual~Soldier();
void work();
protected:
int m_iAge;
};
Soldier.cpp
#include <iostream>
#include "Soldier.h"
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<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Soldier - work"<<endl;
}
Infantry.h
#include "Soldier.h"
class Infantry:public Soldier
{
public:
Infantry(string name = "James",int age = 30);
~Infantry();
void attack();
};
Infantry.cpp
#include <iostream>
#include "Infantry.h"
using namespace std;
Infantry::Infantry(string name,int age)
{
m_strName = name;
m_iAge = age;
cout<<"Infantry()"<<endl;
}
Infantry::~Infantry()
{
cout<<"~Infantry()"<<endl;
}
void Infantry::attack()
{
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Infantry - attack()"<<endl;
}
demo.cpp
#include <iostream>
#include <stdlib.h>
#include "Infantry.h"
using namespace std;
void test1(Person p)
{
p.play();//会实例化临时对象,使用之后会执行析构函数(声明的时候执行的是拷贝构造函数);
}
void test2(Person &p)
{
p.play();
}
void test3(Person *p)
{
p->play();
}
int main()
{
Infantry i1;
cout<<endl;
test1(i1);
cout<<endl;
test2(i1);
cout<<endl;
test3(&i1);
system("pause");
return 0;
}
运行结果
Person()
Soldier()
Infantry()
James
Person - play()
~Person()
James
Person - play()
James
Person - play()
请按任意键继续. . .
无论继承的层级有多少层,只要保持直接或者间接的继承关系,子类都与自己的直接父类或者间接父类称之为is a的关系,并且可以通过父类的指针对直接子类或间接子类进行操作;
5-3 多继承代码演示
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>
Farmer::Farmer(string name)
{
m_strName = name;
cout<<"Farmer()"<<endl;
}
Farmer::~Farmer()
{
cout<<"~Farmer()"<<endl;
}
void Farmer::sow()
{
cout<<m_strName<<endl;
cout<<"Farmer - sow()"<<endl;
}
Worker.h
#include <string>
using namespace std;
class Worker
{
public:
Worker(string code = "001");
virtual~Worker();
void carry();
protected:
string m_strCode;
};
Worker.cpp
#include"Worker.h"
#include<iostream>
Worker::Worker(string code)
{
m_strCode = code;
cout<<"Worker()"<<endl;
}
Worker::~Worker()
{
cout<<"~Worker()"<<endl;
}
void Worker::carry()
{
cout<<m_strCode<<endl;
cout<<"Worker - carry()"<<endl;
}
MigrantWorker.h
#include "Farmer.h"
#include "Worker.h"
class MigrantWorker:public Farmer,public Worker
{
public:
MigrantWorker(string name,string code);
~MigrantWorker();
};
MigrantWorker.cpp
#include "MigrantWorker.h"
#include <iostream>
MigrantWorker::MigrantWorker(string name,string code):Farmer(name),Worker(code)//先构造的Farmer再构造的Worker
{
cout<<"MigrantWorker()"<<endl;
}
MigrantWorker::~MigrantWorker()
{
cout<<"~MigrantWorker()"<<endl;
}
demo.cpp
#include <stdlib.h>
#include <iostream>
#include "MigrantWorker.h"
int main()
{
MigrantWorker *p = new MigrantWorker("Merry","100");
cout<<endl;
p->carry();
cout<<endl;
p->sow();
cout<<endl;
delete p;
p = NULL;
system("pause");
return 0;
}
运行结果
Farmer()
Worker()
MigrantWorker()
100
Worker - carry()
Merry
Farmer - sow()
~MigrantWorker()
~Worker()
~Farmer()
请按任意键继续. . .