A C++ program can be defined as a collection of objects that interact by calling each other's methods. Now let's take a brief look at what classes, objects, methods, and real-time variables are.
Object - Objects have state and behavior. For example: the status of a dog - color, name, breed, behavior - shake, yell, eat. An object is an instance of a class.
Class - Classes can be defined as templates/blueprints that describe object behavior/state.
Methods - Basically, a method represents a behavior. A class can contain multiple methods. Logic, operational data, and all actions can be written in the method.
Real-time variables - Each object has its own unique real-time variable. The state of an object is created by the values of these instant variables.
C++ program structure
Let's look at a simple piece of code that can output the word Hello World.
#include <iostream> using namespace std; // main() is where the program starts executing int main() { cout << "Hello World"; // Output Hello World return 0; }
Next, let’s explain the above program:
- The C++ language defines header files that contain necessary or useful information in the program. The above program contains the header file.
- line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively new concept in C++.
- Next line // main() is where the program starts executing. It is a single-line comment. A single line comment begins with // and ends at the end of the line.
- The next line int main() is the main function, and the program starts executing from here.
- The next line cout << "Hello World"; will display the message "Hello World" on the screen.
- Next line return 0; terminates the main( ) function and returns a value of 0 to the calling process.
Compile & execute C++ programs
Next let's see how to save the source code in a file and how to compile and run it. Here are the simple steps:
- Open a text editor and add the above code.
- Save the file as .
- Open the command prompt and enter the directory where the file is saved.
- Type 'g++', enter Enter, and compile the code. If there are no errors in the code, the command prompt will jump to the next line and generate an executable file.
- Now, type ' ' to run the program.
- You can see 'Hello World' displayed on the screen.
$ g++ $ ./
Hello World
Make sure that the g++ compiler is already included in your path and make sure that it is run in the directory containing the source file.
You can also use makefile to compile C/C++ programs.
Semicolons & blocks in C++
In C++, a semicolon is a statement ending character. That is, each statement must end with a semicolon. It indicates the end of a logical entity.
For example, here are three different statements:
x = y; y = y+1; add(x, y);
A block is a set of logically connected statements enclosed in braces. For example:
{ cout << "Hello World"; // Output Hello World return 0; }
C++ does not use the end of the line as the ending character, so you can place multiple statements on a line. For example:
x = y; y = y+1; add(x, y);
Equivalent to
x = y; y = y+1; add(x, y);
C++ identifiers
A C++ identifier is the name used to identify variables, functions, classes, modules, or any other user-defined project. An identifier begins with the letter A-Z or a-z or underscore_ followed by zero or more letters, underscores, and numbers (0-9).
Punctuation characters such as @, $, and % are not allowed in C++ identifiers. C++ is a case-sensitive programming language. Therefore, in C++, Manpower and manpower are two different identifiers.
Here are a few valid identifiers:
mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal
C++ keywords
The following table lists the reserved words in C++. These reserved words cannot be used as constant names, variable names, or other identifier names.
asm | else | new | this |
---|---|---|---|
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
Three-character group
A three-character group is a three-character sequence used to represent another character, also known as a three-character sequence. A three-character sequence always starts with two question marks.
Three-character sequences are less common, but the C++ standard allows certain characters to be specified as three-character sequences. Previously, this was an indispensable method to represent characters that were not on the keyboard.
A three-character sequence can appear anywhere, including strings, character sequences, comments, and preprocessing instructions.
The most commonly used three-character sequences are listed below:
Three-character group | replace |
---|---|
??= | # |
??/ | \ |
??' | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
All compilers do not support three-character groups. To avoid confusion, it is not recommended to use three-character groups.
Spaces in C++
Lines containing only spaces, called blank lines, may have comments, which the C++ compiler ignores completely.
In C++, spaces are used to describe whitespace characters, tab characters, line breaks, and comments. Spaces separate parts of a statement, allowing the compiler to identify where an element (such as an int) ends and where the next element begins. Therefore, in the following statement:
int age;
Here, there must be at least one space character (usually a whitespace character) between int and age so that the compiler can distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // Get the total number of fruits
The space characters between fruit and =, or = and apples are not required, but to enhance readability, you can add some spaces as appropriate as you want.
C++ Comments
Annotations for a program are explanatory statements that you can include in your C++ code, which will improve the readability of the source code. All programming languages allow some form of annotation.
C++ supports single-line comments and multi-line comments. All characters in the comments are ignored by the C++ compiler.
C++ comments start with / and terminate with /. For example:
/* This is a comment */ /* C++ comments are OK * Cross-track */
Comments can also start with // until the end of the line. For example:
#include <iostream> using namespace std; main() { cout << "Hello World"; // Output Hello World return 0; }
When the above code is compiled, the compiler will ignore // prints Hello World, and the following results will be produced in the end:
Hello World
Inside the / and / comments, the // character has no special meaning. In the // comment, the / and / characters also have no special meaning. So you can nest another comment inside one comment. For example:
/* Comments for outputting Hello World cout << "Hello World"; // Output Hello World */