C Plus Plus Notes
C Plus Plus Notes
C Plus Plus Notes
Written By Robaidali
ROBAIDALI
12/16/2012
*Language: Language is the source of communication among users.
*Computer Language: Source of communication Between user and computer or computer and
Computer are called computer language. Computer language are also
*Types Of Languages: There are three types of languages which are given as below.
1- Low Level Language: That language which is directly understandable to the machine and not to
the user are called low level language. Low level language are also called machine oriented or
coding language. It was a direct source of communication but due to coding it was very difficult.
2- High Level Language: That language which is directly understandable to the users and not to the
Computer are called high level language. High level language are also called English oriented language.
Example of high level language are C/c++ language Java and Oracle.
3- Intermediate level language: That language which is understandable to both user and computer are
………………………………………………..
required. Therefore “A device or a system software which convert computer language into alpha-
batacle language and alphabetacle into computer language are called translator.
1- Compiler: A type of translator software which is used in C-language are called compiler.
2- Intrepretor: A type of translator software which is used in BASIC language are called interpretor.
Intrepretor is more advanced translator than compiler because compiler checks the errors in
the end of a program when program is executed while interpretor checks the error in each line
3-Assembler: A type of translator software which perform the function of both intrepretor nad compiler
………………………………………………………
*History Of The C/C++ language: History of C/C++ language means that how this language was
developed and before this language what type languages were used. So let us give a brief
discussion.
1- BCPL : BCPL stands for basic computer programming language. BCPL was invented and developed by
Martin Richard in 1967.
Function. This language was used in preparing operating systems and also it was used for compiler
construction.
2-B-Language: This language was developed and invented by Ken Thomson in 1969.
3-C-Language: From the combination of both B and BCPL language another type of language was developed
Called as C_language. C-language was invented and developed by Dennish Ritch in 1972.
Function. C is a language having multi datatypes and also system and applications softwares and computer
4-C++ Lnaguage: After the C_language another advanced language was developed called as C++ language.
Copy and paste it when we need it next time and there is no need to create it second time.
……………………………………………………
*Character Set: A set of symbols and characters which are used in the C/C++ language are called as
1-Numeric Character Set: A character set which consists of 0 to 9 digits are called numeric character set.
2-Non-Numeric Character Set: A character set which consists of small and capital alphabets from A to Z
3-Special Character Set: A character which consists special symbols used for special purpose are called as
…………………………………………………….
*Elements Of The C/C++ Language: There are five elements of the C++ language discussed as under.
1- Resrve Word/ Key word/ Pre-define word
2- variables
3- Constants
4- Operators
5- Tokens
1- Reserve word: Reserve word are also called keyword or pre-define word.Reserve words are those
words which have specific meaning meaning in the C++ compiler and which are used for specific
purpose. E-g (main, clrscr, cin, cout, getch, integer, float, character, if-else, for, while, case and switch)
2- Variables: Data in the form of a,b,c or x,y,z are called variables. Or “Variables are those memory
locations which store some values and these values may be changed during the execution of program”.
First letter of the variable name must be alphabetic. E-g ( R_no. 3 and not 3R_no. )
Only a special character Underscore(_) can be used before a variable name. as _Alikhan.
No space or any another special character can be used before a variable name except underscore.
Reserve word as discussed earlier cannot be used in place of a variable name.
The maximum length of the variable name depends upon the C++ compiler. Normally its range is
upto 38 characters.
3- Constants: A quantities that cannot change their values during the execution pf program are called
constants. There are two types of constants.
1-Numeric constants. Numeric constants are composed of 0 to 9 digits. Numeric constants consists
of signed values as(+32, +98, +45, -65, -47) and unsigned values as ( 56, 89, 52 etc).
Types of numeric constants: There are two types of numeric constants.
i- Integer constants. Numeric constants having no decimal point are called integer constants e-g 35,
45,89, etc.
ii- Floating/point/realconstants. Those numeric constants having decimal point are called real
2- Non numeric constants. Those constants which consists of alphabets, expressions, are called non
i -Character constants. Non numeric constants composed of a to z characters are called character
constants. Character constants will always be single and enclosed in a single quote(‘A’).
ii- String constants. String constants consists more than one character. It will always enclosed in a
4- Operators: Special symbols used to take some action are called operators. There are many types of
operators.
Arithematic Operators. They are ( +, --, *, % etc )
Relational Operators. They are ( =, <, >, ==, >=, <=, etc )
Logical Operators. They are And($), OR(: :) , NOT(!) .
Increment Or decrement Operators. They are ( ++, -- ).
Assignment Operators. They are ( = )
Conditional Operators. They are ( IF, IF-else ) etc.
5-Token:
*Every C++ Program Consists of Tgree Basic Components.
OR
1. Pre-processor Directive: Pre-processor directive are also called Compiler directive. Pre-processor
directive means a collections of files which had already processed by the processor. E-g. Include is
pre-processor directory which contain (Iostream.h) and (Conio.h) as header files. We will use #
symbol before a directory and after directory we will enclose the header files in the angular brackets
as. #Include<iostream.h> and # Include<conio.h>. Instead of angular brackets( < > ) we can also
use double quotes as #Include”iostream.h” and #Include”conio.h”.
2. Main Function: Main Function act as heart for the program, because it requests O-system or take
permission from O-system to start a program. E-g. Void main() , Void main(void) , getch(); , clrscr();
or main() are the examples of main functions. Main function are represented by parenthesis ( ).
3. Program Statement: It is the actual program we are going to write. It start by open bracket { and
end by the close bracket } .
……………………………………………………
*Expressions: The combination or collection of operands ( 1,2,3,4,5…..) and operators ( +, --, * ) are called
Expressions. Operands may be constants as (2+3) and may also be variable as (x+y).
Types of Expressions;
1) Arthematic Expressions: They are plus , minus, multiplication, and division signs.
2) Relational Expressions: They are ( >, <, =, ==, ) etc.
3) Logical Expressions: They are And, OR, NOT signs.
<< >> ANGULAR BRACKETS USE TO CLOSE AND OPEN HEADER FILES
: COLON
*Programme For The Summision Of Two Numbers And For general Output.
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
{ {
clrscr(); clrscr();
cout<<”sum”<<sum; getch();
After completing this programs go to compile menu and click on compile and then go to Run
Note: Menu and click on Run so the programs will be executed and will show sum=100. And other will
……………………………………………………….
#Include<iostream.h> #Include<iostream.h>
#Include<conio.h> #Include<conio.h>
{ {
Clrscr(); clrscr();
Cout<<”sum=”<<sum; sum=num1+num2;
Getch(); cout<<”sum=”<<sum;
} end. Getch(); and below getch put } and run the programmes.
……………………………………………………………………………………………………….
*Input Statement: That statement through which we enter or provide data to the computer programme
are called input statement. In the C++ programe “Cin” statement are called input statement whre Cin
*Programme for the addition of two numbers with the help of input statement( Cin ).
#Include<iostream.h> Cin>> y;
{ }
Clrscr(); end.
Int x, y, sum;
Int sum=x+y; Note. After running this program a black screen will
………………………………………………………….
*Statement: A set of instructions which are used to perform specific action or s pecific task are called
Statement.Every C++ programe have set of instructions or statements.
There ar e two types of statements.
1- Simple Statement. A statement which consists of a single line of instructions are called simple
statement. E-g
sum=x+y; ) or ( y=45;) etc are simple statements.
2- Compound Statement. A statement which consists of more than one line of instr uctions and
enclosed in a pair of braces {} are called compound statement or als o called Block statement.Every
compound or block statement will must start with { and end with }.
Sum=x+y; }
………………………………………………..
1- *Upon Condition There Are Two Types Of Statements:
There are two types of statem ents upon Condition. Both are discussed as under.
1- Conditional Statement. Statement contain any condition are called conditional statement.
Or
Statement contains relational operators as >, <, =, == are called conditional statement.
There are many types of conditional statements given as.
> IF-statement
> IF-else statement
> Nested-If statement
> Switch-statement.
2- Non-conditional statement. Simple statement which does not contain any condition are called
non conditional statement.
*IF-else Statement: It is another form of the " If " statement which is used to make two way decision. In
the If-else statement one condition and two blocks of the statement are given. After checking the codition
either one of the two blocks of statement are executed. If the condition comes true then the first block of
the statement is executed. While if the condition is false, then the first block of the statement is ignored
and the second block of the statement which is af ter the else statement are executed (run).
Programe #1
#Include<iostream.h>
#include<conio.h>
void main()
clrscr();
Int eng,urdu,chem,bio,maths;
Cout<<"A1 grade"<<endl;
Cout<<"A"<<endl;
cout<<"B"<<endl;
cout<<"C"<<endl;
cout<<"D"<<endl
………………………………………………………………………………………………………..
* IF- Statement. The If-statement is used to execute(run) or ignore a set of sta tements after checking
or testing a condition. After the condition is checked or tested, if the condition comes true, then statement
following the if statement is executed or otherwise it will not be executed if the condition is not true. I f the
given condition is false then statement following the if-statement ignore s the condition and process the
next statement.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int marks=45;
If(marks>=33)
Getch(); }
*Switch Statement: Switch statement are also called conditional statement. Switc h statement are used
for Multi-way decisions.It is very easy as compared to Nest ed If-else statement. Since it can be used
instead of Nested If-else statement so also it is called the substituent statement.
Switch statement is just like a calculator, through which we can p erform addition,
subtraction,Multiplication, and division simultaneously. In thi s statement condition is one but choices are
made many and it switch to each cho ice numberwise.
#Include<iostream.h> default:
Clrscr();
Int a, b;
Char op;
Cin >>a;
Cin>>op;
Cin>>b;
Switch(op)
Case '+':
Cout<<"addition="<<a+b<<endl;
break;
Case '-':
Cout<<"subtraction="<<a-b<<endl;
break;
Case '*':
Cout<<"multiplication=<<a*b<<endl;
break;
Case ‘/’:
Cout <<"division="a/b<<endl;
break;
*Loop Statement: Statement which are used again and again in a programme are called loop statement.
Types Of the loop statements are given as under.
1- For-Loop
2-While-Loop
3- Do-while Loop The following three steps or tasks are common for all the above loo ps.
* Testing condition
* Incrementation/Decrementation. For incrementation we use (++ )signs while for decrementation we use
(--) signs.
1- For_Loop: The For_Loop is used to execute(run) a set of statements repetedly for a fixed number of
times. It is Also known as counter or pre-test loop. It has the following parts.
> Initilization.
> Condition/test time
> Incrementation/decrementation
> Body of the loop.
For_loop is more flexible, reliable, and easy due to the reason. i-e that incr
ementation/decrementation, condition, and body of the loop are written a single straight line as in the
following programme.
#Include<iostream.h #Include<iostream.h>
#Include<conio.h> #Include<iostream.h> ;
Int i ; int a ;
Cout<<"i ; cout<<"a<<endl;
Getch(); Getch();
Note: If we want to increase a number by one digit we will use i++ but if we want to decrease a number
serial wise then we will use( i-- ). Also if we want to increase the number by 2 then we will give i+ =2; and
vice versa, if we want to decrease the number.
*While_Loop: In the While_loop all the three steps i-e Initialization, condit ion testing, and Increment/
decrementation are taking place in a separate line or written separately. It means that the input method of
statements is in a while_loop is different from the For_loop. In the while_loop statement or set of
statements are Repeatedly executed (run) until the condition comes true.
* { Statement1
Statement2
#Includeisotream.h> #Includeisotream.h>
#Include<conio.h> #Include<conio.h>
Clrscr(); Clrscr();
Int i; int i;
while(i<=5) cout<<"i;
Cout<<"i; getch();
i=i+1;
Getch();
________________________________________________________________________
*Do-While-Loop: This loop is also called post-test loop. It is similar to the w hile-loop.
All the three steps i-e initialIzation , condition testing , and incrementation/decrementation occurs in a
separate line like the while-loop.
The Main difference between while and Do-while loop is that "Do" keyword is used inplace of "while"
keyword and the " While" condition are used after the end of the loop with semicolon.
Note. If the condition is initially is false, then it must be executed one time.
#include<iostream.h> {
#include<conio.h>
void main()
clrscr();
int I;
Do
i=500;
cout<<i<<endl;
i= i-10;
while(i>=0);
Getch();
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Function: A set of element or sub-routine( instructions ) which are used to perform a specific task is
called a function. E.g square root, power, cos0, sin0 , tan0 etc.
1- User define Function/ Specific Purpose Function. That function which are crea ted by user itself, to
fulfill the specific requirements or specific tasks are c alled user-define function. Now there are
three parts of the user define functio n wich are as given under
2- Built-in-Function. A pre-define function , which donot need to be create by u ser itself, but only call
at the time of need are called built-in-function.