0% found this document useful (0 votes)
18 views14 pages

C++ Code block

Uploaded by

arafatabrar003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
18 views14 pages

C++ Code block

Uploaded by

arafatabrar003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

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>

These are the starting code


using namespace std;

Int main () is an important function any code we put under {} is executed


int main()

Count << “Hello World” << endl ; Command we give under these. Cout = Console our

Endl :- Is use to change line or we could use \n inside the sting


Return 0 ;

These is a basic Hello world program:

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.

Lets talk about 2 of them

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

Variable Code can be acceptable in both formate.


System will accept both formate

(And for string value we need “ “ these columbs


For no. we doesn’t need those for int value)

BUILD RUN
BUILD AND RUN

3|Page
Code we write

Output we get

Variable values should be put under << <<

4|Page
If in half of the story i want to change the name of character i should do these:

The Output will be

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.

Variable used here are


 Char :- Character , These is use to represent a single character like A , B , G , L etc. These
should be keep under single inverted comma ( ‘ ‘ ).
 String :- These is use to represent word or group of word . These are kept under double
inverted comma ( “ “ ).
 Int :- These is used to represent Whole no. in our program . These doesn’t need
commas.
 Float & double :- These both are use to represent Decimal No. Only difference is that
double can represent much larger no. then float.
 Bool :- These is use to represent True & False value

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

Get user input :


Cin >> :- Use to get inpute from user.

In case of string we don’t use cin >> we use ,

Getline (cin, string value eg name) ;

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.

Function :- A function is a collection of code that performs a particular task.

Eg. Int main { } , void main {}

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.

|| :- It’s a “or” operator use while there are two variable.

! :- Not operator :- In these eg. Is isMale statement is true and isTall


statement is NOT true (False). Then the output will be “You are a short male” . These operator can be
used vise versa.

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.

2dArrays & Nested Loops:-


 A 2d Arrays is a situation in which every single element inside the arrays is a array.
 A Nested for loop is a situation in which we have a for loop inside another for loop.

2d arrays code :- int num [3][2] ={ Things to know about:

{1,2}, First [] if arrays tells us how many


elements are there &
{3,4},
Eg {1,2},{3,4},{5,6}
{5,6}
Second [] tells how many elements are
there inside the arrays elements eg.1,2 ;
};
3,4 ; 5,6
Cout << num [1][1]; output=4

Cout << num [0][0]; output=1

Cout << num [2][1]; output=6

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 .

/* :- These sign use to write a long block of cmment it is closed by */ .

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

You might also like