Home | Computer | Basic Syntax of C++ Program

Basic Syntax of C++ Program

August 19, 2022

Every language has its syntax which means every language has some rules and protocols. In C++ language the syntax must be error-free.  If some error occurs is C++ syntax the program will not be executed.

C++ Program

C++ programs can be thought of as a bunch of different objects that communicate with each other by calling each other’s methods. Let’s discuss the Object, Class, Method, and variable in detail.

Object

States and behaviors are two qualities that every object has. For example, a cow has states like color, name, and breed. But a cow also has behaviors, like wagging its tail, moo, and eating. An object is an example of a class.

Class

A class is like a template or a blueprint that defines the behavior or states that an object of its type supports.

Methods

In C++, behaviors are called methods. And methods are pretty darn important because they help us do everything from writing ideas down to processing data and everything in between. A single class can have multiple methods.

Instance variables

Every object is special and has slightly different instance variables. The state of an object comes from the values that are assigned to these instance variables.

Syntax of C++ program

#include<iostream>

Using namespace std;

int main()  {

Cout<<“Hello World”;

Return 0;

}

  • Every C++ program needs at least one header file. The <iostream> header file includes information that allows the program to perform input and output operations.
  • The line using namespace std; is telling the compiler that the std namespace is being used. Namespaces were a relatively recent addition to C++.
  • The main function is where the execution of your program starts.
  • cout (pronounced “see-out”) is an object used together with the insertion operator (<<) to output/print text. In the above example, the output will be “Hello World”.
  • In C++, a semicolon is like a period at the end of a sentence. Every C++ sentence must have one, or else the compiler will get confused. It’s important to remember that the semicolon goes at the end of the line, not before hitting return like you might be used to with other languages.

int a,b;

a=6,b=2;

cout<<b<<a;