Lesson 3.1

Cards (16)

  • Header files in C++
    • iostream: Contains definitions of objects like cin, cout, cerr, etc.
    • iomanip: Contains methods for manipulating streams like setw, setprecision
    • fstream: Describes file stream handling
    • bits/stdc++: Includes every standard library, used for time-sensitive tasks
  • Pointers
    • int *ptr;
    • ptr = arr;
  • Array Index
    Each element in an array is associated with a number
  • C++ Array Initialization
    1. int x[6] = {19, 10, 8, 17, 9, 15};
    2. int x[] = {19, 10, 8, 17, 9, 15};
    3. int x[6] = {19, 10, 8};
  • References in C++
    • References serve as aliases for existing variables
    • A reference acts as a nickname for another variable, creating a direct binding to its memory location
    • Changes made to a reference directly affect the original variable they refer to
    • References cannot be reassigned to point to different variables after
  • C++ Array Declaration
    dataType arrayName[arraySize];
  • C++ Array Declaration
    • int x[6];
  • Arrays in C++

    • Arrays are a fundamental data structure in C++ used to store collections of elements of the same data type
    • Arrays store multiple values of the same data type in contiguous memory locations
    • Individual elements are accessed using an index, starting from 0
    • The size of an array is determined at declaration and cannot be changed later
  • Pointers in C++
    • Pointers are variables that hold addresses of other variables
    • Pointers can store the address of cells of an array
  • Declaring a Reference
    • data_type: The data type of the existing variable
    • reference_name: The chosen name for your reference
    • existing_variable_name: The variable you want to create a reference to
  • PROGRAMMING SAMPLE 2 (references)

    #include <iostream>
    using namespace std;
    int main () {
    int i;
    double d;
    int& r = i;
    double& s = d;
    i = 5;
    cout << "Value of i : " << i << endl;
    cout << "Value of i reference : " << r << endl;
    d = 11.7;
    cout << "Value of d : " << d << endl;
    cout << "Value of d reference : " << s << endl;
    return 0;
    }
  • Reference
    • A reference acts as a nickname for another variable, creating a direct binding to its memory location
    • Changes made to a reference directly affect the original variable they refer to
    • References cannot be reassigned to point to different variables after initialization
  • PROGRAMMING SAMPLE 2 (references) Output
    • Value of i : 5, Value of i reference : 5, Value of d : 11.7, Value of d reference : 11.7
  • PROGRAMMING SAMPLE 1 (array and pointer)
    // C++ Program to display address of each element of an array
    #include <iostream>
    using namespace std;
    int main()
    {
    float arr[3];
    float *ptr;
    cout << "Displaying address using arrays: " << endl;
    for (int i = 0; i < 3; ++i)
    {
    cout << "&arr[" << i << "] = " << &arr[i] << endl;
    }
    ptr = arr;
    cout<<"\nDisplaying address using pointers: "<< endl;
    for (int i = 0; i < 3; ++i)
    {
    cout << "ptr + " << i << " = "<< ptr + i << endl;
    }
    return 0;
    }
  • PROGRAMMING SAMPLE 1 (array and pointer) Output
    • Displaying address using arrays: &arr[0] = 0x61fef0, &arr[1] = 0x61fef4, &arr[2] = 0x61fef8
    Displaying address using pointers: ptr + 0 = 0x61fef0, ptr + 1 = 0x61fef4, ptr + 2 = 0x61fef8
  • Declaring a Reference
    data_type &reference_name = existing_variable_name