Documenting my CPP Journey Part #3

Documenting my CPP Journey Part #3

Introduction :

Welcome to my first series on #Hashnode where I will be documenting my journey of learning C++ from scratch to advance to develop my programming skills and later make some interesting projects. In this blog, I will be sharing what I learnt in C++ since I wrote my previous blog . In this tutorial, we can study the Data Types, Basic Input/Output, header files and Operators withinside the C++ language. In our last lesson, I mentioned the different variables (their syntax and types), and comments in a C++ program. Do check it out to be up to date with what I will be sharing in this one.

Data Types :

All variables use data type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory.

C++ supports the following data types:

  1. Primary or Built-in or Fundamental data type : 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.

  2. Derived data types: Derived data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types like Function, Array etc

  3. User-defined data types: These data types are defined by the user itself. For example class, structure, enum etc.

In this blog, we will be focusing on Primitive Data Types.

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.

  • void: Void means without any value. void data type represents a valueless entity. A void data type is used for those function which does not return a value.

Basic Input and Output in C++

C++ language comes with different libraries, which help us in performing input/output operations. In C++ sequences of bytes corresponding to input and output are commonly known as streams. There are two types of streams:

  1. Input Stream: If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.

  2. Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.

Header files available in C++ for Input/Output operations are:

What Are C++ Header Files? These are those files that store predefined functions. It contains definitions of functions that you can include or import using a preprocessor directive #include. This preprocessor directive tells the compiler that the header file needs to be processed prior to the compilation.

iostream: iostream stands for standard input-output stream. This header file contains definitions of objects like cin, cout, cerr, etc. The two instances cout in C++ and cin in C++ of iostream class are used very often for printing outputs and taking inputs respectively. These two are the most basic methods of taking input and printing output in C++. To use cin and cout in C++ one must include the header file iostream in the program.

  • Standard output stream (cout): Usually the standard output device is the display screen. It is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

  • standard input stream (cin): Usually the input device in a computer is the keyboard. C++ cin statement is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard.

#include <iostream>
using namespace std;

int main()
{
    int a; //integer data type
    cout<<"Enter a number \n"; //output stream
    cin>>a; //input stream
    cout <<"Value of a is = "<<a;
    return 0;
}

Operators :

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators −

  • Arithmetic Operators: These operators are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction ‘*’ is used for multiplication, etc.

    Addition

    +

    Adds two operands

    Subtraction

    Subtracts the second operand from the first

    Multiplication

    *

    Multiplies two operands

    Division

    /

    Divides the first operand by the second operand

    Modulo Operation

    %

    Returns the remainder as an integer division

  • Relational Operators: These operators are used for the comparison of the values of two operands. For example, ‘>’ checks if one operand is greater than the other operand or not, etc. The result returns a Boolean value, i.e., true or false.

    Is Equal To

    ==

    Checks if both operands are equal

    Greater Than

    >

    Checks if the first operand is greater than the second operand

    Greater Than or Equal To

    >=

    Checks if the first operand is greater than or equal to the second operand

    Less Than

    <

    Checks if the first operand is lesser than the second operand

    Less Than or Equal To

    <=

    Checks if the first operand is lesser than or equal to the second operand

    Not Equal To

    !=

    Checks if both operands are not equal

  • Logical Operators: These operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. Eg.

    Logical AND

    &&

    Returns true only if all the operands are true or non-zero

    Logical OR

    ||

    Returns true if either of the operands is true or non-zero

    Logical NOT

    !

    Returns true if the operand is false or zero

  • Bitwise Operators: These operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands

  • Assignment Operators: These operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.

    =

    Assigns the value on the right to the variable on the left

    +=

    First adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left

    -=

    First subtracts the value on the right from the current value of the variable on left and then assign the result to the variable on the left

    *=

    First multiplies the current value of the variable on left by the value on the right and then assign the result to the variable on the left

    /=

    First divides the current value of the variable on left by the value on the right and then assign the result to the variable on the left

Congratulations!

You just learnt about Header files, Basic Input/Output and Operators in C++ !!!

Thank you for joining me in my quest to conquer C++. In the next blog, we’ll be talking more about the different control statements in a C++ program, see you there, till then keep coding and keep learning.

Credit To The Following Resource I Referenced :

  1. w3schools

  2. geeksforgeeks