C++ Builder Tutorials

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.

  1. In the Menu, select File / New / VCL Forms Application - C++Builder.
  2. 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.
  3. In the Object Inspector, change the form's Caption to FirstProject
  4. From the Standard page of the Tool Palette, drop a few components on the form: a TEdit, a TButton and a TLabel.
  5. 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).
  6. Now save your files: Menu File / Save All
  7. 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.
  8. 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:

  1. In Design mode, double click on Button1. Automatically, code for an event handler will be created.
  2. Complete the code as follows:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
      Label1->Caption = Edit1->Text;
    }
  3. Save your files and test the project.

Adding a form

Let's add a second form, an "About" dialog:

  1. In the Menu, select File / New / VCL Form - C++Builder
  2. Change the new form's Caption to About and its name to FormAbout
  3. Save the unit as About.cpp (menu: File / Save)
  4. Drop a TLabel on the form and set its Caption to Created with C++Builder
  5. Drop a TBitBtn on the form and set its property Kind to bkOK.
  6. 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)
  7. Open Unit1.cpp (double click on it in the Project Manager)
  8. In the menu, select File / Use Unit...
    Select About.cpp and click OK.
  9. Switch to Design mode.
  10. Add a second TButton to Form1 and set its Caption to About.
  11. Double click Button2 and complete the event handler as follows:
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
      FormAbout->ShowModal();
    }
  12. Save all your files.
  13. Test the application.

Some differences between C++ and Object Pascal

  • In C++, methods of objects are functions. Procedures don't exist.
  • The syntax for writing a method is:
    void __fastcall TObjectClass::MethodName(Parameters) 
    {
      // your code
    }
  • The name of the method is separated by  ::  in contrast to the . (dot, point) in Object Pascal.
  • A block of lines of code is placed between the symbols  {  and  }  in contrast to Object Pascal's keywords begin and end.
  • An object property is indicated by  ->  in contrast to the dot (point) in Object Pascal.
  • C++ uses the assignment operator  =  in contrast to := in Object Pascal.

See also:

  C++Builder vs Delphi