[源碼下載]
作者:webabcd
介紹
不可或缺 Windows Native 之 C++
示例
1、基類 1
CppBase1.h
#pragma once
#include <string>
#include "CppBaseVirtual.h"
using namespace std;
namespace NativeDll
{
// virtual 代表 CppBaseVirtual 是 CppBase1 的虛基類(虛基類是在聲明派生類時,指定繼承方式時聲明的)
class CppBase1 : virtual public CppBaseVirtual
{
protected:
string Name;
float Salary;
public:
CppBase1(int number, string name, float salary);
};
}
CppBase1.cpp
/*
* 基類 1
*/
#include "pch.h"
#include "CppBase1.h"
using namespace NativeDll;
CppBase1::CppBase1(int number, string name, float salary) :CppBaseVirtual(number), Name("cppbase1 " + name), Salary(salary)
{
}
2、基類 2
CppBase2.h
#pragma once
#include <string>
#include "CppBaseVirtual.h"
using namespace std;
namespace NativeDll
{
// virtual 代表 CppBaseVirtual 是 CppBase2 的虛基類(虛基類是在聲明派生類時,指定繼承方式時聲明的)
class CppBase2 : virtual CppBaseVirtual
{
protected:
string Name;
int Age;
public:
CppBase2(int number, string name, int age);
};
}
CppBase2.cpp
/*
* 基類 2
*/
#include "pch.h"
#include "CppBase2.h"
using namespace NativeDll;
CppBase2::CppBase2(int number, string name, int age) :CppBaseVirtual(number), Name("cppbase2 " + name), Age(age)
{
}
3、虛基類
CppBaseVirtual.h
#pragma once
#include <string>
using namespace std;
namespace NativeDll
{
class CppBaseVirtual
{
private:
int Number;
public:
CppBaseVirtual(int number);
};
}
CppBaseVirtual.cpp
/*
* 用於演示虛基類
*
* 注:
* 1、虛基類並不是在聲明基類時聲明的,而是在聲明派生類時,指定繼承方式時聲明的
* 2、一個基類可以在生成一個派生類時作為虛基類,也可以在生成一個派生類時不作為虛基類
*/
#include "pch.h"
#include "CppBaseVirtual.h"
using namespace NativeDll;
CppBaseVirtual::CppBaseVirtual(int number) :Number(number)
{
}
4、派生類
CppDerived.h
#pragma once
#include <string>
#include "CppBase1.h"
#include "CppBase2.h"
using namespace std;
namespace NativeDll
{
class CppDerived : public CppBase1, public CppBase2
{
public:
CppDerived(int number, string name, float salary, int age);
string Show();
};
}
CppDerived.cpp
/*
* 派生類
*
*
* 在多重繼承的情況下:
* 1、如果 A 是 B C D E 的基類,F 同時繼承了 B C D E,那麼實例化 F 時會保存 4 份 A 成員
* 2、如果 A 是 B 的基類,A 是 C D E 的虛基類(虛基類會使得在繼承間接共同基類時只保留一份成員),F 同時繼承了 B C D E,那麼實例化 F 時會保存 2 份 A 成員(從 C D E 的路徑上保留一份,從 B 的路徑上保留一份)
*
*
* 本例中:
* 1、CppBaseVirtual 是 CppBase1 和 CppBase2 的虛基類
* 2、CppDerived 繼承了 CppBase1 和 CppBase2(多重繼承)
* 3、此種情況,實例化 CppDerived 只會保留一份 CppBaseVirtual 成員(因為 CppBaseVirtual 是 CppBase1 和 CppBase2 的虛基類)
* 4、C++ 編譯器只會執行最後的派生類(CppDerived)對虛基類(CppBaseVirtual)的構造函數的調用,而忽略虛基類的直接派生類(CppBase1 和 CppBase2)對虛基類的構造函數的調用,這就保證了虛基類的數據成員不會被多次初始化
*/
#include "pch.h"
#include "CppDerived.h"
#include "cppHelper.h"
using namespace NativeDll;
// 在無虛基類的情況下,派生類的構造函數中只需負責對其直接基類初始化
// 如果有虛基類,且虛基類中定義了帶參數的構造函數,且沒有定義默認構造函數,那麼派生類不僅要負責對其直接基類進行初始化,還要負責對虛基類初始化
CppDerived::CppDerived(int number, string name, float salary, int age) :CppBaseVirtual(number), CppBase1(number, name, salary), CppBase2(number, name, age)
{
}
string CppDerived::Show()
{
// 關於多重繼承的二義性(ambiguous),如果派生類同時繼承的多個基類有相同的成員,則調用這些成員時需顯式指定其基類
return CppBase1::Name + " " + float2string(Salary) + " " + int2string(Age);
// 另外:當然,如果派生類與多個基類有相同成員的話,那麼基類中的這些與派生類相同的成員都會被隱藏掉(即派生類中的成員會覆蓋基類中的成員)
}
5、示例
CppClass6.h
#pragma once
#include <string>
using namespace std;
namespace NativeDll
{
class CppClass6
{
public:
string Demo();
};
}
CppClass6.cpp
/*
* 多重繼承(multiple inheritance), 虛基類(virtual base class)
*/
#include "pch.h"
#include "CppClass6.h"
#include "CppDerived.h"
using namespace NativeDll;
string CppClass6::Demo()
{
CppDerived derived(0, "webabcd", 100.0f, 35);
string result = derived.Show(); // cppbase1 webabcd 100.00 35
return result;
}
OK
[源碼下載]