c++ Book by Krishna Vishwakarma
c++ Book by Krishna Vishwakarma
The c++ is a object oriented static type programming language. It is popular as fastest programming
language in the world. It is also called the world’s hardest programming language, but it is not true
because it is very similar to JavaScript. It is used to make games and super fast applications. In this
presentation we will learn this language from beginner to professional.
In c++ , before writing our code we need to write a small code to include additional properties in
our code. The code below is a example of it.
#include<iostream>
int main(){}
Note that we had created a function (we will learn about it in upcoming chapters) named main , and
your whole code will go in its code block (in the curly braces).
Chapter – 3 Printing
To print something in c++, we use “cout<<” method. We call it “c - out” not “cout” because it
sends a c – value out of the programme (in the console). To use this method we write it like it.
#include <iostream>
When we write something we adds line breaks to make our sentence more readable , but in c++
no matter how much space we leave to make a line break , it always writes our sentence next to
each other. To prevent this we need to add line break. To add line break we’ll use “endl”
method. It stands for end line, and there is two ways to use it. Here is the first way.
#include <iostream>
Int main(){
cout<<endl;
#include <iostream>
Int main(){
Note that whenever we close double quotes and want to add something after we will need to add two
"<<";
Note that endl is not a string (a data type that we will learn in upcoming chapters) so you will not have
to write it inside double quotes, if so it'll be written in console and not give a line break.
Chapter - 5 Variables.
In c++ we will have to write same number more than once. To do that , we stores that number in a
variable. But before we store something in a variable, we will learn how to create a variable. To create a
variable that can store a number we starts with "int" than we gives a name to that variable so we can
use it anywhere in our code.
Now you know how to create variables it's time of assigning them a value, there are multiple ways to do
it ,but we will talk about two easiest ways. The first way is we make a variable than in the next line we
assign it a value just like in example given below.
#include <iostream>
Int main(){
int x;
x = 8530458;
In the second way we will create a variable and then assign it a value in the same line.
#include <iostream>
Int main(){
int x = 8530458;;
Note that int type variable can only store numeric values.
There are five types of variables in c++ and each type of variable can store a certain type of values. We
will learn about all types of variables in this chapter.
1. int type.
The int type variables starts with the "int" keyword and they can only store numeric types of values.
#include <iostream>
Int main(){
int x = 8530458;
2. float type.
The float type variables starts with the "float" keyword and they can store decimal numbers ( that int
type can't ).
#include <iostream>
Int main(){
float x = 8.530458;
3.bool type.
The bool type variables starts with bool keyword and they can only store either "true' or "false".
#include <iostream>
Int main(){
bool x = true;
bool y = false;
Note that Boolean values true and false doesn't go inside double quotes.
4. double type.
These types of variables can store both numbers and decimal point values.
#include <iostream>
Int main(){
double x = 8530458;
double y = 8.530458;
Now you know what is the variable types and what they does, so from now we are going to learn how to
print variables on our console.
To print variables we just use "cout" method and the variable name.
#include <iostream>
Int main(){
int x = 123;
cout<<x;
Note that we do not use any quotes here because if we want to print a variable's value we will have to
write it directly without quotes.
What if we want to print a variable's value after a sentence? Then we will have to use the following
snippet.
#include <iostream>
Int main(){
int x = 123;
A programme is very boring if users cannot pass their own values to code. In such cases where we needs
to get users input to our code we uses "cin>>" method. We calls it 'c-in' not "cin" because it receives a
c-value in programme (from console). To use this method , we will need an existing variable (it's name
can be anything).
#include <iostream>
Int main(){
int x ;
cin>>x;
Here that code exactly means that first create a variable named "x" than take a user input in it. After
user enters a value it automatically gets stored inside the "x" variable so we can use it anytime.
Note that unlike "cout" method, "cin" method has ">>" as breakpoints so make sure you are using right
breakpoint at right place.
Chapter - 9 Using user input.
Now when we know how to take inputs from user, we will learn how to use that input in our code.
So we just have to use that variable where we stored user's input, for example given below is a basic
model of a calculator, where user enters two values and output makes the sum of those numbers.
#include <iostream>
Int main(){
int x ;
cin>>x;
int y;
cin>>y;
#include <iostream>
Int main(){
int x=5 ;
int y=10;
The output will show "15" because the sum of the value of x and y evaluates to "15".
Note that if an arithmetic operator is inside double quotes than it will not work as expected.
Chapter - 10 If statement.
Sometimes , we want our code to do its work only if a condition matches. To do so , we uses "if
"statement.
The "if" statement checks if a condition is set to true (we will learn about it in upcoming chapters).
1. a condition.
it will be very illogical to use a if statement without a condition, so first we will pass it a condition.
2. A code to run.
After the if statement checks the condition, it will need a code to run or return( we will talk about it in
upcoming chapters) , so we simply pass it a code.
if (condition){
Note that the code of a "if" statement always goes inside of its code block (in curly braces).
Note that if condition does not match then the "if" statement just skips its code block.
The "if" statement only works if the condition is true so it is like saying "If something is true then just do
that".
The "if-else" statement is very similar to "if" statement, but it has something extra.
In "if-else" statement , we write a "else" keyword followed by curly braces("{}") the a code to run.
The code of "else" statement only works when the "if" statement code does not execute.
So its like saying "If something is true then do it, otherwise do that".
if (condition){
// A code that you want to run when the condition does not match.
Note that if the "if" statement condition is true then the "else" statement code never works.
Note that the "else" statement do not have any conditions, because it should only run it the "if"
statement condition does not match.
In our code sometimes we have to check multiple conditions, for example "if the color is red then do this
or if the color is blue then do that".
To check multiple conditions with the same code, we use 'Else-if' statements.
The 'else if' statement needs a condition to check and it works the same way as the 'if' statement.
if (condition){
} else if (condition_2){
// A code that you want to run when the second condition matches.
} else {
// A code that you want to run when both conditions does not matches.
Note that if you can use unlimited 'else if' statements in your code.
Note that when you use 'else if' statement you must always use a 'else' statement.
Chapter - 13 Logical operators.
So recently, we learned about "if , if else , else if" statements and their conditions, but how so we
describe those conditions? The tell our code what are the conditions, we use logical opertors.
In c++, there are multiple types of logical operators, in this chapter we are going to learn about all
common logical operators.
The greater than operator checks if the value on left is greater than the value on right.
It is true when the value on left is greater than the value on right, otherwise it returns false.
int x=10;
int y=5;
if (x > y){
// Any code here will run, because the value of variable x is greater than the value of the variable y, so
the condition is true.
The greater than or equal to operator checks if the value on left is greater than or equal to the value on
right.
It is true when the value on left is greater than or equal to the value on right, otherwise it returns false.