C Plus Plus Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

C PLUS PLUS NOTES(C++)

Written By Robaidali

Notes Of The C++ For First Semester Of One Year Diploma

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

Called C-language. Examles of computer languages C/C++ , Cobol , Froton

*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.

Example of low level language are machine language or bainary language.

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

Called intermediate level language. Example of this language is Assembly language.

………………………………………………..

*Features Of The C/C++ Language Or Introduction To The C/C++ Language:


1. C is a powerful high level computer programming language. It means it is an advanced language
through we can perform a lot of tasks.
2. C is a language in which system and application softwares can be developed.
3. C is a language having multi data types such as float, integer,string and characters etc.
4. C is a structure oriented language which means every program written in this language has its own
specific structure.
5. C is a case-sensitive language which means case once used in a program should be kept in mind
next time.
6. C is a strong compiler translator.
7. C is a general purpose programming language which means evry type of programming can be done
in this language and general calculations.

Note. Data types are mainly of two types


1-Numerical datatype: It consists float (having decimal point) and Integers (having no decimal
point).
2-Non numerical datatypes: It consists of characters ( a,b,c…..) and string ( combinations of
characters (ali,kamran).
*Translator: Computer doesnot understand our alphabetical language(a,b,c…) and we did not understand
the binary language( 0,1) of a computer. So for this purpose a device or a system software is

required. Therefore “A device or a system software which convert computer language into alpha-

batacle language and alphabetacle into computer language are called translator.

*Types Of Translator Softwares;

1- Compiler: A type of translator software which is used in C-language are called compiler.

For example C/C++, Java , Visual Basic.

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

of a program during the writing of a program.

3-Assembler: A type of translator software which perform the function of both intrepretor nad compiler

are called Assembler.

………………………………………………………

*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.

Function. This Unix operating system was developed in this language.

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

Games etc are also developed in this language.

4-C++ Lnaguage: After the C_language another advanced language was developed called as C++ language.

C++ language was invented and developed by B-Jorn Stroutay in 1980.


Function. The advancement of this language is that when one time the object is created then we can simply

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

Character set. There are three main types of character sets.

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

Are called non-numeric character set. E-g (A,B,C…………….) (a,b,c……………)

3-Special Character Set: A character which consists special symbols used for special purpose are called as

Special character set. For example [ #, !, *, <, >, =, ==, ; ]

…………………………………………………….

*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”.

Rules For Naming a variable:

 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

constants. E-g 65,48,98 etc.

2- Non numeric constants. Those constants which consists of alphabets, expressions, are called non

Numeric constants. There are two types of non numeric constants.

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

double quotes. E-g ( “Ali”).

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

To Write a C++ Program It Require Three Steps.

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 } .

*Some Importany Hints About C++ Programme.

 Directory is a collection files which has already processed by the processor.


 Files are the collections of documents written before.
 Collections of directories then form a programme.
 Symbol << used with Cout are termed as “double lessesr signs” or “put to operators”.
 Symbols >> are known as “get from operators”.
 For ‘Cout’ and ‘Cin’ Coding has already done in the header file (Iostream.h).
 For ‘Clrscr’ and ‘getch’ coding are already done in the header file ( Conio.h).
 ‘Cin’ statement is more flexible than cout.
 To undefined a directory we use // symbols before it.
 ‘’Endl’’ which stands for end of line are used to show text in new line.
 Semicolon ( ; ) are used for ending a statement in C++ programme.
 ‘Clrscr’ ‘getch’ ‘cout’ and ‘Cin’ are commands used for various purpose in C-programme.
 Clrscr command is used to clear the screen
 Cout command is used to get output of a programme.
 Cin command is used to enter data on the screen according to our choice.
 Getch is a command used to get result of our written programme.
 Collections of statements used to perform a specific job are called a programme.
 ‘{‘ symbol are used before starting a programe statement and ‘}’ are used to end a programe
statement.
 Always use Turbo C++ 3.0 for C- programming.

……………………………………………………
*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.

SYMBOL NAME OF SYMBOL FUNCTION OF SYMBOL

# HASH or NUMBER SIGN USE BEFORE THE PRE-PROCESSOR DIRECTIVE

’ SINGLE QUOTE USE FOR CHARACTER DATA TYPE

” DOUBLE QUOTES USE FOR STRING DATA TYPE

; SEMI COLON USE FOR ENDING OF C++ STATEMENT

!= NOT EQUAL IT IS AN CONDITIONAL OPERATOR

= ASIGN TO USE TO ASIGN VALUES TO SOME VARIABLES

> GREATER THAN USE FOR COMPARIOSN OF VALUES

< LESS THAN USE FOR COMPARISON OF VALUES

== EQUAL SIGN USE FOR COMPARISON OF VALUES

+,─,×,÷ PLUS,MINUS,MULTIPLY,DIVISION ARTHEMATIC OPERATORS USE FOR ARTHEMATIC SOL

/, \,─,% BACK $ FORWD SLASHES,SPACE USE FOR ARTHEMATIC PURPOSE

<< >> ANGULAR BRACKETS USE TO CLOSE AND OPEN HEADER FILES

{ } BRACES USE FOR OPENING AND CLOSING OF C++ STATEMENT

( ) PARENTHESIS USE FOR CONDITIONS IN C++ PROGRAMMING

[ ] SQUARE BRACKETS USE FOR ARRAYS

: COLON
*Programme For The Summision Of Two Numbers And For general Output.
#include<iostream.h> #include<iostream.h>

#include<conio.h> #include<conio.h>

void main() void main()

{ {

clrscr(); clrscr();

Int x=55; cout<<”\t\tName Robaidali\n\n”;

Int y=45; cout<<”\t\tF.name Abdul waris\n\n”;

Int sum=x+y; cout<<”\t\tCell.no 0301-2713927\n\n”;

cout<<”sum”<<sum; getch();

getch(); and below this use } }

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

Show name, f.name, and cell.no on the screen.

……………………………………………………….

*Programe For The Addition Of Two Numbers In Which One Is Float.

#Include<iostream.h> #Include<iostream.h>

#Include<conio.h> #Include<conio.h>

Void main() void main()

{ {

Clrscr(); clrscr();

Int x=25; int num1,num2,sum;

Float y=35.8; num1=65;

Float sum=x+y; num2=45;

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

stands for ‘console input’.

*Programme for the addition of two numbers with the help of input statement( Cin ).

#Include<iostream.h> Cin>> y;

#Include<conio.h> cout<<”Enter value for y=”endl;

Void main() getch();

{ }

Clrscr(); end.

Int x, y, sum;

Int sum=x+y; Note. After running this program a black screen will

Cin>> x; come we will put values for x and y and press

Cout<<”Enter value for x=”endl; enter button from key board.

………………………………………………………….

*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.

Every statement will have semicolon (;) at the end.

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 }.

Example of compound statement are given as under.


{
X=45;
Y=35; Compound statement

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).

*Example on the If-else statement are explained as under.

Programe #1

#Include<iostream.h>
#include<conio.h>

void main()

clrscr();
Int eng,urdu,chem,bio,maths;

float per; obt,total=500;

cout<<"Enter marks for eng"<<endl;


cin>>eng;
cout<<"Enter marks for urdu"<<endl;
cin>>urdu;
Cout<<"Enter marks for bio"<<endl;
Cin>>bio;
Cout<<"Enter marks for maths"<<endl;
Cin>>maths;
Cout<<"Enter marks for chem"<<endl;
Cin>>chem.;
Obt=eng+urdu+chem+bio+maths;
Per=(obt*100)/total;
Cout<<"Total obtained marks"<<obt"<<endl;
Cout<<percentage"<<per;
Now for giving grades we use.
If(per<100 && per>=90)

Cout<<"A1 grade"<<endl;

elseIf(per<90 && per>=80)

Cout<<"A"<<endl;

eIseif(per<80 && per>=70)

cout<<"B"<<endl;

elseIf(per<70 && per>=60)

cout<<"C"<<endl;

elseIf(per<60 && per>=50)

cout<<"D"<<endl

elseif(per<50 && per>=40)

else cout<<"The student is fail”;

………………………………………………………………………………………………………..

* 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.

Example on the If statement are given as under.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int marks=45;

If(marks>=33)

cout<<"The student is pass";

If(marks<33; cout<"The student is fail";

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.

Example or a practical programme on switch statement are given below.

#Include<iostream.h> default:

#Include<conio.h> cout<<"please enter correct operators<<endl;

Void main() getch()

Clrscr();

Int a, b;

Char op;

Cout<<"Enter value for first number;

Cin >>a;

Cout<<"Enter mathematical operator;

Cin>>op;

Cout<<"Enter value for 2nd number;

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.

* Initialization. Initialization means starting of a loop or a starting point of the loop.

* 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.

Programme on the For_loop are explained as under.

#Include<iostream.h #Include<iostream.h>

#Include<conio.h> #Include<iostream.h> ;

Void main() Void main()


{ {
Clrscr(); Clrscr();

Int i ; int a ;

For(i=1; i<=10; i=i+1) For(a=100; a>=10; a=a - 10)

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.

Syntax of the while-loop: While-loop has the following syntax.

* Ist step is initialization which is the start of a while-loop.

* While(condition) is the 2nd step.

* { Statement1

Statement2

Statement3} is the 3rd step.

Example or a Practical Programme are given as under.

While-loop Method For-loop Method

#Includeisotream.h> #Includeisotream.h>

#Include<conio.h> #Include<conio.h>

void main() void main()

Clrscr(); Clrscr();

Int i; int i;

i=1; for(i=1; i<=5; i=i+1)

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.

Syntax Of The Do-While-loop are given as under.

Programme on the Do-while-loop

#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.

There are two main types of functions.

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

> Fuctiuon declaration or function prototype.


> Function calling
> Function definition.

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.

You might also like