Kickstart C++: A Fun Guide at OS Cafe ☕.

Kickstart C++: A Fun Guide at OS Cafe ☕.

Description:

Ready to dive into the world of Data Structures and Algorithms? Start your journey with this fun and beginner-friendly C++ guide! Learn the essentials, grasp the basics of coding logic, and gear up to solve complex problems—all while having a blast. Perfect for anyone looking to ace coding interviews or sharpen their programming skills. 🚀


Basic Programming Fundamentals Of Any Language (esp. C++) :→

User Input / Output
Data Types
If Else statements
Switch Statement
What are arrays, strings?y
For loops
While loops
Functions (Pass by Reference and Value)

0>Installation of CPP :→

step-0:→ Install “VS code” from offical website.

step-1:→ set up “Mingw-w64” in local machine.

step-2:→ click to set-up CPP in your device

step-3:→ copying and pasting this on cmd you will get the version of CPP.

g++ --version

if you get the output as :→

if you get this that means setup is done.


1>User Input / Output :→

for output/input in CPP :→ here we can see the output that we will give and can be seen of the screen.

#include<bits/stdc++.h>
using namespace std;
int main(){
  int x;
  int y;
  cin >> x >> y;
  cout << " x is :-> "<<x<<" and y is :-> "<<y<<"\n";
  cout << "welcome to the OS cafe";
}

let’s see what’s the output ?

and now we will deep_dive into it.

let destructure it :→

1> #include<bits/stdc++.h> :→ it’s a header file of CPP. That include all the libaries for the every function we need to use in CPP.

2> using namespace std :→ every time we have to use “ std:: “ , again and to replace it we gonna use “using namespace std”.

#include<bits/stdc++.h>
using namespace std;
int main(){
   cout << "welcome to the os cafe";
   cout << "hello world!!";
}

now see the difference (without using namespace std ):→

#include<bits/stdc++.h>
int main(){
  std:: cout << "welcome to the os cafe";
  std ::cout << "hello world!!";
}

now we see some more thinks to better coding CPP :→

1> Install CPP extension.(in VS code)

2> Install Code Runner.(in VS code) [ why?? => we will discuss it later]


here we will see how CPP works:→

1> first we will pictorial representation to under it better.


enough theory now let’s let’s go to next parts we will move on and cover the rest of the topics that’s:→


2> Data Types:→

there are many type of data type of data type in CPP. they are :→


here is all about data type in CPP:→

Data-type :->

a> numbers :[whole num ,Decimal] :→

1>whole number :

i> short :→ used for small number.

ii> int :→ used for big number.

iii> long long(/long) :→ used for bigger/biggest numbers.


2>decimal number :→

i> float :→ (data-type of decimal)

ii> double :→ (data-type for bigger decimal)


b> words/string :[char,string,getline] :→

i> char : single alphabet [char]

ii> string: more characters(words) [string]

iii> getline(cin,str) :→[used for the full line]


c> boolean [true/false] :→

i> bool


Let’s code to understand Data-Types in CPP :→

size of operator is used to know about the size of the “Data-Type”.

that’s why we don’t have to memorize the bytes (size of the data-type).

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int x;
  string y;
  char z;
  char man = 'a';
  cin >> x >> y >> z; 
  cout << "x:  " << x << "  y:" << y << "  z:" << z<<"\n";
  cout<<"size of char is :"<<sizeof(man)<<" byte";
  return 0;
}

Here we don’t discuss about “GETLINE“ function. Now we will discuss about “Getline” function.

#include <bits/stdc++.h>
using namespace std;

int main()
{
  string ayush;
  getline(cin,ayush);

  cout << "the line is:  " << ayush;
  return 0;
}

here is the output :→


let’s end data type here now we gonna move on to “IF-ELSE” statement :→

#include <bits/stdc++.h>
using namespace std;

int main()
{
  /*
        if age is 18 you can give vote.
        if not you can't do it .

   */
  int age;
  cin >> age;

  if (age>20)
  {
    cout << "you can give vote... ";
  }
  else if (age<18)
  {
    cout << "you are 18 but can't give vote!!! ";
  }
  else
    {
      cout << "we don't need any code!!! ";
    }

    return 0;
}

3>switch case in CPP :→

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int day;
  cout << "which day you want to see ?  ";
  cin >> day;

  switch (day)
  {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
  default:
    cout << "Invalid day";
  }

  return 0;
}

this in enough theory for switch-case statement now we can we move from this as of now!


4>loops:→

now let’s cover loop’s section in details, like there is 2 section in the loops:→

a>while loop :→

use:→when we know that the initial condition then we should use the while loop.

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int a = 1;
  int n;
  int sum = 0;
  cout << "the sum we want to find :->  ";
  cin >> n;
  while (a <= n)
  {
    cout << a << " ";
    sum = sum + a;
    a++;
  }

  cout << "\n";
  cout << "sum is :->" << sum;

  return 0;
}

2>for loop :→

use:→ when we know “ how many time the code will run “ then we have to use “for loop”.

#include <bits/stdc++.h>
using namespace std;

int main()
{
  /*
       While in CPP

  */
 int n;
 int sum = 0;
 cout << "how much count you want to do ?";
 cin >> n;
 for (int  i = 1; i <= n; i++)
 {
   cout << i<<" ";
   sum = sum + i;
 }

 cout <<"\n"<<"the sum is :->" << sum;

 return 0;
}

5> What are arrays, strings ?

#include<bits/stdc++.h>
//1d array
using namespace std;
int main()
{
  int arr[3];
  cin >> arr[1] >> arr[2] >> arr[3];
  cout << arr[1];
}

here is the code for “2d array“ for CPP :→

#include <bits/stdc++.h> 
using namespace std;
int main()
{      
      int arr[2][3];
      arr[1][3] = 18;   
      cout << arr[1][3]; 
}

here is the code part to understand the “string“ part in CPP:→

#include <bits/stdc++.h>
using namespace std;

int main()
{
  string ayush;
  cout << "the sentence you want to choose :-> ";
  getline(cin, ayush);

  cout << "Size of the string: " << ayush.length() << endl;
  cout << "index of  "<<ayush[4];
  return 0;
}

and here we complete the basic of string.


array and string have “0” based indexing that means it’s start from

(0 to n-1) and n is the size of the array.

here is the pictorial re-presentation for understanding “1-d and 2d array” :→


6>Function:→

second last topic for “CPP” that’s Function.

there are 4 type of function in CPP.

a> void :→ void means that function will not return anything

b> return :→ return means that will return (anydatatype_thing)

c> parameter :→ function(parameter)=> we will pass it in the function.

d> non-parameter :→function()=> it's has nothing that we will pass in the function.

now let’s code little bit.

let’s start the basic type of functions in “CPP“ , that’s :→

a>void function :→

#include<bits/stdc++.h>
using namespace std;

void myName(string name)
{
  cout << "you name is:-> "<<name;
}
int main(){
  string name;
  cout << "what is your name? ";
  cin >> name;
  myName(name);
}

let’s move on to the next type of function that’s:→

2>return function :→ it must return data.(with specific data_type).

#include<bits/stdc++.h>
using namespace std;

int sum(int num1,int num2){
  int sum1 = num1 + num2;
  return sum1;
}
int main(){
  int n1;
  int n2;
  cout << "1st number is ";
  cin >> n1 ;
  cout << "2nd number is ";
  cin >> n2;

  int ans = sum(n1, n2);
  cout << "sum is : ";
  cout << ans;
}

and if in this the function we don’t pass anything that’s non-parameter function.

and if we do that’s parameter function.

so, let’s end the function section here and let’s move on to “pass by value” and “pass by reference”.


7>pass by value and pass by reference :→

a>pass by value :→

generally all data type in CPP are passed by value. That means in a function if we changed the number by any operation or something ,it doesn’t change the original value. actually it passes the copy of variable and in that we actually change.

here is the pictorial representation to understand the “pass by value” :→


now let’s understand it with code :→

#include<bits/stdc++.h>
using namespace std;

void doThis(int num){

  num += 5;
  cout << "the changed number is :-> " << num << "\n";
}

 int main()
{
  int num;
  cout << "choose you number :-> ";
  cin >> num;
  doThis(num);
  cout <<"number inside main fxn :-> "<< num;
}

see the output for better understanding :→


b>pass by reference :→(&)

here we write it as :→ (&name)

what it does?

here it give the original address of the and change the datatype… unlike pass by value.


let’s understand the with what is pass by reference do.


let’s understand it with code.

#include<bits/stdc++.h>
using namespace std;

void doThis(int &num){
  num += 5;
  cout << "the changed number is :-> " << num << "\n";
}
int main()
{
  int num;
  cout << "choose you number :-> ";
  cin >> num;
  doThis(num);
  cout <<"number inside main fxn(after pass by ref) :-> "<< num;
}

here is the output:→


Conclusion

C++ is a foundational language that combines power, efficiency, and versatility, making it a favorite among developers for a wide range of applications. By mastering the basics covered in this guide—such as input/output, data types, control flow, functions, and an introduction to OOP—you’ve taken the first step toward unlocking its full potential.

As you progress, delve deeper into advanced concepts like memory management, templates, and the Standard Template Library (STL) to build efficient and scalable applications. Whether you aim to excel in competitive programming, system development, or software engineering, C++ provides the tools and flexibility to turn your ideas into reality. Keep coding, experimenting, and exploring—your journey has just begun!


here the journey of CPP ends. Let’s move on to “Time complexity and Space complexity“. Let’s meet you at the top. signing off

-by The_OS_Coder.(I’m Batman 🦇)