C++ Basic Syntax and Program Comments

C++ programs can be defined as a collection of objects that interact with each other by calling their methods.

Objects
Objects have states and behaviors. For example: a dog’s state – color, name, breed; behavior – wagging, barking, eating. Objects are instances of classes.
Classes
Classes can be defined as templates/blueprints that describe the behaviors/states of objects.
Methods
Essentially, a method represents a behavior. A class can contain multiple methods. Logic can be written in methods, data can be manipulated, and all actions can be performed.
Instance Variables
Each object has its unique instance variables. The state of an object is created by the values of these instance variables.

01

Program Structure

Example: Hello World
#include <iostream> using namespace std; int main() {   // Output Hello World   cout << "Hello World";    return 0;}
The C++ language defines some header files that contain necessary or useful information for the program; the above code includes the header file <iostream>.
using namespace std; tells the compiler to use the std namespace, which is a relatively new concept in C++.
int main() is the main function where the program starts executing.
cout << “Hello World”; will display the message “Hello World” on the screen.
return 0; terminates the main() function and returns the value 0 to the calling process.

02

Compile & Execute

How to save the source code in a file, compile, and run it.
Simple steps are as follows:
Open a text editor and add the above code.
Save the file as hello.cpp.
Open the command prompt and navigate to the directory where the file is saved.
Type ‘g++ hello.cpp’, press enter to compile the code.
If there are no errors in the code, the command prompt will move to the next line and generate the a.out executable file.
Type ‘a.out’ to run the program.
You should see ‘Hello World’ displayed on the screen.

C++ Basic Syntax and Program Comments

03

Semicolons & Statement Blocks

In C++, semicolons are statement terminators, indicating the end of a logical entity.
x = y; y = y + 1; add(x, y);
Statement blocks are a set of logically connected statements enclosed in curly braces.
{   // Output Hello World   cout << "Hello World";    return 0;}

04

Identifiers, Keywords, and Trigraphs

1. C++ identifiers are names used to identify variables, functions, classes, modules, or any other user-defined items.
An identifier starts with a letter A-Z or a-z or an underscore _, followed by zero or more letters, underscores, and digits (0-9).
Punctuation characters like @, &, and % are not allowed in C++ identifiers. C++ is a case-sensitive programming language.
Thus, in C++, Manpower and manpower are two different identifiers.
The following are all valid identifiers:
model       zero    abc   move_Nm  a_1234 myName110   _temp   i     a123b9      returnVal
2. Keywords

C++ Basic Syntax and Program Comments

3. Trigraphs

Trigraphs are three-character sequences used to represent another character, also known as trigraph sequences. Trigraph sequences always start with two question marks.

C++ Basic Syntax and Program Comments

Program comments are explanatory statements that can be included in C++ code, which will improve the readability of the source code.
All programming languages allow some form of comments.
C++ supports single-line comments and multi-line comments.
All characters in comments are ignored by the C++ compiler.
C++ comments generally come in two forms:
//
Generally used for single-line comments.
/* … */
Generally used for multi-line comments.

C++ Basic Syntax and Program Comments

01

//

Comments start with // and continue until the end of the line.

#include <iostream> using namespace std; int main() {  // This is a comment  cout << "Hello World!";  return 0;}

They can also be placed after statements:

#include <iostream> using namespace std; int main() {   cout << "Hello World!"; // Output Hello World!    return 0;}
02

/* */

C++ comments start with /* and end with */.

#include <iostream> using namespace std; int main() {    /* This is a comment */     /* C++ comments can also     * span multiple lines     */     cout << "Hello World!";    return 0;}
03

Note

Within /* and */ comments, // characters have no special meaning. Within // comments, /* and */ characters also have no special meaning. Therefore, you can nest one type of comment within another.
/* Comment for outputting Hello World cout << "Hello World"; // Output Hello World */
——- END ——-
Disclaimer

This article includes some images and text sourced from the internet.

If there are copyright issues, please contact us promptly.

Leave a Comment