This C++ Cheatsheet is For You!
  1. C++
  2. Basic syntax and functions from the C++ programming language.

  3. Boilerplate
  4. #include <iostream>
    using namespace std;
    int main() {
    cout << " Meezan ";
    return 0;
    }
    
  5. cout <<< /li>

    It prints output on the screen

    cout << "This is C++ Programming";
    
  6. cin >>
  7. It takes input from the user

    cin >> variable_name
    
  8. Data types
  9. The data type is the type of data

  10. Character type
  11. Typically a single octet(one byte). It is an integer type

    char variable_name;
    
  12. Integer type
  13. The most natural size of integer for the machine

    int variable_name;
    
  14. Float type,Double type,Void type,Boolean type
  15. A single-precision floating-point value

    A double-precision floating-point value

    Represents the absence of the type

    float variable_name;
    double variable_name;
    void
    bool
    
  16. Comments
  17. A comment is a code that is not executed by the compiler, and the programmer uses it to keep track of the code.

    // It's a single line comment
     /* It's a 
    multi-line
    comment
    */ 
    
  18. Strings
  19. It is a collection of characters surrounded by double quotes

  20. Declaring String
  21. // Include the string library
    #include <string>
    // String variable
    string variable1 = "Hello World";
    
  22. append function
  23. It is used to concatenate two strings

    string firstName = "Haris";
    string lastName = "malik";
    string fullName = firstName.append(lastName);
    cout << fullName;
    
  24. length function
  25. It returns the length of the string

    #include<iostream>
    using namespace std;
    int main() {
    cout << "Welcome To Alfurqaan";
    return 0;
    }
    
  26. Accessing and changing string characters
  27. string variable1 = "Hello World";
    variable1[1] = 'i';
    cout << variable1;
    
  28. Maths
  29. C++ provides some built-in math functions that help the programmer to perform mathematical operations efficiently.

  30. max function,min function,sqrt function
  31. It returns the larger value among the two
    It returns the smaller value among the two
    It returns the square root of a supplied number

    cout << max(25, 140);
    cout << min(55, 50);
    //sqrt function
    #include <iostream>
    using namespace std;
    int main() {
    cout << "Welcome To Alfurqaan";
    return 0;
    }
    
  32. Conditional statements if
  33. if (condition) {
    // This block of code will get executed, if the condition is True
    }
    
  34. If-else Statement
  35. if (condition) {
    // If condition is True then this block will get executed
    } else {
    // If condition is False then this block will get executed
    }
    
  36. if else-if Statement
  37. if (condition) {
    // Statements;
    }
    else if (condition){
    // Statements;
    }
    else{
    // Statements
    }
    
  38. Ternary Operator
  39. It is shorthand of an if-else statement.

    variable = (condition) ? expressionTrue : expressionFalse;
    
  40. Switch Case Statement
  41. It allows a variable to be tested for equality against a list of values (cases).

    switch (expression) 
    {
    case constant-expression: 
    statement1;
    statement2;
    break;
    case constant-expression: 
    statement;
    break;
    ...
    default: 
    statement;
    }
    
  42. while Loop
  43. while (/* condition */)
    {
    /* code block to be executed */
    }
    
  44. do-while loop/li>
    do
    {
    /* code */
    } while (/* condition */);
    
  45. for loop
  46. for (int i = 0; i < count; i++)
    {
    /* code */
    }
    
  47. Break Statement,Continue Statement
  48. break keyword inside the loop is used to terminate the loop
    continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop

    break;
    continue;
    
  49. Pointers
  50. Pointer is a variable that holds the memory address of another variable

  51. Pointers Declaration
  52. datatype *var_name; 
    var_name = &variable2;
    
  53. Object-Oriented Programming
  54. It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities.

  55. class
  56. class Class_name {
    public: // Access specifier
    // fields
    // functions
    // blocks
    };
    
  57. object
  58. Class_name ObjectName;
    
  59. Constructors
  60. It is a special method that is called automatically as soon as the object is created.

    class className { // The class
    public: // Access specifier
    className() { // Constructor
    cout << "Haris malik";
    }
    };
    
    int main() {
    className obj_name; 
    return 0;
    }
    
  61. Encapsulation
  62. Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

    #include <iostream> 
    using namespace std; 
    class ExampleEncap{ 
    private: 
    /* Since we have marked these data members private, 
    * any entity outside this class cannot access these 
    * data members directly, they have to use getter and 
    * setter functions. 
    */ 
    int num; 
    char ch; 
    public: 
    /* Getter functions to get the value of data members. 
    * Since these functions are public, they can be accessed 
    * outside the class, thus provide the access to data members 
    * through them 
    */ 
    int getNum() const { 
    return num; 
    } 
    char getCh() const { 
    return ch; 
    } 
    /* Setter functions, they are called for assigning the values 
    * to the private data members. 
    */ 
    void setNum(int num) { 
    this->num = num; 
    } 
    void setCh(char ch) { 
    this->ch = ch; 
    } 
    }; 
    int main(){ 
    ExampleEncap obj; 
    obj.setNum(100); 
    obj.setCh('A'); 
    cout<<obj.getNum()<<endl; 
    cout<<obj.getCh()<<endl; 
    return 0; 
    }
    
  63. Creating and writing to a text file
  64. #include<iostream>
    #include <iostream>
    using namespace std;
    int main() {
    // Create and open a text file
    ofstream MyFile("filename.txt");
    // Write to the file
    MyFile << "File Handling in C++";
    // Close the file
    MyFile.close();
    }
    
  65. in
  66. Opens the file to read(default for ifstream)

    fs.open ("test.txt", std::fstream::in)
    
  67. out
  68. Opens the file to write(default for ofstream)

    fs.open ("test.txt", std::fstream::out)