複製鏈接
請複製以下鏈接發送給好友

函數封裝

鎖定
函數封裝是一種函數功能,它把一個程序員寫的一個或者多個功能通過函數、類的方式封裝起來,對外只提供一個簡單的函數接口。當程序員在寫程序的過程中需要執行同樣的操作時,程序員(調用者)不需要寫同樣的函數來調用,直接可以從函數庫裏面調用。程序員也可以從網絡上下載的功能函數,然後封裝到編譯器庫函數中,當需要執行這一功能的函數時,直接調用即可。而程序員不必知道函數內部如何實現的,只需要知道這個函數或者類提供什麼功能。
中文名
函數封裝
所屬學科
數學
C語言函數封裝例子
func1(.....)
{
.....
.....
}
func2(......)
{
.....
.....
}
func3(.....)
{
func1(....)
{
func2(...)
{
}
}
能不能將func1和func2封裝成一個DLL,能夠直接讓func3調用?
當然可以,寫一個DLL就可以了,把fun1和fun2寫進去,舉個例子:
在DLL中寫入:
//MyDll.h
extern "C" int _declspec(dllexport) Max(int a, int b);
//MyDll.cpp
#include "windows.h"
#include "MyDll.h"
int Max(int a, int b)
{
if(a>=b)return a;
else return b;
}
在console應用程序中寫入:
#include "stdio.h"
#include "stdlib.h"
extern "C" _declspec(dllexport)
int Max(int a, int b);
#pragma comment(lib,"MyDll.lib")
int main()
{
int a;
a = Max(10, 5);
printf("%d \n", a);
return 0;
}