Fundamentals

Sample C++ program

#include <iostream>
using namespace std;
int main() {
    cout << "Hello World!!";
    return 0;
}  

#include <iostream> : iostream is a inbuilt header library which allows you to deal with input and output objects like cout etc.

using namespace std : The above line means that the object and variable names can be used from standard library.

cout : cout is used to print the output.

main() : main function is the entry point of any C++ program.

return 0 : To end the main function

Compile & Run a C++ Program

How to compile a program in C++

  • Open your terminal, navigate to the directory where you have saved your program. Consider firstprogram.cpp is the name of your program.
sudo g++ -o firstprogram firstprogram.cpp

How to run a C++ program

./firstprogram

Using Onecompiler

  • You don't need to install any software or compiler.
  • Just goto OneCompiler and enjoy programming without any installation.

Comments

  1. Single line comments

// is used to comment a single line

  1. Multi line comments

/**/ is used to comment multiple lines.