c++_classi
TRANSCRIPT
-
7/31/2019 C++_ClassI
1/48
-
7/31/2019 C++_ClassI
2/48
Fortran Algol 68 C C++ C#
Cobol
Eiffel
JavaAda 95PL/I Pascal
ElementaryProcedural
AdvancedProcedural
SpecialProcedural
ObjectOriented
AdvancedObject
Oriented
Ada 83
-
7/31/2019 C++_ClassI
3/48
WhatisProgramming?
Take
Aproblem
Asetofdata
Asetoffunctions
Apply to tosolvethe
-
7/31/2019 C++_ClassI
4/48
C++isanobject-orientedprogramming
(OOP)languagethatisviewedbymany
asthebestlanguageforcreatinglarge-
scaleapplications.
C++isamultiparadigmlanguage.
C++isanobjectdrivenlanguage.
-
7/31/2019 C++_ClassI
5/48
C++(pronouncedseeplusplus)was
developedbyBjarneStroustrupatBell
Labsin1979.
CombinationofSimula67andc
CWithclasses
In1983,thenamewaschangedtoC++.
C++isanextendedversionofC. C++isasupersetofC.
AlltheconceptsofCareapplicabletoC++
also.
-
7/31/2019 C++_ClassI
6/48
Differenceb/wCandC++
C
CProcedureOriented
Cdoesnothaveanyclassesorobjects.
Cinput/outputisbasedonlibraryandthe
processesarecarriedoutbyincludingfunctions.
C++
ObjectOriented
ItsupportsClassesandObjects
C++i/oismadethrough
consolecommandscinandcout.
-
7/31/2019 C++_ClassI
7/48
Differenceb/wCandC++
Cfunctionsdonot
supportoverloading.
Cdoesnotsupportnewordeletecommands.
Chasatopdown
approach.
Chaspredefineddata
types.
C++supportsOverloading.
Newkeywordisusedtocreateanobjectforaclass.
C++hasabottomup
approach.
In c++, User can createsits own datatype usingclass.
-
7/31/2019 C++_ClassI
8/48
Variablesarelikesmallblackboards
Wecanwriteanumberonthem
Wecanchangethenumber
Wecanerasethenumber
C++variablesarenamesfor
memorylocations
Wecanwriteavalueinthem
Wecanchangethevaluestoredthere
Wecannoterasethememorylocation
Somevalueisalwaysthere
-
7/31/2019 C++_ClassI
9/48
Variables names are called identifiers Choosing variable names
Use meaningful names that represent
data to be stored First character must be
a letter
the underscore character
Remaining characters must be letters
numbers
underscore character
-
7/31/2019 C++_ClassI
10/48
Keywords(alsocalledreserved
words)
AreusedbytheC++language
Mustbeusedastheyaredefinedin
theprogramminglanguage
Cannotbeusedasidentifiers
Keywordisanessentialpartofa
languagedefinition.
-
7/31/2019 C++_ClassI
11/48
-
7/31/2019 C++_ClassI
12/48
C constants can be divided into twomajor categories
Primary Constants
Secondary ConstantsC Constants
Primary Constants Secondary constants
Integer Constant
Real Constant
Character Constant
Array, Pointer
Structure, Union
enum
-
7/31/2019 C++_ClassI
13/48
Anintegerconstantmusthaveatleastone
digit
Itmustnothaveadecimalpoint
Itcouldbeeitherpositiveornegative
Ifnosignprecedesanintegerconstant,itis
assumedtobepositive
ex.,123,-321,+78 Nocommasorblanksareallowedwithinan
integerconstant
-
7/31/2019 C++_ClassI
14/48
Realconstants(RC)musthaveatleastonedigit
Itmusthaveadecimalpoint
Itcouldbeeitherpositiveornegative
Defaultsignispositive
NocommasorblankareallowedwithinaRC
Ex.,0.0083,-0.75,+247.0
-
7/31/2019 C++_ClassI
15/48
ASinglecharacterconstantiseithera
singlealphabet,asingledigitora
singlespecialsymbolenclosedwithinsinglequotes
Themaximumlengthofacharacter
constantcanbe1character Ega,1,5,=(Valid)
asd,12(Invalid)
-
7/31/2019 C++_ClassI
16/48
Itisasequenceofcharacters
enclosedindoublequotes
Charactersmaybeletters,numbers,
specialcharactersenclosedindouble
quotes.
Eg.,Hello!1987?...!X
-
7/31/2019 C++_ClassI
17/48
ThemostcommonlyusedDataTypesinC++
programminglanguage:
shortint
int longint
float
double
longdouble char
bool
-
7/31/2019 C++_ClassI
18/48
Beforeuse,variablesmustbedeclared
Declarationsyntax:
,
, . . . ;
Tellsthecompilerthetypeof
datatostore
-
7/31/2019 C++_ClassI
19/48
DeclaringVariables
Examples:intnumber_of_students;doubleweight;
isanabbreviationforinteger.
couldstore3,102,3211,-456,etc. number_of_studentsisof integer
representsnumberswithafractionalcomponent
couldstore1.34,4.0,-345.6,etc. Weightisof double
-
7/31/2019 C++_ClassI
20/48
Declaringavariabledoesnotgiveitavalue Givingavariableavalueinthe
declarationis thevariable Eg:int age = 5;
Givingavaluetothedeclaredvariableis thevariable Eg:int age;
age = 5;
-
7/31/2019 C++_ClassI
21/48
Include files
Function subprogram
Main function
Global variable and
function declaration
-
7/31/2019 C++_ClassI
22/48
/* A first C++ Program*/
#include
int main()
{
cout
-
7/31/2019 C++_ClassI
23/48
Editor DiskPhase 1
Program edited inEditor and stored
on disk
Preprocessor DiskPhase 2
Preprocessor
program processesthe code
CompilerDisk
Phase 3
Creates object code
and stores on disk
Linker DiskPhase 4
Links object code
with libraries and
stores on disk
-
7/31/2019 C++_ClassI
24/48
LoaderPhase 5
Puts program in
memory
Primary memory
CPUPhase 6
Takes each instruction
and executes it storingnew data values
Primary memory
-
7/31/2019 C++_ClassI
25/48
Input and Output
A isasequenceofdata
Typicallyintheformofcharactersornumbers
Aninputstreamisdatafortheprogramtouse Typicallyoriginates
atthekeyboard
atafile
Anoutputstreamistheprogramsoutput
Destinationistypically
themonitor
afile
-
7/31/2019 C++_ClassI
26/48
cout is an output stream sending data to the monitor The insertion operator"
-
7/31/2019 C++_ClassI
27/48
cinisaninputstreambringingdatafromthe
keyboard
Theextractionoperator(>>)getsdatatobeused
Example: cout
-
7/31/2019 C++_ClassI
28/48
Multipledataitemsareseparatedbyspaces
Dataisnotreaduntiltheenterkeyis
pressed Example: cin >> v1 >> v2 >> v3;
Requiresthreespaceseparatedvalues
Usermighttype344512
-
7/31/2019 C++_ClassI
29/48
Prompttheuserforinputthatisdesired
coutstatementsprovideinstructions
cout > age;
Displayingtheoutputwhatwasread
cout
-
7/31/2019 C++_ClassI
30/48
Commentshouldbeenclosedbetween
SingleLineCommentis
Itisusedtoincreasethereadabilityoftheprogram.
Anynumberofcommentscanbegivenatanyplaceinthe
program.
Commentcannotbenested.
Itcanbesplitovermorethanoneline
-
7/31/2019 C++_ClassI
31/48
\n newline
\t tab
\r carriagereturn
\a alert
\\ backslash
\ doublequote \? QuestionMark
\ QuotationMark
\0 Null
-
7/31/2019 C++_ClassI
32/48
An operator is asymbol(Eg:+,-,*,/)that directs the computer to performcertain mathematical or logical
manipulations and is usually used tomanipulate data and variables.
Eg: a + b
-
7/31/2019 C++_ClassI
33/48
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
-
7/31/2019 C++_ClassI
34/48
C++ operation Algebraic C++
Addition(+) f+7 f+7
Subtraction (-) p-c p-c
Multiplication(*) bm b*m
Division(/) x/y, x , x y x/y
Modulus(%) r mod s r%s
-
7/31/2019 C++_ClassI
35/48
Highest to lowest
()
*, /, %
+, -
-
7/31/2019 C++_ClassI
36/48
a(b+c)+c(d+e)
C:
a * ( b + c ) + c * ( d + e ) ;
Precedence:
3 1 5 4 2
-
7/31/2019 C++_ClassI
37/48
/* Program using Arithmetic operator */
#include
#include
void main(){
int a=5,b=6; // Initializing variables
int c;
clrscr(); // To clear the screen
c=a+b; // + operator
cout
-
7/31/2019 C++_ClassI
38/48
Operator Meaning
< Is less than
Is greater than
>= Is greater than or equal to
== Equal to
!= Not equal to
-
7/31/2019 C++_ClassI
39/48
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Logical expressions are a compound relationalexpressions
-An expression that combines two ormore relational expressions
Eg: if (a==b && b==c)
-
7/31/2019 C++_ClassI
40/48
Syntax:
v op = exp;
Where v = variable,
op = shorthand assignment operator
exp = expression
Ex:x=x+3
x+=3
-
7/31/2019 C++_ClassI
41/48
Simple assignment operator Shorthand operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
-
7/31/2019 C++_ClassI
42/48
1. Increment ++
2. Decrement --
The ++ operator adds a value 1 to the operand
The operatorsubtracts 1 from the operand
++a / --a => pre increment / decrement
a++ / a-- => post increment / decrement
-
7/31/2019 C++_ClassI
43/48
Let the value of a =5 and b=++a then
a = b =6
Let the value of a = 5 and b=a++ then
a =5 but b=6
i.e.:
1. a prefix operator first adds 1 to the operand and thenthe result is assignedto the variable on the left
2. a postfix operator first assigns the value to thevariable on left and thenincrements the operand.
-
7/31/2019 C++_ClassI
44/48
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
Working of the ? : Operator:Exp1 is evaluated first, if it is nonzero(1/true) then theexpression2 is evaluated and this becomes the value of theexpression,
If exp1 is false(0/zero) exp3 is evaluated and its value
becomes the value of the expression
Eg: m=2;
n=3;
r=(m>n) ? m : n; // Here r = n
-
7/31/2019 C++_ClassI
45/48
/* Program using Conditional operator */
#include
#include
void main(){
int a,b,c; // Declaring variables
a=5; // Assigning values
b=6;
clrscr(); // To clear the screen
c=(a>b)?a:b; // Conditional operator
cout
-
7/31/2019 C++_ClassI
46/48
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
> Shift right
-
7/31/2019 C++_ClassI
47/48
/* Program using bitwise operators */
#include
#include
void main()
{
clrscr();
int a=5,b=6; // a = 0101 , b = 0110int c;
c=a&b;
cout
-
7/31/2019 C++_ClassI
48/48
c=a|b;
cout