C++ Builder tutorial for Delphi programmers
When you launch C++Builder, you'll note how familiar the IDE looks. Let's take it for a short spin
and note the similarity to Delphi. Here's what we want to build:
Understanding the IDE
C++Builder is what we call an IDE (Integrated Development Environment). It has three general parts:
- a code editor
- a compiler
- a debugger
The code editor is where you write code. It is a text editor with special enhancements for programming.
When you design a form (that is, a window), one tab of the code editor lets you visually edit the form.
The compiler is a program that converts your code into a program that Windows can run.
The debugger lets you run the program and see what it is doing. You can pause it, look at various parts of it,
run through your code step by step, and so on. The debugger helps you find
the cause of any unexpected behaviour ("bugs").
The three parts are integrated together and accompanied by a number of dockable tool windows and other
tools.
Projects
Typically, you want to use more than one single source code file in your application. A project describes
how the IDE groups together multiple source code files into one program.
Hands on
The first thing to do is create a new project, change a few details and save the files.
- In the Menu, select File / New / VCL Forms Application - C++Builder.
- You now have a project, as you can see in the Project Manager, a window that by default is docked on
the right hand side of the IDE.
You will also be presented with a new empty main form, named Form1.
- In the Object Inspector, change the form's Caption to FirstProject
- From the Standard page of the Tool Palette, drop a few components on the form:
a TEdit, a TButton and a TLabel.
- In the Project Manager, right-click the bold Project1.exe
and select Rename. Now type: FirstProject
You'll notice that some other files are renamed too - that’s because the IDE manages those and
keeps them in sync.
Still in the Project Manager, check that there is a file Unit1.cpp; if it's called differently, e.g.
Unit2.cpp, rename it to Unit1.cpp (just in order to stay in sync with our examples).
- Now save your files: Menu File / Save All
- Don't accept the default location, but navigate to the folder where you want to save
the files. E.g. create a folder C:\C++Projects\FirstProject and save in this new folder.
- Press function key F9 in order to build and run your program.
What are these files? In your Windows file explorer, open up the folder where you saved your project and have
a look:
- Foot2Meter.cbproj is the "project file". It describes what files are in the project and what the
settings are. It corresponds to the bold top-level node labeled "Foot2Meter" in the Project Manager.
- Foot2Meter.cpp is the main source code file for the application.
- Foot2Meter1PCH1.h is used as an optimisation for making your application compile faster. "PCH" stands for
"precompiled header".
- Unit1.cpp and Unit1.h: C++ puts the code into two files, the main implementation file, where your
methods and so on are written, and a header file, which is where they are "declared". A header tells the
compiler "this is what you should expect to see", while the implementation is where you actually "implement"
it.
The combination of these two is called a "unit". In fact, in the code editor, C++Builder opens them as a
single entity, and you switch between the .cpp and header using the tabs at the bottom of the code
editor.
- Unit1.dfm is used for the user interface of the main "form", that becomes a window when you run the
application.
Let's add some functionality:
- In Design mode, double click on Button1. Automatically, code for an event handler will be created.
- Complete the code as follows:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Label1->Caption = Edit1->Text;
}
- Save your files and test the project.
Adding a form
Let's add a second form, an "About" dialog:
- In the Menu, select File / New / VCL Form - C++Builder
- Change the new form's Caption to About and its name to FormAbout
- Save the unit as About.cpp (menu: File / Save)
- Drop a TLabel on the form and set its Caption to Created with C++Builder
- Drop a TBitBtn on the form and set its property Kind to bkOK.
- Note that you don't have to add an event handler to BitBtn1 in order to close the form.
By setting the button's property Kind to bkOK, its property ModalResult
is set automatically to mrOK, and the result is that the form will be closed
when you click the button. Nifty! (and just like in Delphi)
- Open Unit1.cpp (double click on it in the Project Manager)
- In the menu, select File / Use Unit...
Select About.cpp and click OK.
- Switch to Design mode.
- Add a second TButton to Form1 and set its Caption to About.
- Double click Button2 and complete the event handler as follows:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
FormAbout->ShowModal();
}
- Save all your files.
- Test the application.
Some differences between C++ and Object Pascal
|
|