C++ programs can be defined as a collection of objects that interact with each other by calling their methods.
Objects have states and behaviors. For example: a dog’s state – color, name, breed; behavior – wagging, barking, eating. Objects are instances of classes.
Classes can be defined as templates/blueprints that describe the behaviors/states of objects.
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.
Each object has its unique instance variables. The state of an object is created by the values of these instance variables.
Program Structure
#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.
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.

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;}
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

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

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.

//
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;}
/* */
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;}
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 */
This article includes some images and text sourced from the internet.
If there are copyright issues, please contact us promptly.