Introduction To C++

·

7 min read

In this blog we will be learning how programming languages work taking C++ as our example.

Let us consider a file in which a code is written for example

a=10
b=15
c=a+b

the code we write is close to english or we could say it is understandable to humamns but not for computers. we know that the computer only the undrstands the binary language ( whcih consists of bits i.e. 0 and 1)

hence we need a translator that could convert our code to binary languge for the computer to run it and this work is done by thr complier.

So we give the code file to the compiler and it converts it to a Binary output file.
Compiler also find teh mistakes from our code as compile time errors.

But there are some mistakes that our compiler cannot detect.
for example we write a code to multiply two numbers but instead of writing a*b we wrote a+b the compiler will consider this as a valid code and the error will be detected during runtime as a runtime error.

We generally use an IDE to run a code. It compiles and runs the code for us
examples : code blocks, dev c++ etc

For writing a flowchart for a code we start witha start block simillarly our code in C++ alos needs a syaty block

main() {

}

{ } tells us that the code written in the brackets is a part of int main. Whnever we use main, the code written in { } is accsessed.

To print out a statement we use an inbuilt command cout
to use cout in our code we must include the file of the inbulit command we are using otherwise ir shows a runtime error as cout is not defined in the c++ syntax


#include <iostream> 
using namespace std; 
int main() { 
cout << "Hello World"; 
cout << "\n"; 
}

"\n" is a special charecter that is basically the enter button

the ; we use afetr every line in the code is just like a fullstop
; specifies that it is the end of line.

<< pushes our statement into cout

we can also give multiple satements to cout to printout

#include <iostream> 
using namespace std;
int main() { 
cout << "Hello" << "\n"; 
}

we can also use endl ( it does the same work as \n)

#include <iostream> 
using namespace std;
int main() { 
cout << "Hello" << endl; 
}

always include the header files
#include <iostream> using namespace std;
in our code when you use inbluilt satemeents

example code for adding two numbers

 #include <iostream>
using namespace std;
int main() { 
   a = 10;
   b = 15;
   c = a+b;
   cout << c << endl;
}

the above code is wrong!!
and that is beacuse a, b, c are undeclared identifiers
For c++ to assign storage to our identifier we need to declare two things about teh data

  1. The type/kind of data

  2. how big the data is going to be

lets say we want to put number in our storage, we use int a;

int signifies that a is an integer an its size can go upto 4 bytes generally.

so the correct code would be

#include <iostream>
using namespace std;
int main() {
  int a = 10; 
  int b = 15;   
  int c = a + b;
cout << c << endl;
}

other types of charecters in c++
char, float, bool

char d = 'x'; ( size of char is 1 byte)  
float f = 1.32;  
bool t = true;
doubles ( 8 byte)

there is also an inbuilt functionality that tells us the size of our variable that is sizeof

/

cin:

to write a code where the user can decide the input we use cin

/ #include <iostream>
using namespace std;

int main() {
int a, b;

cin >> a;
cin >> b;

int c = a+b;

cout << c << endl;
}

after cout we use << showing we are pushing out the value we want to printout

in case of cin we use >> showing that the user is going to give an input

when you store a number as int a = 259; it gets stored in the 4 byte memory space given to it as its binary form. So then if I want to store char d = 'y' how is that done?
charecters like letters, commas ',' , semicolon ';' etc have been given certain number to them in the ascii table.

how do we repersent floats in binary language, for example we get 100.1101 as the binary value we represent it as 1.xxxx * 2^e , we store the xxxxx part (known as mentissa) and the e part (known as exponenet) seperately. in the 4 byte memory assigned we have certain space to store the mentissa and certain area for the exponenet.

so 100.1101 should we written as 1.001101 * 2^2

/

now we will learn how negetive numbers
these are the steps we follow:

  1. Forget the negetive

  2. Convert number to binary

  3. take 2's complement

  4. store the result of step 3

concept of 2's compliment :
consider the binary number 0101 ,its 1's compliment would be 1010 , now the 2's compliment is the 1's compliment + 1
i.e. 1011

lets take another example given is a binary code 1010 we need to find out the corresponding integer
first we find out the 1's compliment 0101 then add 1 to (+1) it
which gives 0110 which is 6. Hence our answer is -6.

Operators:

Lets write a code that converts farenheit to celcius value

/include <iostream>
using namespace std;

int main() {
int f;
cout << "Enter Fah Value" << endl;
cin >> f;
int c = (5/9)*(f-32);
cout << c << endl;
}

this code will give us the value 0
but why so thats cause 5 and 9 are integers and int/int gives int
float/int gives float.

if we make a minor change in the above code 5.0/9
this will give us 37 as teh answer not 37.66 because c has been declared as an int.

we use = to assign value for example char a = 'b'; but to check if to values are equal we use == to check the equality of datavalue
this is (!) negeation operator

similarly we have greater than(>), lesser than(<), greater than or equal to(>=), lessthen or equal to(<=)
&& and || and and or operaotrs

/ bool isEqual = (a==b);
bool isGreater = ( a>b);
bool and = isEqual && isGreater;
bool or = isEqual || is Greater;

cout << " Are a and b equal" << isEqual << endl;
cout << "is A Greater" << isGreater << endl;
cout << "Not Equal" << !isEqual << endl;

If else loop in c++

Let us write a code to compare two numbers:

include <iostream>
using namespace std;
int main() {   
  int a, b;
  cout << "Enter two numbers" << endl; 
  cin >> a >> b;
     if (a==b) {      
     cout << "equal" << endl;
   }  else { 
     cout << "Not Equal" << endl; 
  }
}

-> if u want to comment out a code in c++ you use /* ....*/

-> now let us write the same code using else if

include <iostream>
using namespace std;
int main() {
 int a, b;
 cout << "Enter two numbers" << endl;
 cin >> a >> b;
 if (a==b) {
  cout << "equal" << endl;
 } else if (a<b) {
  cout << "a is smaller" << endl;
 } else { 
  cout << "a is greater" << endl;
 }
}

Lets take another example and write the code for even or odd.

#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter two numbers" << endl;
cin >> n;

if (n % 2 == 0) {
  cout << "Even" << endl;
} else {
  cout << "Odd" << endl;
 }
}
#include <iostream>
using namespace std;

int main() {
  char ch;

   cin >> ch;

   if (ch >= 'a' && ch <= 'z') {
     cout << "0";
   }
   else if(ch ›= 'A' && ch <= 'Z') {
     cout << "1" 
   } else {
     cout << "-1"
   }

return 0;
}

while loop:

example printing numbers from 1 to n.

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter n" << endl;
    cin>> n;

    int i=1;
    while(i<=n) {
       cout << i << endl;
       i=i+1;
    }
}

example: prime numbers

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter n" << endl;
    cin>> n;

    int d =2;
    while (d<n) {
       if (n%d == 0) {    
         cout << "Not Prime" << endl;
       } else { 
         cout << "Prime" << endl;
       }
    }
}