Using C++
Creating a C++ application
Compiling and linking the C++ application
Setting a breakpoint and executing to it
Printing the Fibonacci numbers
* In this tutorial, C++ is used to create a C++ class. The class is then used for creating two independent objects, and the application is built and debugged. We also show an example of how to set a conditional breakpoint.
* This tutorial assumes that you are familiar with the basics of the IAR Embedded Workbench® IDE described in the previous tutorial.
Creating a C++ application
This tutorial demonstrates how to use the C++ features. The tutorial consists of two files:
*
Fibonacci.h and Fibonacci.cpp define a class Fibonacci that can be used to extract a series of Fibonacci numbers
*
CppTutor.cpp creates two objects, fib1 and fib2, from the class Fibonacci and extracts two sequences of Fibonacci numbers using the Fibonacci class.
To demonstrate that the two objects are independent of each other, the numbers are extracted at different speeds. A number is extracted from fib1 each turn in the loop while a number is extracted from fib2 only every second turn.
The object fib1 is created using the default constructor while the definition of fib2 uses the constructor that takes an integer as its argument.
Compiling and linking the C++ application
1
In the workspace tutorials used in the previous tutorials, create a new project, project3.
2
Add the files Fibonacci.cpp and CppTutor.cpp to project3.
3
Choose Project>Options and make sure these options are selected:
 
All you have to do to switch to the C++ programming language, is to select the C++ language and the dialect C++.
4
Choose Project>Make to compile and link your application.
Alternatively, click the Make button on the toolbar. The Make command compiles and links those files that have been modified.
5
Choose Project>Debug to start C-SPY.
Setting a breakpoint and executing to it
1
2
To see how the object is constructed, set a breakpoint on the C++ object fib1 on this line:
Fibonacci fib1;
3
Choose Debug>Go, or click the Go button on the toolbar.
The cursor should now be placed at the breakpoint.
4
To step into the constructor, choose Debug>Step Into or click the Step Into button in the toolbar. Then click Step Out again.
5
Step Over until the line:
std::cout << fib1.next();
Step Into until you are in the function next in the file Fibonacci.cpp.
6
Use the Go to function button in the upper right corner of the editor window and double-click the function name nth to find and go to the function. Set a breakpoint on the function call nth(n-1) at the line
value = nth(n-1) + nth(n-2);
7
It can be interesting to backtrace the function calls a few levels down and to examine the value of the parameter for each function call. If you add a condition to the breakpoint, the break will not be triggered until the condition is true, and you will be able to see each function call in the Call Stack window.
To open the Breakpoints window, choose View>Breakpoints. Select the breakpoint in the Breakpoints window, right-click to open the context menu, and choose Edit to open the Edit Breakpoint dialog box.
Set the value in the Skip count text box to 4 and click OK.
Looking at the function calls
8
Choose Debug>Go to execute the application until the breakpoint condition is fulfilled.
9
When C-SPY stops at the breakpoint, choose View>Call Stack to open the Call Stack window.
Five instances of the function nth are displayed on the call stack. Because the Call Stack window displays the values of the function parameters, you can see the different values of n in the different function instances.
You can also open the Register window to see how it is updated as you trace the function calls by double-clicking on the function instances.
Printing the Fibonacci numbers
1
2