DEV Community

Rohit Juyal
Rohit Juyal

Posted on • Edited on

Day 02

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
*/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ 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 <iostream>
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  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;
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ 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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก 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
Enter fullscreen mode Exit fullscreen mode

Or in one line:

int abc = 100; // Initialization
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”’ 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
Enter fullscreen mode Exit fullscreen mode

โœ… 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.

Top comments (0)