C++ Basic Syntax
When considering a C++ program, it can be characterized as a compilation of objects that interact by invoking one another’s methods. Let’s now take a brief look into the m
eanings of a class, object, methods, and instance variables.- Object − Objects encompass both states and behaviors. For instance, a dog possesses states such as color, name, breed, as well as behaviors like wagging, barking, and eating. An object is an exemplar of a class.
- Class − A class is a template or blueprint that outlines the behaviors and states that objects of its type can exhibit.
- Methods − Methods essentially represent behaviors. Within a class, multiple methods can be included. Methods encompass the logic, data manipulation, and execution of actions.
- Instance Variables − Each object maintains its distinct set of instance variables. The state of an object is formed by the values attributed to these instance variables.
Structure of a C++ Program
Let’s examine a simple code snippet that outputs the phrase “PiEmbSysTech.”
Demo
using namespace std;
// The main() function marks the inception of program execution.
int main() {
cout << "PiEmbSysTech"; // displays PiEmbSysTech
return 0;
}
Let’s delve into the various components of the aforementioned program −
- The C++ language defines several headers containing information crucial or beneficial to your program. In this program, the header is essential.
- The line using namespace std; instructs the compiler to utilize the std namespace. Namespaces are a relatively recent addition to C++.
- The subsequent line ‘// main() is where program execution begins.’ is a single-line comment in C++. Single-line comments start with // and extend to the end of the line.
- The line int main() represents the main function, where program execution commences.
- The following line cout << “Hello World”; leads to the display of the message “
PiEmbSysTech
” on the screen. - The next line return 0; concludes the main() function and imparts a return value of 0 to the calling process.
Compiling and Running the C++ Program
Let’s explore the process of saving the file, compiling, and executing the program. Kindly follow the steps below −
- Open a text editor and input the code as provided.
- Save the file as:
PiEmbSysTech
.cpp - Access a command prompt and navigate to the directory where the file is saved.
- Type ‘g++
PiEmbSysTech
.cpp’ and hit Enter to compile the code. If your code contains no errors, the command prompt will proceed to the next line, generating an executable file named a.out. - Now, type ‘a.out’ to run the program.
- You will observe ‘
PiEmbSysTech
‘ displayed on the window.
$ g++ PiEmbSysTech.cpp
$ ./a.out
PiEmbSysTech
Ensure that g++ is accessible via your path and that you are executing the command in the directory containing the PiEmbSysTech
.cpp file.
C++ Semicolons and Code Blocks
In C++, the semicolon functions as a statement terminator. In other words, each individual statement must conclude with a semicolon, signifying the culmination of a logical unit.
For instance, the following are three distinct statements:
x = y;
y = y + 1;
add(x, y);
A code block encompasses a set of logically linked statements enclosed within opening and closing braces. For example:
{
cout << "Hello World"; // displays Hello World
return 0;
}
C++ does not interpret the end of a line as a terminator. Consequently, the placement of a statement within a line does not affect its meaning. For instance:
x = y;
y = y + 1;
add(x, y);
is equivalent to
x = y; y = y + 1; add(x, y);
C++ Identifiers
A C++ identifier designates a name used to identify a variable, function, class, module, or any other user-defined element. An identifier commences with a letter (A to Z or a to z) or an underscore (_), followed by zero or more letters, underscores, and digits (0 to 9).
Punctuation characters like @, $, and % are not permitted within C++ identifiers. C++ is case-sensitive, distinguishing between identifiers such as Manpower and manpower.
Here are some examples of acceptable identifiers:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal"
C++ Reserved Keywords
The subsequent compilation enumerates the reserved terms in C++. These reserved terms hold a distinct function and cannot be employed as identifiers for constants, variables, or any other purposes.
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 |
Trigraph Sequences
Certain characters possess an alternative representation, known as a trigraph sequence. A trigraph entails a three-character arrangement signifying a solitary character, with the sequence always commencing with two question marks.
Trigraphs are expanded wherever they occur, encompassing string literals, character literals, comments, and preprocessor directives.
Here are some frequently employed trigraph sequences:
Trigraph | Replacement |
??= | # |
??/ | \ |
??’ | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
It’s noteworthy that not all compilers support trigraphs, and their usage is generally discouraged due to their potential to cause confusion.
Whitespace Usage in C++
A line exclusively comprising whitespace, perhaps accompanied by a comment, is recognized as a blank line, which is entirely disregarded by the C++ compiler.
In C++, whitespace encompasses blanks, tabs, newline characters, and comments. Whitespace plays a pivotal role in distinguishing one segment of a statement from another, allowing the compiler to differentiate between elements within a statement, like int.
Statement 1
int age;
In the above statement, there should be at least one whitespace character (typically a space) between int and age to enable the compiler to differentiate them.
Statement 2
fruit = apples + oranges; // Calculate total fruit
In the statement above, no whitespace characters are necessary between fruit and =, or between = and apples, although you are welcome to incorporate some for enhanced readability.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.