C++ : backend compatibility.

Objectives: This is example we will review “Back-end compatibility of C++”, and test environment. Here we used Eclipse IDE as a model-template.

  • Project Template:  When you are testing a new piece of code, you may consider to create a test-project or test-source-file as a template, than creating a new project or source file. You may simply overwrite the existing code with the new ones, and  run  it.  Here we will learn how to use a source file of an existing project as a template. You  can simply alter the existing codes and run. There are many open source compilers or IDEs available, each will have its own distinct features; therefore the outlook of a project will vary considerably among these applications.
  • Eclipse IDE: In this example we opened the source file containing, int main() function, of a project created earlier with Eclipse IDE, and then replaced the existing codes were  with new ones. Please note that, Eclipse is a java application, and you need to add C++ Developer Tools (CDT )as a plug-in.



     
  • Quincy 2005 Editor:  If you are using Quincy 2005 please review this example, Know_Your_Environment1.htm, you can create a stand-alone source file like, "test.cpp", as a template.
  • Your choice of IDE and restrictions:
    The scanf() function of C language may hold-up or freeze the console window of Eclipse workspace. The input  ("scanf()") and output ("printf()") functions of C language are replaced with "iostream" objects like"cin"and "cout"  of  C++ programming language.
  • File Name : compare_c_cplus.cpp
  • Code: compare_c_cplus2.txt
 

Step: 1 Open up a stored project  in Eclipse workspace.

If you don't have this template made, please refer to the section "know your environment" and look for " Hello_World_CPP.htm " , where you would find how to use Eclipse IDE and other tips. Please note, in the screenshot below, Test1.cpp pane had some codes which I used test before.
 

Step: 2  The illustration below, shows that Eclipse IDE did not support scanf(), gets(..) and few other functions.

When you add those two header files, the code will compile, but won't display any output.

The console window did not display any output during the run-time, and did not take any input from the keyboard. To terminate this example, click on the red-square.

Now, edit the codes again, use "cin" object instead of "scanf()" function, and this time codes will compile, and would also read all the input from keyboard, as shown in the next step.

Step: 3 Runtime Views:  In this example we may notice that "cout" and "cin" objects worked along with printf() function side-by-side; this was not the case with scanf() function. The "cout" (console out)  object does all the formatting for you, and whereas with print() function of C language, you are required to format the output.

Step: Brief Discussion:

The function scanf()  in C language is used to get user's input from the keyboard. Similarly, in C++ language,  a stream-object "cin (console input)" and has been optimized to read characters typed from the keyboard.

By origin "cin and cout" are the instances of istream and ostream classes respectively. A pair of operators, namely, extraction (>>) and insertion (<<) operators are overloaded for input and output operations respectively with "cin" and "cout" objects. 

The scanf() did not work on Eclipse platform, and stunned the application at the runtime.

C++ language has many backend capability to accommodate C functions, but may behave strange on C++ platform. In this example, we noticed that on Eclipse platform, scanf() function had arrested the application during run time; to exit the run-time mode, you have to click on the terminate (red-square) button .

Returning a function with return or exit(0)", (numeric zero), means normal completion of function and function should terminated with no error. And, returning function with

Special Notes: As a knowledge FAQ, the cout object has a bit of overhead to maneuvering the display, and although it is not noticeable, it would be slower than prinf() function.