PlanetSquires Forums

Support Forums => C++ Journey => Topic started by: Paul Squires on June 03, 2020, 10:51:06 PM

Title: Language Differences between C++ and BASIC’s
Post by: Paul Squires on June 03, 2020, 10:51:06 PM
I won’t list everything that I encounter because obviously the differences between the two languages is immense, however, it is good to dive into topics where the “BASIC way of doing things” is somewhat different than C++.

Terminating statements with semi colon “;”. The problem here is not so much forgetting to do it, but rather remembering when to do it because some things are not terminated with ; whereas other things are. For example, For and Loops and Function bodies are not ; terminated, whereas regular statements, namespaces, classes, are.

Namespaces and the Using statement. From my reading and watching tutorial videos, it became quickly apparent that the general consensus is *not* to use the Using keyword, but rather to prepend statements with the namespace itself. This is because a lot of C++ is in various namespaces and you can quickly run into trouble when trying to switch from namespace to namespace with the Using statement. For example, the std namespace for Standard Library functions is extremely but you should still use the prefix approach nonetheless:
    std::cout << szText << std::endl;

Capitalization. This has already bitten me more than once! Obviously C++ is case sensitive but coming from BASIC I sometimes look at a variable or keyword and wonder why it is not compiling only to realize that I am not spelling it correctly.

Strings and WStrings. Thankfully, C++ has a very strong a robust string class that is fast and handles Unicode easily.
Only issue so far is the concatenation of multiple strings and/or string literals all on one line of code. In BASIC it’s easy, but with C++ the individual strings need to be enclosed in functions in order to make it work. This is only for multiple strings (concatenating two strings works the same in C++ as it does in BASIC):

std::wstring fullstring;
fullstring = std::wstring(L"Hello!") + std::wstring(L" How are you?") + L" End with a literal wide string!"
std::cout << fullstring << std::endl;

WString literals need to be prefixed with an “L”.    L”Paul Squires”

Function parameter default values. You should only define them in the header’s prototype for the function definition, not also in the function itself. In BASIC, we’d put them in both places.

By default, C++ passes all variables by value. This can a performance issue when trying to pass things like large strings or classes into a function. Copies of those incoming elements need to be made. In C, you’d pass a pointer to the object and manipulate that object’s value within the function. In C++, the preferred approach is to pass a reference to the object. You can also pass const with the reference to prevent the function from modifying this income variable. This is especially useful for strings and wstrings. You merely append an ampersand & to the end of the variable type in order to pass it by reference.

Code: [Select]
int myFunction(const std::wstring& wszText)
    std::wcout << wszText << std::endl;

When I call class methods that do not have any parameters, I keep forgetting to end the statement in empty parentheses. For example:

At 1:57 PM on May 30, 2020, I was finally able to get a window to display using g++ and my modified version of Jose Roca’s CWindow class. Using several default parameters, the following code simply displays the window. I’m make progress slowly but I’m learning lots as I go along.

One thing that I have learned…. YOU LEARN BY DOING!!!! Reading and watching tutorials is important but it is no substitute for getting your hands dirty and writing code and learning through trial and error. The concepts get reinforced so much faster that way.

Code: [Select]
// Main entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
    Afx::CWindow win;
    win.Create(0, L"This is my first window", WindowProc);

    return 0;

C++ does not use any “_” line continuation underscores at the end of the line to continue a statement to the next line. Everything is processed and line feeds ignored until the ; is encountered.

For loops, If statements: Do not need to be enclosed in curly braces if they are only one line after the For or If.
for (i = 0; i < 5; i++)

Switch statements only work with integral data types. You can not use a string or wstring class to compare against a string literal. You have to use if/elseif/else statement instead.

Uppercase/Lowercase. This was interesting. There are no built in methods on the string or wstring class to convert the string to upper or lowercase. All my research pointed to either looping the string and converting each character, or using the transform function:
Code: [Select]
#include <algorithm>
std::transform(wsClassName.begin(), wsClassName.end(), wsClassName.begin(), ::toupper);

When the string is transformed to uppercase then you can easily do the comparisons:
if (wsClassName == L"BUTTON") {
    // Adds a button to the window
    if (dwStyle == -1)

Similarly, you can use the compare method of the string class to do the comparison. Returns 0 if the strings are a match:
Code: [Select]
    if ("BUTTON1") == 0) {
        std::cout << "make a button" << std::endl;

Passing strings or wstrings to win32 api functions requires sending it as wszTitle.c_str()

I am leaning a lot on to help with a lot of questions that I have. Every question that I have thought of so far has been answered there over the years.

I am quickly starting to enjoy the brevity that C++ affords. It is less verbose than BASIC especially in relation to setting up the subs/functions and the IF’s, FOR’s, LOOP’s, etc. However, it can be more verbose in areas where you need to work with the standard library or templates. For example, things like I previously mentioned for converting a string to uppercase, or prepending the namespace to the function calls.

Dimensioning variables is so much nicer in C++.
int nHeight = 0;
DIM AS LONG nHeight = 0

Need to remember that the division operator is “/” rather than “\”. Using a backslash “\” usually indicates that you are about to define a literal character.

Don’t forget that == is used to test if things are equal, whereas = is an assignment operator.

Once you start to get used to the keyboard shortcuts in Visual Studio Code you can quickly become VERY productive.