C++ Code block
C++ Code block
INDEX
Topics Page No
Introduction 2
Variable 2-5
Data Type 6
Function in string 7
Working with no. 8
Get user input 8-9
If Statement 9-10
Switch Statement 10
While loop 10
For loop 11
2D Arrays & Nested Loops 11
Comment 12
Pointer 12
Classes & Object 13
Construction Function 14
Inheritage 14
#include <iostream>
Count << “Hello World” << endl ; Command we give under these. Cout = Console our
Variable
A variable is a container where we put some piece of data that will help us to keep record and manage
those piece of data .
E.g. Lot of time in our data there are some character that repeat many time like character name and if
we want to change name of character later on or if we have not decided the name of character name
yet we can create variable for that which will insert character name every where we want to insert that
that will make it easy inspit of going to every corner and change the character name.
1. STRING
String is a variable which contain words ,letter, sentences like “My name is __”
How to insert string as a variable
I. Type string inside int main () : it’s a container
II. Give these container a name says : CharacterName
III. Give these container a value, which will contain the charactername which we want to
give to our document : John
String CharacterName = “John”
2. INTEGER
It contains whole no.
2|Page
OR
BUILD RUN
BUILD AND RUN
3|Page
Code we write
Output we get
4|Page
If in half of the story i want to change the name of character i should do these:
We can see at first part name is John and in second part its mike.
5|Page
Data type:
It is basically just means a type of data or info that we can use or work with C++ program like
text, no. , decimal no. , true& false value ,etc.
Output :-
6|Page
Function in string :-
.length() :- Tells no. of character inside those string
.find() :- Locate the position of particular letter or group of letter
.substr ( 8, 2) :- Substring remove those letter which we don’t want of print like in these case we
have commanded that remove 2 letter from 8th letter on word.
[] :- print a particular word of the string e.g, [0],[1],[4] etc.
We can change the character letter \
Eg. Word [2] = ‘W’ . These will change the [2] character to W.
Output:-
107ooImU am groot – 10 7 oo Im U am groot
7|Page
Working with No:-
++ :- these will add 1 value to the no.
-- :- these will subtract 1 value from the no.
cout << pow (2 , 5); :- For power
cout << squt (2 , 5); :- For squaroot
cout << round (4.5); :- Removes decimal value , round the no.
cout << ceil ( 4.2); :- Round the no. to next high whole no.
cout << floor (4.8); :-Round the no. to previous whole no.
cout << fmax (5 , 10); :- Tells which one is bigger.
cout << fmin (5 , 10); :- Tells which one is smaller. #include <cmath> :- these tells the program
that we are going to do some math function
A calculator code:
Arrary :-It is a container like a variable but it can hold a lot more dara then a variable there for it is used
in huge works . It is created as a normal variable. Just difference is that we use [] these bracket In arrary.
8|Page
Return :- When ever we use return function we tells c ++ that we are done with executing code in that
function. It’s a kind of full stop. Code after return is not going to executed.
If Statement :-
If statement allow our program to react to different situation differently . Essentially we check certain
condition and when the condition are true we do different thing and when they seems to be false we
take different action.
In these code it is said that if the isMale statement is true then “You are a Male” else it will print “You
are not Male” .
&& :- It’s a “and” operator use while there are two variable.
9|Page
10 | P a g e
If statement to compare different values :-
Switch statement :-
It’s a kind of if statement which allow us to check one particular value against a bunch of value. A switch
statement is used where we have a lot of if statement to write so inspite of writing those numbers of if
statement we could use switch statement. Eg, giving a integer to every days of the week.
While loops:-
It’s a program structure that allow us to loop through block of codes while a certain condition is true.
11 | P a g e
For loops:-
It’s a kind of tracker which allow us to find how many times we are been through the loops.
Nested loops:-
12 | P a g e
Comment:-
Comment is some thing which would be ignored by C++ by useful by the user.
// ;-Comment are written by using these. These only help to write 1 line .
Pointer:-
All the data that we put in the code have some physical address in our computer as while working or
RAM is storing all the data . To find out those address we use pointer.
13 | P a g e
Classess & Object :-
There are many type of variables like int ,float ,etc but all the things present in the real world cannot be
represent by some limited no. of variables. Like there is no variable to represent an object present in the
real life eg. Keyboard ,mouse ,phone , etc. So we could create a class for such
Class :- A Class is essentially a new data type. We can create a blueprint for a new type of data in our
program. Class is the specification , it’s the blueprint , it’s the template of a data type.
Object :- An Object is the actual instance of class or of that blue print. An Object is the exact object.
In these eg. First we created a class book and specify its attributes that what are the things a
book should have . Then used book as a variable inside int main(){}.
Constructor Function:-
In these constructor help us to shorten the code. The text selected over are known as
constructor. We can also create more the one constructor.
Inheratige:-
14 | P a g e