Skip to content

This Repository is made to track all my progress of CPP Language

Notifications You must be signed in to change notification settings

43H1-BOI/CPP-Language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Some Important Notes

Introduction

C++ -->

  1. Prev. C with Classes
  2. Mother of All Language ( C/C++ )
  3. Fast
  4. Middle Level Language
  5. Embed System Language

Used in -->

  1. Advanced Graphics
  2. Embedded Systems
  3. Video Games




Basics

Input and Output :- Let's see a program first then we'll understand each line of code in detail.

    1 #include<iostream>
    2 using namespace std;
    3
    4 int main(){
    5   int age ;
    6
    7   cout << "Enter Your Age : ";
    8   cin >> age ;
    9
    10  cout << "WOW! You are " << age << " Years Old." << endl;
    11
    12  return 0;
    13 }

"Don't Worry if you won't understand any of these things , We'll talk about them in depth later on this Repo"

In 1st line , we have included a header file for functionalities to work properly . If we don't add header file in C/C++ , the whole code meant nothing .

In 2nd line , we have added a standard namespace which consist of all important and really usefull funtionalities.

In 4th line , we have created a main function having int as its's return-type .

In 5th line , there is a variable declared of int data type.

In 7th line , we used the standard output of C++ . The Text Written in between the Quotation marks " " will be displayed upon the screen.

In 8th line , we used standard input of C++ . We can store data provided by user in Variables using this .

In 10th line , we have binded up everything and printed them.

In 12th line , we have return-type for the main funtion and the parenthesis for ending a function.


Variable and Data Types

Variables are containers for storing data values. Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program.

The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. In C++, all the variables must be declared before use.

syntax :-

    // Declaring a single variable
    type variable_name;

    // Declaring a variable with value
    type var_name = value;

    // Declaring multiple variables:
    type var1_name, var2_name, var3_name;

    // Declaring multiple variable with value
    type var1 = value1,var2 = value2;

Data Types

  1. int --> Stores integers -- 2 or 4 bytes
  2. char --> Stores single characters -- 1 byte
  3. bool --> Stores True or False / 1 or 0 -- 1 byte
  4. float --> Floating Point Number , 6 or 7 Decimal Places -- 4 bytes
  5. double --> Stores floating point numbers , 15 Decimal Places -- 8 bytes
  6. string --> Sentence or couple of words , stores text -- Changes Accordingly

Constants

const keyword - Used to Convert Value of any Variable Read Only , It can't be modified once assigned. If using const keyword , use all capitals for Naming of Constant Variable (It Increases in readability of Code)


Namespace

Provides a Solution for Preventing name conflicts in large projects. Each Entity needs a unique name . A namespace allows for identically named entities as long as the namespaes are different.

We use "using namespace std;" to get rid of writing "std::" before each statement like "std::cout" , "std::string"

We can also create Custom namespace to use them in our program

syntax :-

namespace name_of_namespace {
    // value of Variables
    // EG :
    int x = 5;
    std::string Jai = "Jai Mata Di";
}

typedef is used to define your own identifiers

that can be used in place of type specifiers such as int , float , and double , etc.

syntax :-

typedef <current_name> <new_name>;

We must use "_t" or "_type" as suffix for the typedef variables.

Example :-

 typedef std::string str_t;

using keyword is also used for aliasing of DataTypes.

syntax :-

using <new_name> = <current_name>;

Example :-

 using str = std::string;

Operators :- Operators are used to perform operations on variables and values.

Operators in C++ are divided into following groups :

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Bitwise operators

Arithmetic operators :- Arithmetic operators are used to perform common mathematical operations.

  1. " + " Addition --> Adds together two values --> x + y
  2. " - " Subtraction --> Subtracts one value from another --> x - y
  3. " * " Multiplication --> Multiplies two values --> x * y
  4. " / " Division --> Divides one value by another --> x / y
  5. " % " Modulus --> Returns the division remainder --> x % y
  6. " ++ " Increment --> Increases the value of a variable by 1 --> ++x
  7. " -- " Decrement --> Decreases the value of a variable by 1 --> --x

Assignment operators :- Assignment operators are used to assign values to variables.

Assignment operators


Comparison operators :- Comparison operators are used to compare two values (or variables).

This is important in programming, because it helps us to find answers and make decisions. The return value of a comparison is either 1 or 0, which means true (1) or false (0).

  1. " == " -- Equal to --> x == y
  2. " != " -- Not equal --> x != y
  3. " > " -- Greater than --> x > y
  4. " < " -- Less than --> x < y
  5. " >= " -- Greater than or equal to --> x >= y
  6. " <= " -- Less than or equal to --> x <= y

Logical operators :- Logical operators are used to determine the logic between variables or values.

As with comparison operators, you can also test for true (1) or false (0) values with logical operators.

  1. " && " Logical AND --> Returns true if both statements are true --> x < 5 && x < 10
  2. " || " Logical OR --> Returns true if one of the statements is true --> x < 5 || x < 4
  3. " ! " Logical NOT --> Reverse the result, returns false if the result is true --> !(x < 5 && x < 10)

Bitwise operators :- Bitwise operators perform operations on integer data at the individual bit-level.

These operations include testing, setting, or shifting the actual bits.

    &	Bitwise AND Operator
    |	Bitwise OR Operator
    ^	Bitwise XOR Operator
    ~	Bitwise Complement Operator
    <<	Bitwise Shift Left Operator
    >>	Bitwise Shift Right Operator




Type Conversion :- Conversion of a value from one type to another .

  • Implicit --> Automatic by Compiler
  • Explicit --> Precode value with new data type (Done by Programmer)



















Conditional Statements

There are three types of conditional statements exists in CPP Language.

  1. If-Else statement
  2. Switch statement

1. If-Else Statement :- If - Else Statement is divided mainly into 4 parts.

    if(condition){
    // Statements to execute if
    // condition is true
    }

Loops

Functions

Functions are block of code which can be reused many times.

Declaration :-

<return_type> <Function_name>(parameters);

Definition :-

<return-type> <Function-name>(parameters if any){
    //block of code
}

Function must be declared before main and can be defined after main or before main() fun , we can both declare and define function at the same time.

Example :-

/* Function Example 1 :
Print Namaste for India and Bonjour for French*/
#include<iostream>
using namespace std;

void Greet(int n); // Function Declaration before main()

// Function Decration and Definition at same time
void GoodBye( ){
    cout << "Good Bye ! " << endl>>;
}

int main( ){ // Calling Main Function
    cout << "Enter 1 for Indian " << endl;
    cout << "Enter 2 for French " << endl;

    printf("Enter Your Choice : ");
    cin >> n;

    Greet(n); // Calling Greet() Function

    GoodBye(); // Calling GoodBye() Function
    return 0;
}

// this function has a parameter of int data-type
void Greet(int n){ // Function Definition
    if (n == 1){
        printf("Namaste India");
    } else if (n == 2) {
        printf("Bonjour");
    } else{
        printf("Invalid Input");
    }
}



Arrays

String

OOPs

Extras :

Types of Language of Computer :-

  1. Higher Level language - Easy to write and Understand as a normal human and Slower . Python , JS , Java , C# , Ruby , etc.

  2. Middle Level Language - Comparatively not easy to write and understand but Faster in Speed . C , C++ , Oberon , Pascal , etc.

  3. Low Level Language - Not Possible to write and understand cause this was written in Binary Form . Assembly Language or Machine Language




Aliasing is a process of providing other Name to a Object.

Dangling Pointer : A pointer pointing out to a location where nothing is present .

About

This Repository is made to track all my progress of CPP Language

Resources

Stars

Watchers

Forks

Languages