Вы находитесь на странице: 1из 14

emeronTI13

WRITING

PROGRAM WITH C++

I. Introduction to C++ Language:

CaPasakmviFITUeTAmYyEdlsitenAkg
( High Level Programming Language) . eKbegIt C
PasakmviFIkMritx<s;
edIm,Isresr System program eKfa C CaPasa edIm kMeNItrbs; UNIX Operating
System.
C/C++
RtUv)aneKcat;TukCaPasamYymanRbCaRbiykgkarsresrPab;
Application CamYynig System edayPasa C/C++ CaPasakMritx<s; (High Level
Language) nig man Compiler bkERbBak bBaaeGayeTACaPasa ma:sIun rYc
mkCaPasaEdlGacGan)anedayPasarbs;mnusS. C minGaRsyEt enAelI
O.S(Operating
System)
rW ma:sIunNamYyedayELkeLIy mannyfa C
RtUv)aneKykeTAeRbIR)as; CamYy O.S nig ma:sIunepSgtam Object rbs;va.
qaM 1970 elak Ken Thompson )anerobcMPasa B Language
Edlman\TiBlenAelI Basic Computer Programming Language EdlbegIteday elak
Martin Richard.
qaM 1972 elak Dennis Ritchie )aneFVIkarEkERb B Programming Language
eGayeTACa C Programming Language enA Bell Telephone Laboratories.
C/C++

Programming

Language

257

qaM 1978 elak Brain W Kernighen nig Dennis Ritchie )ane)aHBum<esovePA


The C Programming Language (First edition) .
qaM 1982 eTIbeKbegIt ANSI(American National Standard Institute) edayepIm
sresr enA GnuKmn_Edl Ca Standard Library bBaleTAkg Compiler
edIm,IeRbIR)as;CaTUeTArhUtmkdl;sBVfenH.
enAqaM 1988 eKcab;epImbegIt OOP( Object Oreinted Programming)
edayelak Brjare Stron Stup enA Bell Laboratories EdlkgeKalbMNgedIm,IbegItnUv
Application Wizard nig System Programming.
II. TMrg;TUeTAn C++:
#include<iostream.h>//Header file/Preprocessor/Library
int main(){//

cMnuccab;epImnkmviFI

statement1;
statement2;
.;

Block of statement

bBaak;fadMeNIrkardl;TIbBab;
}// cMnucbBab;nkmviFI
return 0;//

#include<iostream.h>//Header file/Preprocessor/Library

cMnuccab;epImnkmviFI

void main(){//
statement1;
statement2;
.;
}//
-

Block of statement

cMnucbBab;nkmviFI

ebIkkmviFI C/C++:

- C:\> TC\Bin\Tc.exe
- Compiler: Alt +F9
- Run (Make EXE): Ctrl +F9
- Copy: Ctrl+Insert
258

- Past: Shift+Insert
- Cut: Shift+Delete
Source Code
COMPILER

TEMPORARY

OBJECT + LINK

.EXE

III. Feature of iostream.h: iostream.h (Input/ Output Stream library) Ca

sMrab; Input/ Output stream


EdlmanmuxgarCaGkbkRsaykarbeBaj nig karbBal Tinny. vapk function
cin sMrab;bBalTinnytamryH Keyboard nig function cout
sMrab;karbeBajTinnyenAelI Screen .
Standard header file/ Standard library

Ex:
cout<< Hello world!<<endl;
cout>>x;

IV. GnuKmn_ getch( ):

eRbIsMrab;Tb; Screen output edIm,IeGayeyIgGacBinitlTpl (Result)


nkmviFIenAeBlEdleyIg dMeNIrkarkmviFI . GnuKmn_ getch( ) sit enA library conio.h
(Console Input/Output library) .
V. Input and Output function:
- cout<< String/Variables;
Ex:

eRbIsMrab;bgaj string b tMlmkelI Screen .

int x=10;
cout<< x=<<x;

- cin>>Var1,var2,var3, ...;

eRbIsMrab;bBalTinnytamryH Keyboard

eTAeGay var1,var2,...
Ex:

int x;
cin>>x;

259

VI. Data Type:


VI.1. Definition of Data-Type:

CakarkMNt;lkNHeTAelIGefr (Variables)

nimYy Edl)an Rbkas.


VI.2. Type of Data-Type: Data type
-

maneRcInRbePTdUcCa

Scalars type
Structure type
Pointer type
Data Type

Scalar Type

Structure Type

- int

- array

- float

- file

- double

- struct

Pointer Type

- pointer

- Boolean
- ...

Keyword

Byte

Char

Int

Long

Float

Double

Unsigned int

Limited

dl; 128
-32768 dl; 32768
-2147483648 dl; 2147483648
3,4 .10 dl; 3,4 .10
1,7.10 dl; 1,7.10
0 dl; 65535
-128

260

-38

38

-308

308

GnuKmn_ Sizeof()eRbIsMrab;pl;TMhMnRbePTrbs;Tinny (Data type


)pl;tMlCacMnYn integer.

Note:

Syntax:
int sizeof(Data-Type/var);
Ex:
cout<<Size of Integer=<< sizeof(int);
Output:
Size of Integer=2

VII. Variables:

CaGefr b Address
eRbIsMrab;rkSanUvtMlCabeNaHGasnkg Memory enAeBlkmviFIdMeNIrkar (Run time) .
tMlenHRtUv)an)at;bg;eTAvijenAeBleKQb;dMeNIrkar kmviFI (Stop Run) .
7.1. Definition of Variable:

7.2. Syntax:
Data-type VarName1, VarName2, ..., VarName n;
Ex:
int x,y;
float a,b,z;
7.3. Assignment Statement:

CakareprtMleTAeGayGefr (Variable) .

Ex1:
- int x;
x=20;
- int y=30;
Ex1:

sresr Program edIm,IKNna Y=4x

+ 2x2 + 3x + 1

#include<iostream.h>
#include<math.h>
#include<conio.h>
void main(){
clrscr();
float x, y;
cout<< Please Input x=;
cin>>x;
y=4*pow(x,5)+2*pow(x,2)+3*x+1;
261

cout<< y=<<y<<endl;
getch( );
}

VIII. Constants:
VIII.1. Definition of Constants:

CatMlEdlminPas;brkgeBlkmviFIdMeNIrkar (Process) . eKGacpas;


brtMlva)anEttamryH Source codeEtb:ueNaH.
VIII.2. Memory Constants: RtUv)aneKRbkaseday Keyword const .
Ex:

const int num=100;


const float y=20.00;

CakarbegIt Constant tamryHkareRbI # (Signal


number) nig Keyword define sMrab;kMNt;BI Pre-Processor kgkmviFI.
VIII.3. Define Constants:

Ex1:
#define PI
Ex2:

3.14

kmviFIsMrab; KNnaRklaprgVg;

#include<iostream.h>
#include<conio.h>
#define PI
3.14
void main(){
clrscr();
float radius, circle_area;
cout<< Please Input radius of Circle=;
cin>>radius;
circle_area=PI * (radius * radius);
cout<< Circle Area =<<circle_area<<endl;
getch();
}

IX. Comments and Escape Code:

CarkareFVIkMNt;sMKal;enAkgkmviFI. vaminman
dMeNIrkareTenAeBl Run Time.
enAkg C ++ man Comments BIrKW
- Comments:

// One Line
262

- /* Multiple Line */
- Escape Code:
- \n : New line
- \t : Move one tab
- \a : Beeb
- \\ : \
- \r : Move cursor to the first of line
- \ :
Ex:
// This program used to change dollars to riels
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
const riel=4000;
float dollar, result;
cout<< \n Enter Dollar you have=;
cin>>dollar;
result =dollar * riel;
cout<< After dollar to riel=<<result<<endl;
getch();
}

X. Predefined Manipulate function:

CabNaGnuKmn_mYycMnYnRtUv)aneKeRbIsMrab;eFVIkarEkERbkar bBaj nig bBalTinny.


bNaGnuKmn_TaMgenaHman
X.1. endl: Ca output manipulate function eRbIsMrab;cuHedImbnat;fI.
Ex: cout<< Area=<<area<<end<< Population=<<pop<<endl;

eRbIsMrab;bMElgRbBneKal
- hex : CaRbBneKal 16
- dec : CaRbBneKal 10
- oct : CaRbBneKal 8

X.2. hex, dec and oct:

Ex:
//Convert to hex, dec, oct;
#include<iostream.h>
263

#include<conio.h>
void main(){
clrscr();
int num;
cout<< Enter num=;
cin>>num;
cout<< Decimal=<<dec<<num<<endl;
cout<< Octal=<<oct<<num<<endl;
cout<< Hexadecimal=<<hex <<num<<endl;
getch();
}

eRbIsMrab;bMElgRbBneKaltamryH base (8, 10, 16);


EdlvasitenAkg header file <iomanip.h> .
X.3. Setbase(base):

Ex:
//Convert to hex, dec, oct;
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int num;
cout<< Enter num=;
cin>>num;
cout<< Decimal=<<setbase(10)<<num<<endl;
cout<< Octal=<<setbase(8)<<num<<endl;
cout<< Hexadecimal=<<setbase(16)<<num<<endl;
getch();
}
10.4. setw():

eRbIsMrab;kMNt;TMhMbgajmkelI Screen .

Ex:
cout<< setw(4)<< Web<<setw(5)<< Site;
Output:
9Web9Site

eRbIsMrab;bMeBjkEng Space enAeBl Output


edaynimitsBaaNamYy.
10.5. setfill():

264

Ex:
cout<<setfill(*);
cout<< setw(4)<< Web<<setw(5)<< Site;
Output:
*Web*Site
10.6. setprecision():
double

eRbIsMrab;kMNt;eRkayek,scMeBaHTinnyRbePT float or

.
Ex:
float a;
a=7/3;
cout<< a=setprecision(2)<<a<<endl;
Output:
a=2.33

XI. Operators: enAkg C ++ manRbmaNviFIepSgmYycMnYnEdleKeRbIjwkjab;dUcCa


XI.1. Arithmetic Operators:
Operators
+
*
/
%

Meaning
Addition
Subtraction
Multiplication
Division
Modulo

Example
c = a + b;
c = a b;
c = a * b;
c=a/b;
c = a % b;

Ex:
int a=20, b=40, c;
c= a+ b;
cout<< c=<<c<<endl;
XI.2. Assignment Operators:
Operators
=
+=
-=
/=
*=
%=

Meaning/Example
a = 10;
a + = 10; a = a + 10;
a - = 10; a = a 10;
a / = 10; a = a / 10;
a * = 10; a = a * 10;
a % = 10; a = a % 10;
265

>> =
<<=

n >> = 1; n = n >> 1;
n << = 1; n = n<<1;

XI.3. Comparison and Logical Operators:


Meaning

Operators
<
>
<=
>=
==
!=
&&
||
!

Less than
Greater than
Less than or equal to
Greater than or equal to
Equal to
Not equal to
Logical AND
Logical OR
Not

Ex:
- Logical AND (&&):
X
1
1
0
0

Y
1
0
1
0

X && Y
1
0
0
0

X
1
1
0
0

Y
1
0
1
0

X || Y
1
1
1
0

- Logical OR (||):

266

XI.4. Bit-wise Operators:


Meaning

Operators
&
|
^
>>
<<
~

Bit wise AND


Bit wise OR
Bit wise XOR
Bit wise Right Shift
Bit wise Left Shift
Bit wise Complement

Ex:
- Bit wise AND (&):
X
0101
0110
1111
0001

Y
0110
0010
0001
0000

X&Y
0100
0010
0001
0000

X
0101
0110
1111
0001

Y
0010
0010
0001
0000

X|Y
0111
0110
1111
0001

X
0101
0110
1111
0001

Y
0010
0011
0001
0000

X^Y
0111
0101
1110
0001

- Bit wise OR (|):

- Bit wise XOR (^):

267

- Bit wise Complement (~):


X
00001011
XI. 5. Special Operators:
- Unary Operator:

Y
11111111

~X
11110100

~Y
00000000

Ca Operator EdlRtUvkar Operant EtmYyKt;.

Ex:
++ i;
-- i;
i ++;
i--;
- Ternary Operator (?):

Ca Operator lkxN EdlmanbI Operants.

Syntax: Operant1 ? Operant2 : Operant3;


Ex:
MAX= (a>b) ? a : b;
MIN=(a>b) ? a : b;
cout<< Max=<<MAX<<endl;
cout<< Min=<<MIN;
XII. Type Conversion:

dMbUgGefrxHRtUv)anRbkasCa Integer b:uEnCYnkalvaGacmanlTPaB


TTYlyknUvcMnYnTsPaK (float) )an EdrtamTMrgxageRkam
Var1= (Data-type Var1) Var2;
Ex1:
int x;
x=(int)y;
float y;
Ex2:
int x;
y=x;
float y;
13.

GnuKmn_ Math Ed;lmanenAkg Preprocessor directive <math.h>:


268

Functions
abs(i)
sin(d)
cos(d)
tang(d)
exp(d)
log(d)
pow(x1,x2)
flour(d)
ceil(d)
fmod(d1,d2)
sqrt(d)
random(d)
14.

Description
|i|
Sin(d)
Cos(d)
Tang(d)
Exp(d)
Log(d)
Pow(d)
Flour(d)
Ceil(d)
Fmod(d1,d2)
Sqrt(d)
Random(d)

GnuKmn_EdlmanenAkg <ctype.h>:
Functions

Ex:

Result Data Type


int
double
double
double
double
double
double
double
double
double
double
void

toascii (ch)
tolower (ch)

Result Data
Type
int
char

toupper(ch)

char

#include<stdio.h>
#include<conio.h>
#include<math.h>
main( ){
clrscr( );
float a;
int result;
printf("Please, Input a=");
scanf(" %f",&a);
result =abs(a);
textcolor(6);
cprintf("Result are =%d",result);
getch( );
return 0;
269

Description
Convert ch to Ascii code
Convert ch to lower
character
Convert ch to upper
character

Exercises:

1. Write program to calculate Sum of Three number?


2. They have output on the screen as follow:
W*e*l*C*o*m*e***to***Learn***C++**Programming**Language!
Write program that output above?
3. Write program to calculate area of Rectangle?
4. Write program to calculate f(x)= axn + bxn-2 + c
5. Using Ternary operator, write program to find Min and Max of two
numbers?
6. Using setbase() function, write program to convert to hexadecimal and
octal number?
7. Write program to change Riels to Dollars?

270

Вам также может понравиться