How to Program using C++ - Auxiliary Tech


Monday, April 8, 2019

How to Program using C++

C++ is one of the best and simple programming language for every one...
Enter the following source codes using a programming text editor (such as NotePad++ for Windows or gedit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE)..

these are the Source code
/* * First C++ program that says hello (hello.cpp) *
/ #include <iostream> // Needed to perform IO operations using namespace std; int main() { // Program entry point cout << "hello, world" << endl; // Say Hello return 0; // Terminate main() } // End of main function

Compile and Link (aka Build) the source code "hello.cpp" into executable code ("hello.exe" in Windows or "hello" in UNIX/Linux/Mac).
  • On IDE (such as CodeBlocks), push the "Build" button.
// Windows (CMD shell) - Build "hello.cpp" into "hello.exe" > g++ -o hello.exe hello.cpp // UNIX/Linux/Mac (Bash shell) - Build "hello.cpp" into "hello" $ g++ -o hello hello.cpp

Explanation How to Program using C++
/* ...... */
// ... until the end of the line

These are called comments. Comments are NOT executable and are ignored by the compiler; but they provide useful explanation and documentation to your readers (and to yourself three days later). There are two kinds of comments:
  1. Multi-line Comment: begins with /* and ends with */. It may span more than one lines (as in Lines 1-3).
  2. End-of-line Comment: begins with // and lasts until the end of the current line (as in Lines 4, 7, 8, 9 and 10).
#include <iostream>
using namespace std;
The "#include" is called a preprocessor directive. Preprocessor directives begin with a # sign. They are processed before compilation. The directive "#include <iostream>" tells the preprocessor to include the "iostream" header file to support input/output operations. The "using namespace std;" statement declares std as the default namespace used in this program. The names cout and endl, which is used in this program, belong to the std namespace. These two lines shall be present in all our programs. I will explain their meaning later.
int main() { ... body ... }
defines the so-called main() function. The main() function is the entry point of program execution. main() is required to return an int (integer).
cout << "hello, world" << endl;
"cout" refers to the standard output (or Console OUTput). The symbol << is called the stream insertion operator (or put-to operator), which is used to put the string "hello, world" to the console. "endl" denotes the END-of-Line or newline, which is put to the console to bring the cursor to the beginning of the next line.
return 0;
terminates the main() function and returns a value of 0 to the operating system. Typically, return value of 0 signals normal termination; whereas value of non-zero (usually 1) signals abnormal termination. This line is optional. C++ compiler will implicitly insert a "return 0;" to the end of the main() function.


No comments:

Post a Comment

Contact Form


Email *

Message *