继承的方式:
public继承:
注意上图中pirvate中的成员继承到了不一见位置,不是private下。在work方法中访问m_iAge是错误的。
总结:
示例:
#pragma once
#include <string>
using namespace std;
class Person
{
public:
Person(void);
~Person(void);
void eat();
protected:
string m_strName;
private:
int m_iAge;
};
#include "Person.h"
#include <iostream>
using namespace std;
void Person::eat(){
m_strName = "bxf";
m_iAge = 19;
cout << "eat()" << endl;
}
Person::Person(void)
{
cout << "Person()" << endl;
}
Person::~Person(void)
{
cout << "~Person()" << endl;
}
#pragma once
#include "person.h"
class Worker :
public Person
{
public:
Worker(void);
~Worker(void);
void work();
int m_iSalary;
};
#include "Worker.h"
#include <iostream>
using namespace std;
Worker::Worker(void)
{
cout << "work()" << endl;
}
Worker::~Worker(void)
{
cout << "~work()" << endl;
}
void Worker::work(){
m_strName = "xxx";
//m_iAge = 10; 这里是父类里面的private 下的成员,不可以在子类中调用
cout << "work()" << endl;
}
#include <iostream>
#include "Worker.h"
using namespace std;
int main(){
Worker *worker = new Worker;
worker->eat();//父类中public下的方法
worker->work();
delete worker;
worker = NULL;
system("pause");
return 0;
}
执行结果只是为了说明上面的语法没有错误。
注意上面各个限定符下子类可调用的成员和方法。
protect继承:
private继承:
隐藏:
子父类之间,相同的名称
注意上面的父类隐藏方法的调用方式
子类中调用父类中相同名称的成员变量
简单的多继承:
#pragma once
#include <string>
using namespace std;
class Worker
{
public:
Worker(string code = "001");
virtual ~Worker(void);
void work();
protected:
string m_strCode;
};
#include "Worker.h"
#include <iostream>
using namespace std;
Worker::Worker(string code):m_strCode(code)
{
cout << m_strCode << endl;
cout << "work()" << endl;
}
Worker::~Worker(void)
{
cout << "~work()" << endl;
}
void Worker::work(){
cout << m_strCode << endl;
cout << "work()" << endl;
}
#pragma once
#include <string>
using namespace std;
class Farmer
{
public:
Farmer(string name="jack");
~Farmer(void);
void sow();
protected:
string m_strName;
};
#include "Farmer.h"
#include <iostream>
using namespace std;
Farmer::Farmer(string name):m_strName(name)
{
cout << m_strName << endl;
cout << "Farmer()" << endl;
}
Farmer::~Farmer(void)
{
cout << "~Farmer()" << endl;
}
void Farmer::sow(){
cout << "sow()" << endl;
}
#pragma once
#include "Worker.h"
#include "Farmer.h"
#include <string>
using namespace std;
class MigrantWorker:public Worker, public Farmer
{
public:
MigrantWorker(string name, string code);
~MigrantWorker(void);
};
#include "MigrantWorker.h"
#include <iostream>
using namespace std;
MigrantWorker::MigrantWorker(string name, string code):Worker(code), Farmer(name)
{
cout << "MigrantWorker()" << endl;
}
MigrantWorker::~MigrantWorker(void)
{
cout << "~MigrantWorker()" << endl;
}
#include <iostream>
#include "MigrantWorker.h"
using namespace std;
int main(){
MigrantWorker *mw = new MigrantWorker("Merry", "002");
mw->work();
mw->sow();
delete mw;
mw = NULL;
system("pause");
return 0;
}
虚继承:
有一种菱形继承:
类D继承B和C, B和C分别继承A,在实例化D时就会出现实例化2个A,造成数据冗余的问题,用虚继承来解决:
class B: virtual public A
class C: virtual public A
class D: public B, public D
在实例化D时就会只有一个A的构造和析构函数:
A()
B()
C()
D()
~A()
~B()
~C()
~D()