[源碼下載]
作者:webabcd
介紹
不可或缺 Windows Native 之 C++
示例
1、CppEmployee 類
CppEmployee.h
#pragma once
#include <string>
using namespace std;
namespace NativeDll
{
class CppEmployee
{
int Number; // 默認為 private
private: // 以下都是 private 的
string Name;
bool IsMale;
protected: // 以下都是 protected 的
string ToString();
public: // 以下都是 public 的
float Salary;
int Age;
string Show();
// 構造函數(constructor),不定義的話,編譯時會自動生成一個默認的不做任何事情的無參數構造函數(如果指定為 private 的話就可以禁止直接實例化,一般做單例模式的話會這麼做)
// 另外:如果聲明了帶參數的構造函數,則不會自動生成默認的無參數構造函數
CppEmployee();
CppEmployee(int number, string name = "webabcd"); // 可以為構造函數中的參數指定默認值(參考:CppFunction1.cpp)
CppEmployee(int number, string name, bool isMale);
// 析構函數(destructor),對象被銷毀時會調用,例如釋放動態分配的內存等。不需要時也可以不定義,編譯時會自動生成一個默認的不做任何事情的析構函數,析構函數的函數名與類名相同,前面有“~”
~CppEmployee();
private:
// 注:在體內定義的成員函數,系統會自動將其作為 inline 函數處理(關於 inline 函數,參見:CppFunction2.cpp)
void Temp()
{
int a = 100;
}
/*
下面這個與上面那個一樣
inline void Temp()
{
int a = 100;
}
*/
};
}
CppEmployee.cpp
/*
* CppEmployee 類
*/
#include "pch.h"
#include "CppEmployee.h"
#include "cppHelper.h"
using namespace NativeDll;
// “::”是作用域限定符(field qualifier)
string CppEmployee::Show()
{
return int2string(Number) + " " + Name;
}
string CppEmployee::ToString()
{
return int2string(Number) + " " + Name;
}
// 無參數的構造函數
CppEmployee::CppEmployee()
{
Number = 888;
Name = "webabcd";
}
// 有參數的構造函數,可以在聲明中為參數指定默認值
CppEmployee::CppEmployee(int number, string name)
{
Number = number;
Name = name;
}
// 可以通過下面這種簡單的方式,將構造函數中的參數值賦值給對象的變量
CppEmployee::CppEmployee(int number, string name, bool isMale) :Number(number), Name(name), IsMale(isMale)
{
}
CppEmployee::~CppEmployee()
{
}
2、演示 this 指針, 對象數組, 對象和指針, const 對象, const 指針和指向 const 對象的指針, const 對象的引用
CppClass2.h
#pragma once
#include <string>
using namespace std;
namespace NativeDll
{
class CppClass2
{
private:
string Name;
public:
string Demo();
};
}
CppClass2.cpp
/*
* this 指針, 對象數組, 對象和指針, const 對象, const 指針和指向 const 對象的指針, const 對象的引用
*/
#include "pch.h"
#include "CppClass2.h"
#include "CppEmployee.h"
using namespace NativeDll;
void cppclass2_demo1();
void cppclass2_demo2();
void cppclass2_demo3();
void cppclass2_demo4();
void cppclass2_demo5();
string CppClass2::Demo()
{
// 什麼是 this 指針:
// 在每一個成員函數中都包含一個特殊的指針,即 this 指針。它是指向本類對象的指針,它的值是當前被調用的成員函數所在的對象的起始地址
this->Name = "abc";
string name = (*this).Name;
// 對象數組
cppclass2_demo1();
// 對象和指針
cppclass2_demo2();
// const 對象
cppclass2_demo3();
// const 指針和指向 const 對象的指針
cppclass2_demo4();
// const 對象的引用
cppclass2_demo5();
return "看代碼及注釋吧";
}
// 對象數組
void cppclass2_demo1()
{
// 對象數組
CppEmployee employees[3] =
{
CppEmployee(1, "name1"),
CppEmployee(2, "name2"),
CppEmployee(3, "name3")
};
// 數組名 employees 就是該數組的首地址
employees->Show(); // 1 name1
}
// 對象和指針
void cppclass2_demo2()
{
CppEmployee employee(1, "webabcd");
// 指向對象的指針
CppEmployee *p1 = &employee;
p1->Salary = 1000;
// 指向對象中的指定屬性的指針
float *p2 = &employee.Salary; // . 的優先級高
// 函數指針(指向函數的指針)
string (CppEmployee::*p3)(); // 定義一個函數指針。此函數為 CppEmployee 類中的一個 public 函數,其無參數,返回值為 string 類型
p3 = &CppEmployee::Show; // 使 p3 指向 CppEmployee 類中的 public 函數 Show
string result = (employee.*p3)(); // 調用對象 employee 中 p3 所指的函數
string(CppEmployee::*p4)() = &CppEmployee::Show;
result = (employee.*p4)(); // 1 webabcd
}
namespace NativeDll
{
class CppEmployeeConst
{
public:
float Salary;
int Age;
mutable int Num; // mutable 屬性,即使對象是 const 對象的話,也可以設置其 mutable 屬性
const string Name; // const 數據成員
const boolean IsMale; // const 數據成員
string Show() const // const 成員函數(注意 const 寫在後面)
{
// const 函數不能調用非 const 函數
// return this->ToString();
return "abc";
}
string ToString()
{
return "abc";
}
// 必須提供一個構造函數,用以初始化所有 const 數據成員
CppEmployeeConst(string name) :Name(name), IsMale(true)
{
this->Age = 35;
}
};
}
// const 對象
void cppclass2_demo3()
{
// 實例化一個非 const 對象
CppEmployeeConst employee("webabcd");
// 不能修改對象中的 const 屬性
// employee.IsMale = true;
// 實例化一個 const 對象
// CppEmployeeConst const employeeConst;
const CppEmployeeConst employeeConst("webabcd");
// 可以獲取 const 對象中的 const 屬性和非 const 屬性
string name = employee.Name;
int age = employee.Age;
// 不能設置 const 對象中的 const 屬性和非 const 屬性
// employee.Salary = 100;
// employee.Name = "wanglei";
// 可以設置 const 對象中的 mutable 屬性
employee.Num = 88;
// 可以調用 const 對象中的 const 函數,不能調用非 const 函數
employee.Show();
// employee.ToString();
}
// const 指針和指向 const 對象的指針
void cppclass2_demo4()
{
CppEmployee employee(1, "webabcd");
CppEmployee employee2(1, "webabcd");
// const 指針:指針始終指向同一個對象(指針是 const 的)
CppEmployee *const p1 = &employee;
// p1 = &employee2; // 編譯錯誤,因為指針是 const 的,不能對其賦值
// 指向 const 對象的指針(所指的對象是 const 的)
const CppEmployee *p2 = &employee;
// p2->Salary = 100; // 編譯錯誤,因為所指的對象是 const 的,不能對其屬性賦值
// 當使用指針的方式傳參時,如果不希望在函數中修改其所指的對象,則可以這麼做
void cppclass2_function1(const CppEmployee *p);
cppclass2_function1(&employee);
}
void cppclass2_function1(const CppEmployee *p)
{
// 不能在此函數中修改 p 所指對象
// p->Salary = 100;
}
// const 對象的引用
void cppclass2_demo5()
{
CppEmployee employee(1, "webabcd");
CppEmployee employee2(1, "webabcd");
// const 對象的引用(被引用的對象是 const 的)
const CppEmployee &r = employee;
// r.Salary = 100; // 編譯錯誤,因為被引用的對象是 const 的,不能對其屬性賦值
// 當使用引用的方式傳參時,如果不希望在函數中修改其引用的對象,則可以這麼做
void cppclass2_function2(const CppEmployee &r);
cppclass2_function2(r);
cppclass2_function2(employee);
}
void cppclass2_function2(const CppEmployee &r)
{
// 不能在此函數中修改 r 所引用的對象
// r.Salary = 100;
}
OK
[源碼下載]