– basic C++ syntax

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.

C++ program structure

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

using namespace std; Int main() {cout << "Hello World"; // 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. The above program contains the iostream 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.

The semicolon & statement block in C++

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"; // 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
Copy the code

C + + keyword

The following table lists 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	 
Copy the code

Three character groups

A three-character sequence is a sequence of three characters used to represent another character, also known as a three-character sequence. Three-character sequences always start with two question marks.

Three-character sequences are less common, but the C++ standard allows certain characters to be specified as three-character sequences. This used to be a necessary way to represent characters that weren’t on the keyboard.

Three-character sequences can appear anywhere, including strings, character sequences, comments, and preprocessing instructions.

The most commonly used three-character sequences are listed below:

Three character groups replace
?? = #
?? / \
?? ‘ ^
?? ( [
?? ]
?? ‘
?? < {
?? > }
?? – ~
If you want two consecutive question marks in a source program and do not want them replaced by the preprocessor, as in character constants, string literals, or program comments, you can use automatic concatenation of strings: “… ?” “? …” Or escape the sequence: “… ?? …” .

Starting with Microsoft Visual C++ 2010, the compiler does not automatically replace three-character groups by default. If you want to use three-character substitution (for example, to be compatible with older software code), you need to set the compiler command line option /Zc: Trigraphs

G++ still supports three-character groups by default, but will give a compilation warning.

Whitespace in C++

Lines that contain only Spaces, called blank lines, may have comments that the C++ compiler ignores completely.

In C++, Spaces are used to describe whitespace, tabs, newlines, and comments. Spaces separate parts of a statement, allowing the compiler to identify where an element in the statement (such as int) ends and where the next element begins. Therefore, in the following statement:

int age;
Copy the code

Here, there must be at least one space character (usually a whitespace) between int and age so that the compiler can distinguish between them. On the other hand, in the following statement:

fruit = apples + oranges; // Get the total number of fruitsCopy the code

The Spaces between fruit and =, or = and apples are not required, but for readability, you can add Spaces as needed.