Parents : Programming

Date and time note was created$= dv.current().file.ctime
Date and time note was modified$= dv.current().file.mtime

Pre-processing C++ Code

  • pre-processing in c++ happens before the source code is compiled
  • the code that pre-processes the C++ code is called C++ pre-processor
  • there are several pre-processing commands and directives is one of them.

Syntax Of C++

Container functions

Operators

Classes , Structures , Unions

Classes

Constructor function
  1. Initialising the constructor using parameterized constructor
class Wall {
  private:
    double length;
    double height;
 
  public:
    // parameterized constructor to initialize variables
    Wall(double len, double hgt): length{len},height{hgt} {
    }
 
    double calculateArea() {
      return length * height;
    }
};
// the same behaviour is shown for the classes and union 
#include <bits/stdc++.h>
using namespace std;
struct Employee
{
    // char first_name[16];
    string second_name;
    int age;
} emp;
int main()
{
    // when using plain automated/static initialisation 
    // cin >> emp.second_name;
    // when using dynamic initialiation
    Employee *e = new Employee;// new keyword works in the same way as the malloc and calloc does in c (returns pointer to new memory)
    cin >> (*e).second_name;// imagine as the . is converted to line and star is converted to arrow -* ~= ->
    cout << (*e).second_name;
    // or
    cin >> e->second_name;
    cout << e->second_name;
 
    return 0;
}

Common syntax

Strings in C/C++

#include <bits/stdc++.h>
using namespace std;
struct Employee
{
    char first_name[16];
    string second_name;
    int age;
} emp;
int main()
{
    // how to initialise char array string in c++
    // First method
    cin >> emp.first_name;
    // Second method
    // for (int i = 0; i < ;i++){
    //     cin >> emp.first_name[i];
    // }
    // It can be initialised as follows (unsafe tho if you have immutable class use it)
    // strcpy(emp.first_name,"neha");
    cout << emp.first_name;
    // C++ strings (recommended)
    emp.second_name = "meru";// can be initialised directly
    // can be taken input as follows too 
    cin>>emp.second_name
    cout<<emp.second_name;
    
    return 0;
}

Loops in c++

Range based for loops

Pointers in C++

Pointers in C++

Pointers are variables that store the addresses of other variables.

 // To store the variables's address in a pointer 
 // data_type *pointer_name = variable_whose_address_needs_to_be_stored
 #include<bits/stdc++.h>
 using namespace std;
 int main(){
	 int x=10; 
	 int *ptr=&x;// "*" here is used to indicate the variable is pointer and "&" is used to take out the address of the variable (don't confuse it with references in c++ mentioned below)
	 // to dereference or get the value of the pointer use the following syntax
	 int value=*ptr;
	 cout<<value;
 }
 

References in C++

References in C++ pointers and references is that pointers point to an actual location in the memory while the references point to the same location of the variable they are bound to.

References are just another name alias given to some variable. The main difference between

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x = 10;
    int &r = x;// creates an alias for the x variable
    // cout << r; //should output value of x
    int *p1 = &x; // & in case of pointers scrapes the memory address of the variable
    // cout << p1;// address of x
    int *p2 = &r; // should store the same address as x ;
    // cout << p2;
    // assert(p1 != p2);// if the pointers contain equal address this will fail 
    // to get value from the pointer we can use "*"
    cout << *p1;
    return 0;
}
  

What are the differences between a pointers and a references | what are references in c++ ?

Arrays

  • To define the array with all zeroes method
    • int arr[size] = {0};

Resources to study

What’s the best resource to start learning C++? : r/Cplusplus

  • www.learncpp.com is the best free tutorial out there. (reason) It covers everything from the absolute basics to advanced topics. It follows modern and best practice guidelines.
  • www.studyplan.dev/cpp is a (very) close second, even surpassing learncpp in the breath of topics covered. It covers quite a few things that learncpp does not, but does not have just as much detail/in depth explanations on the shared parts. Don’t be fooled by the somewhat strange AI generated images. The author just had a little fun. Just ignore them.
  • www.hackingcpp.com has good, quick overviews/cheat sheets. Especially the quick info-graphics can be really helpful. TBF, cppreference could use those. But its coverage is not complete or in depth enough to be used as a good tutorial - which its not really meant to be either. The last update apparently was in 2023.
  • www.cppreference.com is the best language reference out there.

Stay away from these resources

Again. The above are bad tutorials that you should NOT use.

Sites that used to be on this list, but no longer are:

  • Programiz has significantly improved. Its not perfect yet, but definitely not to be avoided any longer.(reason)

YouTube Tutorials to watch in free time

Most YouTube tutorials are of low quality, I would recommend to stay away from them as well. A notable exception are the CppCon Back to Basics videos. They are good, topic oriented and in depth explanations. However, they assume that you have some knowledge of the language’s basic features and syntax and as such aren’t a good entry point into the language.

If you really insist on videos, then take a look at this list.

This resource list is a macro developed by a user /u/IyeOnline

This may get updates over time if something changes or I write more scathing reviews of other tutorials :)