C++ programs can be defined as collections of objects that interact by calling each other’s methods. Now let’s take a quick look at what classes, objects, methods, and immediate variables are.

Objects – Objects have state and behavior. For example: a dog’s state – color, name, breed, behavior – shaking, barking, eating. An object is an instance of a class.

Class – A class can be defined as a template/blueprint that describes the behavior/state of an object.

Method – Basically, a method represents a behavior. A class can contain multiple methods. You can write logic, manipulate data, and perform all actions in a method.

Instant variables – Each object has its own unique instant variables. The state of the object is created from the values of these immediate variables.

Let’s look at a simple piece of code that prints the word Hello World.

The instance

#include <iostream>
using namespace std;
Copy the code

// Main () is where the program starts execution

int main(a)
{
   cout << "Hello World"; // Output Hello World
   return 0;
}

Copy the code

Let’s take a look at the above procedure:

The C++ language defines header files that contain necessary or useful information in a program. In this program, we have a header file.

The next line using namespace STD; Tell the compiler to use the STD namespace. Namespaces are a relatively new concept in C++. The next line // main(), where the program begins execution, is a one-line comment. Single-line comments start with // and end at the end of the line. The next line, int main(), is the main function from which the program starts. Next line cout << “Hello World”; The message “Hello World” is displayed on the screen. Next line return 0; Terminates main() and returns the value 0 to the calling process.

Compile and execute C++ programs

Let’s look at 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 hello.cpp. Open a command prompt and go to the directory where you saved the file. Type ‘g++ hello.cpp ‘, enter enter, and compile the code. If there are no errors in the code, the command prompt jumps to the next line and generates the A.out executable. Now, type ‘a.out’ to run the program. You can see ‘Hello World’ on the screen.

$ g++ hello.cpp
$ ./a.out
Hello World
Copy the code

Make sure you have the g++ compiler in your path, and make sure you run it in the directory that contains the source file hello.cpp. You can also use makefiles to compile C/C++ programs.

In C++, a semicolon is a statement terminator. 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);
Copy the code

A statement block is a group of logically connected statements enclosed in braces. Such as:

{
   cout << "Hello World"; // Output Hello World
   return 0;
}
Copy the code

C++ does not identify the end of a line as an terminator, so you can place multiple statements on a single line. Such as:

x = y;
y = y+1;
add(x, y);
Copy the code

Is equivalent to

x = y; y = y+1; add(x, y);
Copy the code

C + + identifier

A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier begins with the letters A-Z or A-Z or underscore _, followed by zero or more letters, underscores, and numbers (0-9).

C++ identifiers do not allow punctuation characters, such as @, &, and %. C++ is a case-sensitive programming language. Therefore, Manpower and Manpower are two different identifiers in C++.

Here are a few valid identifiers:

mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal