C++ Builder: Function Library
If you have functions that may be used in several projects, the best place to put them would a formless unit, say a "library" unit called Funcs.
Short example
- Start a new project, say FunctionTest, with a form named Form1.
- In the menu, select File / New / Unit C++Builder
- Save the unit as Funcs.
You declare the functions in Funcs.h (a "header" file) and put the implementation in Funcs.cpp. Proceed as follows:
- In Funcs.h, add the following lines:
int Summer(int a, int b);
int Multiplier(int a, int b);
- In Funcs.cpp, add:
int Summer(int a, int b)
{
return (a + b);
}
int Multiplier(int a, int b)
{
return (a * b);
}
- Before using the functions in your Unit1.cpp, you have to tell C++ Builder that Unit1.cpp "uses" Funcs.cpp:
Open Unit1.cpp, in the menu select File / Use Unit... and select Funcs.cpp
This adds the following line near the top of Unit1.cpp:
#include "Funcs.h"
From now on, you can use these functions in Unit1.cpp, for example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Label1->Caption = IntToStr(Summer(3, 5));
}
CBTFunc : a library with useful functions
Start by creating a test project and next add a second unit without a form. But this time,
save that unit as CBTFunc.cpp.
We want the following functions:
- ArrAverage() returns the average of an array of floating point numbers.
- Factorial() returns the factorial (n!) of an integer number.
n! = 1 x 2 x 3...(n-2) x (n-1) x n
- ProgDir() returns the folder ("directory") where the application's exe-file is located.
- WeekDayStr() returns a string with the day of the week for a given date.
- WeekDayNr() returns the number of the day of the week for a given date.
Add the declarations and the implementations to CBTFunc:
- Modify CBTFunc.h:
#ifndef CBTFuncH
#define CBTFuncH
//------------------------------------------------------
#endif
#include <System.Classes.hpp> // for the String class
float ArrAverage(float arr[], int size);
String ProgDir();
String WeekDayStr(TDateTime D);
int WeekDayNr(int y, int m, int d);
- Modify CBTFunc.cpp:
#pragma hdrstop
#include "CBTFunc.h"
#include <vcl.h> // for the TApplication class
//--------------------------------------------------------
#pragma package(smart_init)
float ArrAverage(float arr[], int size)
{
int i;
float sum;
sum = 0;
for (i = 0; i < size; ++i) {
sum += arr[i];
}
return (sum / size);
}
String ProgDir()
{
return ExtractFilePath(Application->ExeName);
}
String WeekDayStr(TDateTime D)
{
String Days[7] = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
int WeekDayNr;
WeekDayNr = DayOfWeek(D); // returns integer 1 to 7
return (Days[WeekDayNr - 1]); // Days[0] is "Monday"
}
- Don't forget to declare that Unit1.cpp uses CBTFunc.cpp:
In the menu, select File / Use Unit... and select CBTFunc.cpp
After you have tested our examples, we advise you to back up the
files to a safe location ;)
Next, feel free to add your own functions!
Use CBTFunc in any project
Copy-pasting the code is the worst form of code reuse. If you want to use your library with functions in another project:
- Copy both files CBTFunc.h and CBTFunc.cpp to the folder of the other project.
- Open your project in C++ Builder, in the menu select Project / Add to Project... and select CBTFunc.cpp.
- Open the unit where you want to use the functions of CBTFunc, say in Unit1.cpp. In the menu, select
File / Use Unit... and select CBTFunc.cpp
Example project
Here's a small project for testing. Start a new project, save the files, and next:
- Copy both files CBTFunc.h and CBTFunc.cpp to the folder of your project.
- Add a button and a TMemo component the form.
- Create an event handler for the button:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float nums[5] = {10, 20, 3.14, 6, 9};
Memo1->Lines->Add("ProgDir: " + ProgDir());
Memo1->Lines->Add("Average: " + FloatToStr(ArrAverage(nums, 5)));
Memo1->Lines->Add("!5 = " + IntToStr(Factorial(5)));
Memo1->Lines->Add("Week day: " + WeekDayStr(Date()));
}
- In the menu select Project / Add to Project... and select CBTFunc.cpp.
- Open Unit1.cpp. In the menu, select
File / Use Unit... and select CBTFunc.cpp
|
|