Day-2 C++ Basic to Advance

Day-2 C++ Basic to Advance

What I did on Day 2

I started Day 2 by setting up a coding environment on my computer and wrote my first code in C++, then, I learned about Data Types, Operators and Variables.

Setting up IDE or coding Environment

Before we start programming with C++. We will need an environment to be set up on our local computer to compile and run our C++ programs successfully.

Now there are two ways in which we can set up the environment for coding in our computer system , either by setting up a local environment or by using IDE (Integrated Development Environment).

IDE (Integrated Development Environment)

IDE stands for an integrated development environment. IDE is a software that provides facilities to a computer programmer for developing software.

There are many online IDEs available that you can use to compile and run your programs easily without setting up a local development environment. I will be using VS Code Editor to write and compile my codes.

Setting up Visual Studio Code

Step 1:- You have to start by installing Visual Studio Code as per your system software.

Step 2:- Open the downloaded file and click run.

Step 3:- Accept all the options and click on next and let it install. It may take few minutes to finish.

Now you’ll be able to see the Visual Studio Code icon on your desktop.

Setting up MinGW Compiler

Now to compile and run the code, we need to install a compiler. To run C++ codes, we need to install MinGW Compiler.

Step 1:- Download MinGW from this link.

Step 2:- After installation, check all the packages and click on installation and apply changes.

Now we need to set up mingw path in system environment variables so the our compiler can work on all the files in our system.

Step 3:- Open This PC -> C Drive -> MinGW -> Bin. (Copy this path)

Step 4:- Right, Click on “This PC” -> Properties -> Advanced System Setting -> Environment variables -> (Select PATH in System variables) -> Edit -> New -> Paste the path here and OK.

Step 5:- Go to Visual Studio Code, and Install some useful extensions (from the right sidebar, last icon(probably))-

  • C/C++

  • Code Runner

Yayay!!! You are good to go now. Open any folder, create new files and Save them with the extension “.cpp”.

First Code in C++

Now open a file with the extension ".cpp" and write your first code

#include<iostream>
using namespace std;

int main() {

  cout << " Namaste Dunia " << endl;

  return 0; 
}

here in the above code ,

int main() represents the main function in which the main code or instructions will be written. It returns some integer '0' at the end of the program execution.

cout function is used to print something

Now compiler doesnt know the commands to use cout (print) function, so for that we will declare a file <iostream> which includes certain rules and commands to run particular funtions such as cout

<< we use this after the cout funcyion to return some standard output when the code is compiling.

Data Types in C++

C++ supports a variety of data types that the programmer can select according to the needs of the application.

Data types specify the size and types of values to ve stored.

C++ supports the following data types:

  1. Primary or Built-in or Fundamental data type

  2. Derived data types

  3. User-defined data types

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive data types available in C++ are:

  • Integer

  • Character

  • Boolean

  • Floating Point

  • Double Floating Point

2. Derived Data Types: Data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:

  • Function

  • Array

  • Pointer

  • Reference

3. Abstract or User-Defined Data Types: Data types that are defined by the user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes:

  • Class

  • Structure

  • Union

  • Enumeration

  • Typedef defined Datatype

Primitive Data Types

  • Integer: The keyword used for integer data types is int. Integers typically require 4 bytes of memory space and range from -2147483648 to 2147483647.

  • Character: Character data type is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.

  • Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can store either true or false. The keyword used for the Boolean data type is bool.

  • Floating Point: Floating Point data type is used for storing single-precision floating-point values or decimal values. The keyword used for the floating-point data type is float. Float variables typically require 4 bytes of memory space.

  • Double Floating Point: Double Floating Point data type is used for storing double-precision floating-point values or decimal values. The keyword used for the double floating-point data type is double. Double variables typically require 8 bytes of memory space.

sizeof() operator

It is used to find the number of bytes occupied by a variable/data type in computer memory.

Code of data types and sizeof() operator

#include<iostream>
using namespace std;

int main()
{
    int a = 123;
    cout << a<<endl;

    char m = 'o';
    cout << m <<endl;

    bool bl=true;
    cout << bl;
    cout << '\n';

    float f =1.234;
    cout <<f;

    cout <<endl;

    double d=20.35;
    cout <<d;
}

In this way , you can play with different numbers and characters and this code will give this output

Here is another code in which i used sizeof() operator

#include<iostream>
using namespace std;

int main()
{
    int a = 123;
    cout << a<<endl;

    char m = 'hello';
    cout << m <<endl;

    bool bl=true;
    cout << bl;
    cout << '\n';

    float f =1.234;
    cout <<f;

    cout <<endl;

    double d=20.35;
    cout <<d;

    int size = sizeof(a);
    cout <<endl <<"the size of a is "<<size <<endl;

    int size1 = sizeof(m);
    cout <<endl <<"the size of m is "<<size1 <<endl;

    int size2 = sizeof(f);
    cout <<endl <<"the size of f is "<<size2 <<endl;

    int size3 = sizeof(d);
    cout <<endl <<"the size of d is "<<size3 <<endl;
}

The output this code will be

In this way , you can try different data types and can get friendly with working of the operator.

Did you find this article valuable?

Support Jaival Bhatia's Blog by becoming a sponsor. Any amount is appreciated!