Skip to content

uv-goswami/Cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 

Repository files navigation

C ++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language.

Why Learn C++?

  • C++ is one of the most used and popular programming languages.
  • C++ is used in making operating systems, embedded systems, and Graphical User Interfaces.
  • It is an object-oriented programming language that implements all the OOPs concepts such as Abstraction, Encapsulation, and Inheritance, which gives a clear structure to programs and allows code to be reused, lowering development costs and providing security.
  • It is portable and can be used to create applications that can be adapted to multiple platforms.
  • C++ is easy to learn so that you can choose it as your first programming language.
  • It makes programming easy for programmers to switch to C++ because its syntax is similar to C, Java, and C#.

Features

  • C++ is a high-level, general-purpose programming language designed for system and application programming. It was developed by Bjarne Stroustrup at Bell Labs in 1983 as an extension of the C programming language.
  • One of the key features of C++ is its ability to support low-level, system-level programming, making it suitable for developing operating systems, device drivers, and other system software.
  • C++ has a large, active community of developers and users, and a wealth of resources and tools available for learning and using the language. Some of the key features of C++ include:
  • Object-Oriented Programming: C++ supports object-oriented programming, allowing developers to create classes and objects and to define methods and properties for these objects.
  • Templates: C++ templates allow developers to write generic code that can work with any data type, making it easier to write reusable and flexible code.
  • Standard Template Library (STL): The STL provides a wide range of containers and algorithms for working with data, making it easier to write efficient and effective code.
  • Exception Handling: C++ provides robust exception handling capabilities, making it easier to write code that can handle errors and unexpected situations.

image

Advantages of C++ :

  • Simple
  • Machine Independent (Platform dependant)
  • Mid-level language
  • Rich library support
  • Speed of execution
  • Case-sensitive
  • Dynamic Memory Allocation
  • Memory Management
  • Multi-threading
  • Pointer and direct memory-access
  • Object oriented
  • Compiled Language

Disadvantages of C++ :

  • Steep Learning Curve
  • Verbose Syntax
  • Error-Prone

image

INSTALLATION

image

First-Code in C++

// C++ Program to display "Hello, World!"
#include <iostream>
using namespace std;

int main()
{
   cout << "Hello, World!";

   return 0;
}

Output

image

  • // C++ program to display “Hello World” : This line is a comment line.
  • #include : This is a preprocessor directive. #include tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions
  • using namespace std : This is used to import the entity of the std namespace into the current namespace of the program. The statement using namespace std is generally considered a bad practice. When we import a namespace we are essentially pulling all type definitions into the current scope.The std namespace is huge. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type. For example, std::cout.
  • int main() { } : A function is a group of statements that are designed to perform a specific task. The main() function is the entry point of every C++ program, no matter where the function is located in the program.
  • cout<<“Hello World”; : std::cout is an instance of the std::ostream class, that is used to display output on the screen. Everything followed by the character << in double quotes ” ” is displayed on the output device. The semi-colon character at the end of the statement is used to indicate that the statement is ending there.
  • return 0 : This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.
  • Indentation : We must always use indentations and comments to make the code more readable.

Basic of Syntax

image

  • Header File : The header files contain the definition of the functions and macros we are using in our program. They are defined on the top of the C++ program.

    Syntax :

    #include <library_name>
  • Namespace : A namespace in C++ is used to provide a scope or a region where we define identifiers. It is used to avoid name conflicts between two identifiers as only unique names can be used as identifiers.

    Syntax :

    using namespace std;
  • Main Function : The main function is the most important part of any C++ program. The program execution always starts from the main function. All the other functions are called from the main function. In C++, the main function is required to return some value indicating the execution status.

    Syntax :

    int main() {
    
      ... code ....
      return 0;
    }
  • Blocks : Blocks are the group of statements that are enclosed within { } braces. They define the scope of the identifiers and are generally used to enclose the body of functions and control statements.

    Syntax :

    {
        
    // Body of the Function
    
      return 0;
    }
  • Semicolons : It is used to terminate each line of the statement of the program. When the compiler sees this semicolon, it terminates the operation of that line and moves to the next line.

Syntax :

any_statement ;

DATATYPES :

  • PRIMITIVE
    • Integer

      • Size - 4 bytes

      • Eg : 1, 4, 100

      • Range [unsigned] (0 to 232 - 1)

      • Range [signed] (- 231 to 231 - 1) (using MSB(mosy significant bit) as sign)

        image image

    • Float Eg : 3.14, 6.5, 1.00

      • Size - 4 byte
      • DOUBLE - Size 8 byte image
    • Character

      • Size - 1 byte

      • Eg : c, f, @, %

        image

    • Booleam

      • Eg : 0, 1

      • Size 1 byte

        image

LETS TRY IT OUT : image OUTPUT : image

Type Modifiers :

image

  • DERIVED
    • Function
    • Array
    • Pointer
    • Reference
  • USER-DEFINED
    • Class
    • Structure
    • Union
    • Enum

IF-ELSE Statement :

image

LOOPS IN C++ :

For Loop :

image

While Loop :

image

Do While Loop :

image

Do While Loop V/s While Loop:

image

Do while loop don't apply condition on the first count while, while loop does so

Break and Continue Statements in loops :

// To understancd the concept

#include <iostream>
using namespace std;

int main(){
    
   int pocketMoney = 3000; 
   for(int date=1;date<=30;date++){
    
    if(date%2==0){
        continue;
    }
    if (pocketMoney==0){
        break;
    }
    cout<<"Go out Today"<<endl;
    pocketMoney-=300;

   }
    return 0;
}
// To print prime numbers between two given numbers

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;

    for (int num = a; num <= b; num++) {
        int i;
        for (i = 2; i < num; i++) {
            if (num % i == 0) {
                break;
            }
        }
        if (i == num) {
            cout << num << endl;
        }
    }
    return 0;
}

Switch-Case Statement

// multiple language robot
#include <iostream>
using namespace std;

int main() {

    char button;
    cout<<"Input a character";
    cin>>button;

    // if (button=='a'){
    //     cout<<"hello"<<endl;
    // }

    // else if (button=='b'){
    //     cout<<"namaste"<<endl;
    // }
    
    // else if (button=='c'){
    //     cout<<"hola amigo"<<endl;
    // }
    
    // else if (button=='d'){
    //     cout<<"salut"<<endl;
    // }
    
    // else if (button=='e'){
    //     cout<<"Ciao"<<endl;
    // }
    
    // else{
    //     cout<<"I am still learning more..."<<endl;
    // }

    switch(button){
        
        case 'a':
           cout<<"Hello"<<endl;
           break;

        case 'b':
           cout<<"Namaste"<<endl;
           break;
        
        case 'c':
           cout<<"Hola amigo"<<endl;
           break;
        
        case 'd':
           cout<<"Ciao"<<endl;
           break;
        
        case 'e':
           cout<<"Salut"<<endl;
           break;
        
        default:
        cout<<"I am still learning more!"<<endl;
           break;
    }

    return 0;
}

Operators in C++

Operators are symbols that tell the computer to perform some specific operations.

  • Arithematic Operators :

    • Binary Operators : (Done on two operands) +, -, *, /, %
    • Unary operator : (Done on only one operands) ++, --
      • Pre Incrementer (++a)
      • Post Incrementer (a++) image image
  • Relational Operators :

    Defines a relation between 2 operands

    Returns a boolean Value (== , != , > , < , >= , <=)

  • Logical Operators :

    && AND (works similar like gate operations)

    || OR (All values are true except 0)

    ! NOT

  • Bitwise Operators : image

  • Assignment Operators :

    • (= , += , -= , *= , /=)
  • Miscellaneous Operators :

    • Sizeof() : returns size of variable
    • Condition?X:Y : also called (Ternary Condtional Operator)returns value of X if condition is true or else value of Y
    • cast : convert one datatype to another
    • Comma(,) : Causes a sequence of operations to be performed
    • & : (Reference Operator) Returns address of of a variable
    • * : pointer to a variable

Operators Precedence

image

Patterns In C++

//Reactangle pattern
#include <iostream>
using namespace std;

int main(){
    int rows,cols;
    cout<<"Enter the rows and columns"<<endl;
    cin>>rows>>cols;

    for(int i=1;i<=rows;i++){
        cout<<endl;
        for(int j=1;j<=cols;j++){
            cout<<"*";
        }
    }

    return 0;
}
//Hollow Reactangle pattern
#include <iostream>
using namespace std;

int main(){
    int rows,cols;
    cout<<"Enter the rows and columns"<<endl;
    cin>>rows>>cols;

    for(int i=1;i<=rows;i++){
        cout<<endl;
        for(int j=1;j<=cols;j++){
            if(i==1 || i==rows){
                cout<<"*";
            }
            else if(j==1 || j==cols){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
        }
    } 

    return 0;
}

Functions in C++:

Syntax

returnType functionName(parameter1,parameter2,....){
   //function body
}

image

Array

Array is a list of similar datatypes.

Syntax :

#include <iostream>
using namespace std;

int main(){

    int n;
    cin>>n;

    int array[n];
    for(int i=0;i<n;i++){
        cin>>array[i];
    }

    for(int i=0;i<n;i++){
        cout<<array[i]<<" ";
    }

    return 0;
}

Pointers

Pointers are variables that store the address of other variables.

Pointers and Arrays

#include <iostream>
using namespace std;

int main(){

    int arr[]={10,30,50};
    cout<<*arr<<endl;

    int* ptr = arr;
    for(int i=0;i<3;i++){
        cout<<*(arr+i)<<endl;
    }

}

Pointers to Pointer

#include <iostream>
using namespace std;

int main(){

    int a=10;
    int* p;
    p = &a;
    cout << *p << endl;
    int**q = &p;
    cout << *q << endl;
    cout << **q << endl;

    return 0;

}

Pointers Uses in Function

#include <iostream>
using namespace std;

void swap(int *a,int *b){
    int temp=*a;
    *a = *b;
    *b = temp;
}


int main(){

    int a=10;
    int b=5;

    swap(&a,&b);
    cout<<a<<" "<<b<<endl;

    return 0;

}

Strings

  • String Declaration
#include <iostream>
#include <string>
using namespace std;

int main(){

    string str;
    getline(cin,str);
    cout<<str;
    string str1(5,'n');
    string str2 = "IamGay";

    cout<<str2;
    return 0;

}
  • Concatenation of Strings
  #include <iostream>
#include <string>
using namespace std;

int main(){

    string str1 = "fam";
    string str2 = "ily";

    //str1.append(str2);
    cout<<str1 + str2<<endl;

    return 0;

}
  • Indexing of Strings
#include <iostream>
#include <string>
using namespace std;

int main(){

    string str1 = "fam";
    string str2 = "ily";

    cout<<str1[2] + str2[0]<<endl;

    return 0;

}
  • Functions of Strings
    • .clear( ) - clears the string
    • .compare( ) - show the difference between string
    • .empty( ) - check whether string is empty or not
    • .erase(start_index,characters to erase) - erase a specific part of string
    • .find( ) - finds a substring in a string
    • .insert(index, substring) - insert a substring in a string
    • .size( ) - finds size of string
    • .substr(startindex,no. of characters) - get a substring from a string
    • stoi( ) - changes string to integer
    • to_string( ) - changes integer to string
  • Sorting in String
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(){

    string s1 = "qwertyuioasdfghjklzxcvbnm";
    
    sort(s1.begin(), s1.end());

    cout<<s1<<endl;

    return 0;

}

Recursion

It is when a fuction call itself to make the problem smaller.

#include <iostream>
using namespace std;

int factorial(int n){
    if(n==0){
        return 1;
    }

    int prevfact = factorial(n-1);
    return n*prevfact;

}

int main(){

    int n;
    cin>>n;

    cout<<factorial(n)<<endl;

    return 0;

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages