Professional Documents
Culture Documents
Learn C++ in 9 Hours: Sanasam Ranbir Singh
Learn C++ in 9 Hours: Sanasam Ranbir Singh
1our Principles
#ncapsulation
Abstraction
0neritance
Poly"orpis"
CS201, IITG
is a better #
int myCDariableE
char myFStr1:
*nt !my*1E
int brea78
car bool8
int case8
CS201, IITG
6ariable an! Data -ypes
int myCDariableE
char myFStr1:
*nt !my*1E
int brea78
car bool8
int case8
CS201, IITG
6ariable an! Data -ypes
#har FNE char FFME )hey can also begin 3ith an underline character (F "
int brea78
car bool8
int case8
CS201, IITG
6ariable an! Data -ypes
char myFStr1: -- +nly letters5 digits and single underscore characters are
4alid.
#har FNE char FFME )hey can also begin 3ith an underline character (F "
int brea78
car bool8
int case8
*dentifier: a seBuence of one or
more letters5 digits or underscore
characters (F".
CS201, IITG
6ariable an! Data -ypes
*nitialiGation in #
int a N 0E
*nitialiGation in #
int a N 0E
*n Memory5 myName
rahul&umar
r a h u l
r a h u l
SiGe("
9egin("
end("
CS201, IITG
Operators
Assign"ent ;:<
A +: + /////Co"poun! assign"ent
(2N!5 aCN%
( flo3 of character
$Iam-le:
cout KK 4ariableE
clog KK 4ariableE
buffered
cerr KK 4ariableE
KK (left-shift o-erator"
LL (right-shift o-erator"
if and else
if (condition" statement
?um- statements
?um- statements
?um- statements
?um- statements
?um- statements
S3itch
CS201, IITG
C++ Class
9or7 2it C++ Classes an! Ob.ects
Class :!ata + functions
#lass Structure
class classNameM
accessFs-ecifierF1:
membersE
accessFs-ecifierF!:
membersE
accessFs-ecifierF%:
membersE
WW.
P ob/ectNameE
(n access s-ecifier is one of the follo3ing
three
7-ri4ate: accessible 3ithin other members
of the same class and their friends.
7-rotected: accessible 3ithin other
members of the same class5 their friends
and deri4ed classes
7-ublic: accessible from any3here the
class is 4isible
71efault access s-ecifier is -ri4ate
$Iam-le
class aRectangleM
-ri4ate:
int IE
int yE
-ublic:
4oid setDalue(int5int"E
int area("E
P myRectE
(n access s-ecifier is one of the follo3ing
three
7-ri4ate: accessible 3ithin other members
of the same class and their friends.
7-rotected: accessible 3ithin other
members of the same class5 their friends
and deri4ed classes
7-ublic: accessible from any3here the
class is 4isible
71efault access s-ecifier is -ri4ate
$Iam-le
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int5int"E
int area("E
PE
4oid aRectangle::setDalue(int a5 int b"
M INaE yNbE
P
int aRectangle::area("
M return ICyE
P
$Iam-le
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle aE
a.setDalue(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
int main("
M aRectangle a5 bE
a.setDalue(!5O"E
cout KK Sarea of a:TKKa.area("E
b.setDalue(%5O"E
cout KK Sarea of b:TKKb.area("E
return 0E
P
! O
setDalue(int5int"
area("
% O
setDalue(int5int"
area("
a b
$Iam-le
#lass aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle aE
a.setDalue(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
int main("
M aRectangle a5 bE
a.setDalue(!5O"E
cout KK Sarea of a:TKKa.area("E
b.setDalue(%5O"E
cout KK Sarea of b:TKKb.area("E
return 0E
P
! O
setDalue(int5int"
area("
% O
setDalue(int5int"
area("
#an 3e do a.IN10
data enca-sulation b
$Iam-le
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle aE
a.setDalue(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
8hat if
a.setDalue(!5O"E
is before
cout KK S area :TKKa.area("E
$Iam-le
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle aE
a.setDalue(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
8hat if
a.setDalue(!5O"E
is before
cout KK S area :TKKa.area("E
( mechanism to initialiGe
-arameters -- #onstructor
$Iam-le
class aRectangleM
int IE
int yE
-ublic:
4oid setDalue(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle aE
a.setDalue(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
class aRectangleM
int IE
int yE
-ublic:
aRectangle(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle a(!5O"E
cout KK S area :TKKa.area("E
return 0E
P
#onstructor
#onstructor
0t as
V
No return ty-e.
V
No return statement.
0t as
V
No return ty-e.
V
No return statement.
*t /ust allocates the memory s-ace and assigns the default 4alues (if
any"
7 Overloa!ing Constructors
*t /ust allocates the memory s-ace and assigns the default 4alues (if any"
7 Overloa!ing Constructors
#lass allocations
Static allocation
aRectangle a(%5X"E
aRectangle CbE
bN ne3 aRectangle(%5X"E
Copy Constructor
ReBuired 3hen
string(ShelloT"E
double a N ! 2 %.XE
string b NShelloTE
V
(bo4e $Iam-les are im-licit con4ersion
V
*t in4ol4es a tem-orary ob/ect
V
double aN ! 2%.X
7 *t is eBui4alent to
V double tem- N(double" !
V double a Ntem- 2 %.X
Conversion Constructor
#onstructor
Member initialiGer
aRectangle(": I(!"5y(O" M P
)o initialiGe references.
( non-member function or an
eIternal class can ne4er access
the -ri4ate data.
Solution: frien!
#lass aRectangleM
int IE
int yE
-ublic:
aRectangle("
M IN=E yN!E P
aRectangle(int a5int b"
M INaE yNbE
P
int area("
M return ICyE
P
PE
int main("
M aRectangle a(!5O"E
cout @@ L area@@ a'4Ba'y8
return 0E
P
1rien! 1unction
SyntaI:
SyntaI
+4erloading ('olymor-hism"
;unction o4erloading
+-erator o4erloading
SyntaI:
$Iam-le
aRectangle aE
22aE
22(22a"E
aNbE
aN(cN(b2Nd""E
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
voi! operator ++;<I ++ 48 ++y8 K
K8
int "ain;<
I "y1rien! b8
++b8 ++;++b<8
return H8
K
returnWtype operatorX;<
A non"e"ber function
returnWtype operatorX;-<
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
voi! operator ++;<I ++ 48 ++y8 K
K8
int "ain;<
I "y1rien! b8
++b8 ++;++b<8
return H8
K
returnWtype operatorX;<
A non"e"ber function
returnWtype operatorX;-<
#omment6
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
a)ectangle operator ++;<I ++ 48 ++y8 return BtisK
K8
int "ain;<
I "y1rien! b8
++;++b<8
cout @@ b'area;<8
return H8
K
+ut-ut6
!1 or %!
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
K8
voi! operator ++;a)ectangle b<
I ++ b'48 ++b'y8 K
int "ain;<
I "y1rien! b8
++b8
return H8
K
returnWtype operatorX;<
A non"e"ber function
returnWtype operatorX;-<
#omment6
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
K8
voi! operator ++;a)ectangle b<
I ++ b'48 ++b'y8 K
int "ain;<
I "y1rien! b8
++b8
return H8
K
returnWtype operatorX;<
A non"e"ber function
returnWtype operatorX;-<
+4erloading
should be a
friend function
#4a"ple% unary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
K8
voi! operator ++;a)ectangle b<
I ++ b'48 ++b'y8 K
int "ain;<
I "y1rien! b8
++b8
return H8
K
returnWtype operatorX;<
A non"e"ber function
returnWtype operatorX;-<
Self Study:
-ostfiI increament
i.e.5 b22
#4a"ple% binary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
voi! operator +;a)ectangle b<
I 4:4+b'48 y:y+b'y8 K
K8
int "ain;<
I "y1rien! b(a8
a:a+b8
return H8
K
returnWtype operatorX;Q<
A non"e"ber function
returnWtype operatorX;Q(-<
#4a"ple% binary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
voi! operator +;a)ectangle b<I 4:4+b'48 y:y+b'y8 K
K8
int "ain;<
I "y1rien! b(a8
a:a+b8
return H8
K
#omments6
#4a"ple% binary
class a)ectangleI
int 48
int y8
public%
a)ectangle;< I 4:J8 y:+8 K
a)ectangle;int a(int b< I 4:a8 y:b8 K
Sa)ectangle;<I K
int area;< I return 4By8 K
a)ectangle operator +;a)ectangle b<
I 4:4+b'48 y:y+b'y8
return Btis8
K
K8
int "ain;<
I "y1rien! b(a8
a:a+b8
return H8
K
Return
Self Stu!y%
Overloa!ing for oter operators
Class 0neritance
Dehicle
#ar 9us
SuGu&i
)ata
Class 0neritance
class ( M
-ri4ate:
int (FIE
-rotected:
int (FyE
-ublic:
4oid (Ff1("
PE
class 9 : -ublic ( M
-ri4ate:
int 9FIE
-rotected:
int 9FyE
-ublic:
4oid 9Ff1("E
PE
9ase #lass
1eri4e #lass
Class 0neritance
class ( M
-ri4ate:
int (FIE
-rotected:
int (FyE
-ublic:
4oid (Ff1("
PE
class 9 : -ublic ( M
-ri4ate:
int 9FIE
-rotected:
int 9FyE
-ublic:
4oid 9Ff1("E
PE
B is Pa 7in! of aP A
B is P!erive! fro"P A
B is Pa specialize!P A
B is a PsubclassP of A
B is a P!erive! classP of A
A is te Pbase classP of B
A is te PsuperclassP of B
0neritance is referre! to as
1eri4e classes inherites the
-rotected and -ublic members of
base class
Class 0neritance
class ( M
-ri4ate:
int (FIE
-rotected:
int (FyE
-ublic:
4oid (Ff1("
PE
class 9 : -ublic ( M
-ri4ate:
int 9FIE
-rotected:
int 9FyE
-ublic:
4oid 9Ff1("E
PE
public%
private%
protecte!%
frien!s
9at can not be inerite!
9en 2e create an ob.ect of !erive! class
Te2 frien!s
9at can be a!!e! on !erive!
classes
Class 0neritance
class ( M
-ri4ate:
int (FIE
-rotected:
int (FyE
-ublic:
4oid (Ff1("
PE
class 9 : -ublic ( M
-ri4ate:
int 9FIE
-rotected:
int 9FyE
-ublic:
4oid 9Ff1("E
PE
(bstract class
tem-late Kclass )L
class aRectangleMP
tem-late Kclass )L
) add() a5 ) b"
M return a2bEP
Class -e"plate
SyntaI:
)hree ty-es
dataty-e
integral or enumeration
-ointer to member
*t is a generaliGed format.
Usage:
;or eIam-le:
te"plate@class -? class Q % A@-?
I
-%%B 48
voi! f;A@-?D y<
I
By++8
K
K8
te typena"e -%%B
te variable y'
Depen!ent Ta"e
;or eIam-le:
te"plate@class -? class Q % A@-?
I
-%%B 48 **//;a"biguous<///
voi! f;A@-?D y<
I
By++8
K
K8
-e co"piler bin!s !epen!ent na"es 2en a te"plate is instantiate!'
-e co"piler bin!s non/!epen!ent na"es 2en a te"plate is !efine!'
A ,ualifie! !epen!ent na"e suc as
-%%4 can be%
B A nontype ;ob.ect or function<
B A type
B A te"plate
By !efault( a ,ualifie! !epen!ent
na"e is a nontype
Depen!ent Ta"e
;or eIam-le:
te"plate@class -? class Q % A@-?
I
typena"e -%%B 48
voi! f;A@-?D y<
I
By++8
K
K8
-e co"piler bin!s !epen!ent na"es 2en a te"plate is instantiate!'
-e co"piler bin!s non/!epen!ent na"es 2en a te"plate is !efine!'
A ,ualifie! !epen!ent na"e suc as
-%%4 can be%
B A nontype ;ob.ect or function<
B A type
B A te"plate
By !efault( a ,ualifie! !epen!ent
na"e is a nontype
1unction -e"plates
SyntaI:
Non--ointer constants
String literals
-e"plate -e"plate Para"eters
o-erators
destructors
*m-licit
$I-licit
dynamic
Static cast
reinter-retFcast
constFcast
-ype cast
ty-eid
closing a file
myfile.close("E
tellg;<
tellp;<
see7g;<
see7g;<
rea!;<
2rite;<
2rite to a file
)eferences
)he #22 'rogramming Language5 by 9. Stroustru-
'rogramming -- 'rinci-les and 'ractice Using #225
by 9. Stroustru-
)he 1esign and $4olution of #225 by 9. Stroustru-
1ata (bstraction and -roblem sol4ing 3ith #225 by
;ran& M. #arrano
#22 tem-lates
9y 1a4id Dande4oorde5 Nicolai M. ?osuttis
*9M(R" im-lementation of the # and #22
-rogramming languages (htt-:..-ublib.boulder.ibm.com. (htt-:..-ublib.boulder.ibm.com.
infocenter.com-hel-.44101.indeI./s-" infocenter.com-hel-.44101.indeI./s-"
)eferences