Written on: 06 April, 2025.

📝 Comments in C++

We use comments to explain what a code do. It is ignored by compiler and is used for other developers:

// This is a single-line comment

/*
This is a 
multi-line comment
*/

📦 Preprocessor Directives

At the top of most C++ programs, you’ll often see lines starting with #. These are preprocessor directives, and they give instructions to the compiler before the actual compilation starts.

#include

This line tells the compiler to include the iostream file — a pre-written file that contains useful code for input and output operations.


💬 Input/Output in C++

To print something on the screen in C++, we use:

std::cout << "Hello, world!" << std::endl;

Let’s break it down:

  • std stands for standard.
  • cout is the function used for console output.
  • << is the insertion operator (used to send output to the screen).
  • endl means end line, just like pressing “Enter”, it moves cursor to new line.

Another way of writing is:

using namespace std;

This line allows you to skip writing std:: every time. But be careful! Importing the entire std namespace can be heavy in larger programs. A better approach is:

using std::cout;
using std::endl;

🧠 Namespaces

A namespace is like a region that allow us to group names for variables etc to avoid conflicts. "std" is standard C++ library for names.
We can also create our own namespaces.

Example:

namespace my_own {
    void do_something() {
        // do some thing
    }
}

int main() {
    my_own::do_something(); // calling the function inside the namespace
    return 0;
}

🌐 Real-World Namespace Examples

Here are some common namespaces you’ll see in C++:

  • std → Standard C++ Library (e.g., std::cout, std::endl)

  • cv → OpenCV library (e.g., cv::Mat, cv::imread)

  • Eigen → Eigen library for linear algebra (e.g., Eigen::MatrixXd)


📌 Functions: What Are They?

In simple words, a function is a name given to a block of code that performs a specific task. It allows us to reuse code and makes our programs more organised.

int addNumbers() {
    // code to add numbers
    return 0;
}

💡 Key Point:

Every function returns a value. We use return 0; to indicate that the function is executed successfully.

🛠️ Methods: Functions Inside Objects

Methods are also functions, but they belong to objects or classes. I will learn about objects later.

Even the main() function is technically a method. It's the starting point of any C++ program.


📚 Variables

To store data, we use variables.

int abc;       // Declaration
abc = 100;     // Assignment

Or in one line:

int abc = 100; // Initialization

There are certain keywords which we can't use as variable names, as they have meaning in C++.


🎯 Why main.cpp?

Big projects often contains many files, so it’s common to name your main file main.cpp. It makes us clear where the program begins execution. This is just a naming convention for better understanding and structuring.


💻✨ Data Types in C++

The data we store in variables, is divided into categories called data types. Each data type tells the compiler what kind of value a variable will hold. This avoids confusion and prevents error.


🔢 int – Integer Type

The int data type is used to store whole numbers — that is, numbers without decimal points.

int age = 25;
int year = 2025;
int temperature = -10;

🔒 Constant Values

If you don’t want a variable’s value to change, declare it as const:

const int abc = 231;
// abc = 200;  ❌ This will give an error

✅ Summary

  • Add comments to keep your code readable.
  • Use #include to bring in standard or custom files.
  • Use std::cout or using namespace std for output.
  • Namespaces help organize and avoid conflicts in code.
  • Functions do specific tasks and return values.
  • Methods are functions connected to objects.
  • int data type is used for storing whole numbers.
  • Use const to lock variable values.

About me(a little more detail): Hi, I am Rohit. I started my career late in life i.e. last year, right now I am 35. I am currently working in BPO. My aim is to get into HFT. I know it's not a joke & age and other factors not on my side. But let's see how things goes on. A year earlier I was just sitting on my desk unemployed hoping for life to become better. If not that I will be happy trying.