When you switch programming languages, you need to learn how that language works. What we have learnt about C#, converts quite easily to C++. Let’s have a quick look at the main differences.
Different languages…
have different naming conventions. In C# method names use PascalCase
, while the equivalent functions/procedures use snake_case
in C++.
use different names for their types. C# and C++ include int, double, bool, and string.
have different control flow statements, though most are similar. The control flow statements we have used in C# are the same in C++.
support different capabilities when working with strings. In C# we used string interpolation to create strings, while in C++ you have to use concatenate to join together the parts of the strings.
have different libraries. The C# and C++ languages have different libraries, but the SplashKit library is available across both languages.
The C++ code for…
Variable and constant declarations use the same syntax we were using in C#. Declare variables using int age; string name;
and constants using const int MAX_WIDTH = 300
. Our C++ coding convention will use snake_case
variable names.
Function and procedure calls use the same syntax write_line("Hello World");
. Our C++ coding convention will use snake_case
for function and procedure names.
If statements remain the same as we were doing in C#.
Switch case statements are similar , but are limited to integer values. So no more switch based on a string, for example.
While loops remain the same as we were doing in C#.
Do while loops remain the same as we were doing in C#.
For loops remain the same as we were doing in C#.
Example
The following code is the C++ version of the menu code from the control flow chapter.
write_line ( " 1: Addition " );
write_line ( " 2: Subtraction " );
write_line ( " 3: Multiplication " );
write_line ( " 4: Division " );
write ( " Enter your choice: " );
while ( ! is_integer (line))
write_line ( " Please enter a whiole number " );
write ( " Enter your choice: " );
write_line ( " Subtraction " );
write_line ( " Multiplication " );
write_line ( " Invalid choice " );