C++ Basics — Part I (for beginner’s)
C++ is a general-purpose programming language.
(and a lot more)
Before diving into code you must have compiler installed in your system.
Compiler — A program that converts High Level Language(HLL) into Low Level Language (LLL).
HLL :
int fib(int n) { int store[n+2];
store[0] = 1;
store[1] = 1; int i;
for(i=2;i<n;i++) {
store[i] = store[i-1] + store[i-2];
} return store[n-1];
}// Any language that you can code in, and is very close to "English" (a natural language) which is easy for a human to read, write and understand.
LLL :
mov edx, [esp+8]
cmp edx, 0
ja @f
mov eax, 0
ret// Commands or functions in the language map closely to processor instructions.
Windows Users : Install CodeBlocks (IDE) from here
Linux Users : Install g++ (highly recommended) and a CodeEditor (Sublime or VSCode)
Lazy Users : (yes lazy) : Can use online IDE’s , hackerearth, ideone .. etc.“I recommend you to always refrain from online IDE’s especially during a contest”
before we start, you must know that a C++ file has extension ‘cpp’, while a C file has extension ‘c’.
Let’s Start
#include<iostream>
using namespace std;int main() {
// A simple program to print "Hello World"
cout<<”Hello World!”;
return 0;
}Output: Hello World!
Now with explanation
#include<iostream> // iostream is a header file, it contains
// definition of cin & cout
using namespace std; // if this were not used then we'd have to
// write std::cout<<"abcd"; in the program
int main() { // declaration of function with name
// 'main' and return type of integer.
// the definition is found within "{}"
cout<<"Hello World!"; // cout prints output on the terminal
// notice - '<<' this is an operator
// notice - '//' statements beginning with
// these characters do not get executed,
// they are used for comments
return 0; // function main would return 0
// (an integer)
}
Let’s Math
#include<iostream>
using namespace std;int main() {
cout << "3 + 2 = " << 3+2 << endl;
cout << "3 - 2 = " << 3-2 << endl;
cout << "3 * 2 = " << 3*2 << endl;
cout << "3 / 2 = " << 3/2 << endl;
cout << "10 / 3 = " << 10/3 << endl;
cout << "10.0 / 3 = " << 10.0/3 << endl; return 0;
}OUTPUT :
3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
10 / 3 = 3
10.0 / 3 = 3.33333
Notice 3 / 2 = 1 and 10 / 3 = 3, yes you guessed it correct, only the integral part is retained.
This happens because by default data-type is integer. Unless specified like in example 10.0 / 3 = 3.33333 (any operation that includes a decimal will result in one)
Book : Balaguruswamy OOPs with C++ (recommended)
Please go through chapter 3, 4 for a detailed content.
“Recommended”
All the chapters are important and would be covered in your course in span of one to two semesters. I cannot cover everything in this blog hence provided you with a resource.
Topics that you must know before moving forward are:
- Variables (data type)
- Loops (for, while, do)
- IF ELSE
- Arrays & Matrix
- Functions
Important: To learn C/C++ you must read a book and run your code.
There is no other easy way.
(do not watch videos for this, start reading the book)
The next part would focus on getting a good grip on the language.