Top Banner

of 156

C++ NOTES COMPLET

Feb 14, 2018

Download

Documents

avinashavi93
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/23/2019 C++ NOTES COMPLET

    1/156

    [C++ Notes]

    --

    www.songmp9.com

  • 7/23/2019 C++ NOTES COMPLET

    2/156

    C C++

    #include #include

    Function >a >>b

    &ascading of etractor

    for net line/rintf ( Hello 0n user, cout

  • 7/23/2019 C++ NOTES COMPLET

    3/156

    HISTORY OF C++

    3ear 4956

    7eeloped 82 8jarne stroustrap

    ab 8ell abs

    &ompan2 :t + ;

    & is procedure oriented language & is object oriented language

    -as2 + fast programming in &'' closel2 models rear world

    &. ogics can be easil2 deeloped problem soling approach.

    CLASSES AND OBJECTS

    !n structure of & = onl2 data can be member of structure and not functions

    :ll member of structure of are public b2 default

    !n class of &'' data ' functions accessing those data are member of class and :ll

    member of class are priate b2 default

    class stud

    int roll

    char gradefloat par

    public

    oid get( ,

    oid show( ,

    ?

    oid stud get( ,

  • 7/23/2019 C++ NOTES COMPLET

    4/156

    cout >roll>> grade >> per?

    oid stud show

    cout

  • 7/23/2019 C++ NOTES COMPLET

    5/156

    cin >> a >>b

    ?

    oid add sum( ,

    c$ a'b

    ?

    oid add show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    6/156

    -ncapsulation means building or wrapping up of data members and fnacting on

    those data members with in a single unit. Since a class allows us to hold data +

    functions within it are sa2 that it supports the principle of encapsulation.

    POLYMORPHISM

    ;he word pol2morphism is deried from combination of two words

    pol2 meaning multiple and morph means the abilit2 to hae multiple forms. !n

    other words if an entit2 can acGuire multiple forms in different situation we sa2

    that its behaiors is pol2morphic for eg in c'' it is possible for programmer to

    redife operator ' in such a wa2 that it can be used to add two integers as well as

    at the same time it can add two abject or two strings. So it a programmer define '

    to behae in aboe mentioned member we sa2 that ' behaes pol2morphicall2 .

    !n &''* pol2morphism is implemented in two wa2=

    (1). Compile ime pol!mo"p#i$m= function oerloading and operator

    "erloading

    (%). R&'Time pol!mo"p#i$m= Iirtual functions* pure irtual functions

    :bstract classes

    (iii). I'#e"i'e= to inherit means to acGuire properties and features of an

    eisting entit2 into a newl2 created entit2 iEe a

    child acGuires properties of his or her parents* similar2 when designing

    software if a programmer wishes then he can acGuire the data member andmember function of an eisting class in his own class with the help of

    inheritance. ;he class which gets inherited is Enown as base class and the

    class which inherits is Enown as deried class. ;hus b2 inheriting the

    members of base class it becomes possible to access them through the

    objects of derie class. ;he major adantage offered b2 principle of

    !nheritance is reusabilit2 and reliabilit2

    CREATIN* PARAMETERIED FUNCTIONS ,ITHIN CLASSES

    class -mp

    int age

    char nameJ6KL

    float salar2

    public

  • 7/23/2019 C++ NOTES COMPLET

    7/156

    oid set (int * char M* float,

    oid show( ,

    ?oid -mp set (int !* char Mj* float N,

    age $!

    strcp2(name* j,

    salar2 $E

    ?

    oid -mp show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    8/156

    oid Fact init( ,

    f$4?

    oid Fact getno( ,

    cout > n

    ?

    oid Fact calculate( ,

    int i

    for (i$4 i

  • 7/23/2019 C++ NOTES COMPLET

    9/156

    B. ;he2 are automaticall2 called as soon as the object of class is created i.e. their

    calling is implicit.

    C. ;he2 cant be declared as staticQ. ;he2 cant be declared as irtual

    :n2 class which does not contain an2 constructor then compiler from itself

    supplier a constructor but it is hidden. for programmer these constructors are

    default

    class fact

    ?&onstructors are automaticall2 called een when not declared* at that time

    default constructors are called. 7efault contructors are destro2ed as soon as we

    declare constructor

    Emple 23

    class fact

    int n

    long int fpublic

    fact( ,

    f$4

    ?

    oid getno( ,

    oid calculate( ,

    oid displa2( ,

    ?

    oid fact getno( ,

    cout > n

    ?

    oid fact calculate( ,

  • 7/23/2019 C++ NOTES COMPLET

    10/156

    int i

    for (i$4 i

  • 7/23/2019 C++ NOTES COMPLET

    11/156

    ?

    oid main( ,

    -mp e(4K4*:mit*RKKK.KK,

    e.show( ,

    getch( ,

    ?

    FUNCTION O4ERLOADIN*oid show (int* int* int,

    4. %o. of :rguments oid show (int,

    6. ;2pe of :rgument oid show (double,B. "rder of argument oid show (int* double,

    oid show (double* int,

    int show ( ,

    %ot allowed (return t2pe differs,

    oid show( ,

    &ompiler does not consider return t2pe* because it is not compulsor2 to receie the

    alue returned b2 the function. ;hus compiler will not be able to differentiate for

    which function the call is being made. Function oerloading allows same function

    name in same scope but there should be some difference in functions.

    oid ol (int,

    oid ol (int* int* int ,

    oid main ( ,

    int choicecout choice

    switch (choice,

    case 4

  • 7/23/2019 C++ NOTES COMPLET

    12/156

    int s

    cout > sol (s,

    breaE

    case 6

    int l* b* h

    cout > l >> b >> h

    ol (l*b* h,

    breaE

    default

    cout

  • 7/23/2019 C++ NOTES COMPLET

    13/156

    8o(int*int*int, OOconstructor

    oid show( ,

    ?8o 8o( ,

    cout > l >> b>> h

    ?

    8o 8o(int s,

    l$b$h$s

    ?

    8o 8o (int i* int j* int E,

    l$i

    b$j

    h$E

    ?

    oid 8o show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    14/156

    4. when eer programmer creates an object and at the same time passes another

    object of the same class as a parameter

    6. wheneer a fn

    accepts an object as a parameter b2 alue.B. wheneer a f nreturns an object b2 alue.

    Re5e"e'e /"i6le23

    S2nta= + $

    oid main( , 7rawbacEs of pointer

    int a $ 4K 4. will be initialiPed b2 garbageint Mp 6. necessar2 to initialiPe before their use

    p$+a

    cout

  • 7/23/2019 C++ NOTES COMPLET

    15/156

    B. !n case of arra2* alwa2s use pointer. Teference ariable can not worE with

    arra2

    C. we can not maEe arra2 of reference ariable

    int +p JCL

    &O&'' &O&'' onl2 in &''

    p$$ 6! /l&e p$$ 6! 00"e$$ p$$ 6! "e5e"e'e

    oid swap (int* int, oid swap (intM* intM, oid swap (int+*int+,oid main( , oid main( , oid main( ,

    int a*b int a*b int a*b

    cout a>>b cin >> a >> b

    swap (a*b, swap (+a* +b, cout

  • 7/23/2019 C++ NOTES COMPLET

    16/156

    G$temp

    ?

    A. D:/ to use a function call maimum which accepts an integer arra2 of siPe

    pure as an argument + return the largest + smallest element of that arra2 to main.

    Dithout changing the original position of element of arra2.

    oid Uaimum (int aJL* int +* int+,oid main( ,

    int aJQL* i* large* small

    for (i$K iaJiL

    ?

    Uaimum (a* large* small,cout

  • 7/23/2019 C++ NOTES COMPLET

    17/156

    class 8o

    int l* b* hpublic

    8o( ,

    8o (int,

    8o (int* int* int,

    8o (8o +,

    oid show( ,

    ?

    8o 8o( ,

    cout > l >> b >> h

    ?

    8o 8o (int S,

    l$b$h$s

    ?

    8o 8o (8o +p,

    l$p.l b$p.b

    h$p.h

    ?

    8o 8o (int i* int j* int E,

    l$i

    b$j

    h$E

    ?

    oid 8o show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    18/156

    8o 86 (4K,

    8o 86 (Q**44,

    8o 8C (84,84. show( ,

    86.show( ,

    8B.show( ,

    8C.show( ,

    ?

    8o 86 (4K, 8o 86 $ 4K

    8o 8C (84, 8o 8C $ 8

    &all for cop2 constructor

    8o 8C

    8C $ 84

    %o call for cop2 constructor use of assignment operator other

    destination object is alread2 made

    DEFUALT AR*UMENTS

    oid printline (char $M* int$4,oid main ( ,

    printline ( , OO printline (M* 4,

    printline (#, OO printline ( #* 4,

    printline (V* 4K, OO printline (V* 4K,

    ?

    oid printline (char ch* int n,

    for (int i$4 i

  • 7/23/2019 C++ NOTES COMPLET

    19/156

    class Stud

    int agechar grade

    float per

    public

    Stud (int* char* float, OO stud (int$o* char$ float$K.K,

    oid get( ,

    oid show( ,

    ?

    Stud Stud (int !* char j* float E,

    age $!grade$ j

    per $E

    ?

    oid Stud get( ,

    cout > age >> grade >> per

    ?

    oid Stud show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    20/156

    DESTRUCTOR

    :re special member fn

    of class which hae same name as that of the class butpreceded with the s2mbol of tilde (W,. ;he2 are automaticall2 called wheneer an

    object goes out of scope* that is *it is about to be destro2ed

    Dhen an object is created first function which is automaticall2 called is

    constructor + when object ends its life the last function which is called is

    destructor which can be used to deallocate the resource occupied b2 the object.

    class -mp

    int age

    char name J6KL

    float sal

    public

    -mp( ,

    W-mp( ,

    oid show( ,

    ?

    -mp -mp( ,

    cout > age >> name >> sal

    ?

    oid -mp show( , Noe

    : class b2 default has B built in

    cout

  • 7/23/2019 C++ NOTES COMPLET

    21/156

    f. show( ,

    ?

    &reate a class cOo student haing B data members (i, for storing Toll no. (ii, for

    storing name (iii, for storing marEs in subjects

    ;he member for storing name should be char M* member for storing marEs should

    be int M

    &reate constructor of class which prompts user to enter length of name*

    allocates sufficient memor2 + accepts the name from user. ;he same constructor

    asEs the user how man2 subject he want to enter* againallocate sufficient memor2

    for that + accepts the marEs gien b2 user. Finall2 proide appropriate member f nwhich displa2 ) + grade of student. :t the end define the destructor in proper

    manner to deallocate memor2 allocated b2 constructor.

    class student

    int roll *n

    char M name* grade

    int MmarEs

    float perpublic

    student ( ,

    oid get(,

    oid calculate( ,

    oid show( ,

    Wstudent( ,

    ?

    student student ( ,

    cout >n

    name $ (char M, malloc ((n'4, M siPeof (char,,

    if (name$ $ %1,

    eit(4,

    get( ,

  • 7/23/2019 C++ NOTES COMPLET

    22/156

    ?

    oid student get( ,

    cout > roll

    cout > name

    cout >n

    marEs$(intM,malloc(nM6,

    for (i$K i M (marEs'i,

    ?

    ?

    oid student calculate ( ,

    per$K

    for (int i$K i$Q,

    grade $ :

    else if (per >$RK,

    grade $ 8

    else

    grade $ f

    ?

    student W student ( ,

    free (name,

    free(marEs,

    ?

    oid student show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    23/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    24/156

    4. are special member fnof class haing same name as that of the class but

    preceded with the s2mbol of tilde6. a destructor is also automaticall2 called but wheneer an object is about to be

    destructor or goes out of scope thus these calling is implicit.

    B. we can not pass parameters to a destructor.

    C. as the2 dont accept parameters we can not oerload then* thus a class can not

    hae multiple destructor

    Q. calling of destructor is alwa2s done in reerse or des of the creation of objects.

    R. inheriting of destructor is also not possible

    . we can hae irtual destructor in a class.

    INLINE FUCNTIONS

    I'li'e 5&'io'$

    :re those fnwhose call is replaced b2 their bod2 during

    compilation. 7eclaring a fnas inline has two major adantages.

    4. compiler does not has to leae the calling fnas it finds definition of fnbeing

    called their it self.

    6. ;he oerhead of maintaing the stacE in belureen function call is seduced;hus declaring a function as inline increases the eecution

    Speed* s0reuces eecution time + thus enhances the oerall efficienc2 of program.

    8ut two ptr must be considered before declaring a fnas inline

    4. ;he definition of inline function must appear before its call i.e. if a non

    member fn is to be made inline then its declaration + definition must appear

    about main as a single unit.

    6. the bod2 of inline fnmust be short + small

    B. it should not contain an2 comple Ee2words or Ee2word or statement liEe for*

    if* while* dowhile do*

    if an2 of the aboul sules are iolated then compiler ignores the

    Ee2word inline an treats the fnas offline or normal one. Uoreoer a

    class can hae two Einds of inline functions

  • 7/23/2019 C++ NOTES COMPLET

    25/156

    1.Implii I'li'e= fnefined within the bod2 of class

    %. Eplii I'li'e= fndeclared within the class but defined outside the class

    preceded with the Ee2word inline.

    ;hus at the end we can sa2* that declaring a fnas line is a reGuest made

    b2 programmer which the later ma2 agrel or den2.

    class -mp

    char name J6KL

    int age

    float sal

    public oid get( ,

    Implii

    I'li'e cout > age >> name >> sal

    ?

    oid show( ,

    ?

    Eplii inline oid -mp show( ,I'li'e

    cout

  • 7/23/2019 C++ NOTES COMPLET

    26/156

    So"-e l$$ 0ei0e$ #e 5olloi'-

    4. 7efault alue6. life (persistence,

    B. scope (accessibilit2,

    $o"-e 0e5&l li5e $ope

    4.auto garbage limited to limited to declaration blocE

    (automatic, their

    7eclaration

    8locE

    6. static Xero throughout

    ;he programB. register garbage same as auto same as auto

    C. global Pero throughout throughout the program

    ;he program

    A&o Si

    oid displa2 ( , oid displa2 ( ,

    oid main( , oid main ( ,

    displa2( , displa2( ,

    displa2 ( , displa2 ( ,

    displa2 ( , displa2( ,

    ? ?

    oid displa2( , oid displa2( ,

    int a

    cout

  • 7/23/2019 C++ NOTES COMPLET

    27/156

    STATIC DATA

    Static data members with in the class

    class data

    int a

    Static int b

    ? K b

    a a

    int data b

    d4 76

    static ariable dont wait for creation of object* before object creation memor2 is

    allocated for then

    of class

    4. : static data member has a single cop2 shared amongst each object of that

    class. "n other hand if a class has non static data then eer2 object of thatclass has its own cop2 of non static data

    6. static data members arrie in memor2 een before objects of the class get

    created. 8ecause of their feature it becomes necessar2 to redefine them outside

    the class so that the2 can be gien space in T:U without the help of object.

    B. since the2 are not related to an2 particular object of the class + hae a single

    cop2 in the entire class* a static data member neer contributes in the siPe of

    object. !n other words siPe of object is alwa2s calculated b2 summing up siPes

    of non static members.

    D:/ to create a class cOo student haing two data members roll + count. ;he

    member roll should Eeep the roll no. allocated to eer2 student object while themembers count should Eeep tracE of total no. of student objects currentl2 in the

    memor2. Finall2 proide appropriate members fn to initialiPe + displa2 the alues

    of Toll no. + count.

    class Student

  • 7/23/2019 C++ NOTES COMPLET

    28/156

    int roll

    static int count

    publicStudent (int i,

    roll$i

    '' count

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    29/156

    (list of arguments,

    RESTRICTIONS ON STATIC FUNCTIONS

    4. static fncan neer access non Zstatic data numbers. 8ut reerse is true.

    6. &onstructor + 7estructor can neer be made or declared as static.

    B. !f a static function is defined outside the class the Ee2word static cannot be

    used.

    class student

    int roll

    static int count

    public

    student (int i,

    roll $i

    ''count

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    30/156

    student S(4K,. /(6K,* A(BK,

    S.show(,

    /.show(,A.show(,

    student total[student( ,

    student Y(CK,* 3(QK,

    Y. show( ,

    3.show(,

    studenttotal[student(,

    ?

    studenttotal[student(,

    ?

    studenttotal[student(,

    getch( ,

    ?

    P"o-"m= &reate a class cOa emplo2a haing data members for storing age* name

    + id. :lso proide another member which stores the net id which will be

    allocated to net incoming object. /roide a member fn

    to initialiPe this ariablewith its initial alue + also proide appropriate constructor + member f nfor

    initialiPed all other members of class.

    class -mp

    int age

    char name J6KL

    int id

    static int nid

    publicstatic oid init( ,

    id$nid

    ?

    -mp( ,

  • 7/23/2019 C++ NOTES COMPLET

    31/156

    cout > age >> name

    id$nid''nid

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    32/156

    oid -mplo2ee show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    33/156

    float sal

    public

    oid get( ,

    cout > age>> name>>sal

    cout

  • 7/23/2019 C++ NOTES COMPLET

    34/156

    82 default class has B this pointer.

    &onstructor cop2 constructor 7estructor

    Ae$$i'- D Mem6e" U$i'- ;#i$:

    class 8o

    int l* b* h

    public

    8o( ,

    8o (int* int* int ,

    8o (8o +,

    oid show( ,?

    8o 8o( ,

    cout > l >> b >> h

    ?

    8o 8o (int !* int j* int E,

    l$i

  • 7/23/2019 C++ NOTES COMPLET

    35/156

    ?

    oid 8o show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    36/156

    6. pointer to const

    B. const pointer

    C. const pointer to const ariableQ. const parameters

    R. data members of class

    . member f nof a class

    9o'$: 4"i6le$

    oid main ( ,

    const float pi$B.4C

    ?

    &onst ariable are initialiPed at the pt of declaration menns the2 are readonl2 ariables + their alues can not be manipulated.

    cout

  • 7/23/2019 C++ NOTES COMPLET

    37/156

    int b$QK

    int a$4K

    int M const p$+aMp$6K

    p$+b

    9o'$: p"mee"2

    Dhen the constmodifier is used with a pointer parameter in a function\s

    parameter list* the function cannot modif2 the ariable that the pointer

    points to.

    For eample*

    int strlen(const char Mp,

    Here the strlen function is preented from modif2ing the string that p points to.

    DorEing on the same theme the protot2pe of a cop2 constructor is actuall2

    ( +,

    For eample

    8o (const 8o +p, OO protot2pe of default cop2 constructor

    $p.l

    8$p.b

    H$p.h

    ?

    Co'$ 0 mem6e"$ o5 l$$

    Uan2 times we hae some data members in a class whose alues should remain

    unaltered after the2 are initialiPed. ;o create such members we can use the constEe2word in front of their declaration and such members are called const

    members. 8ut the2 must be initialiPed using initialiPers.

    For eample

    class circle

  • 7/23/2019 C++ NOTES COMPLET

    38/156

    int rad

    const float pi OO ;his is inalid?

    Tather the class should hae a constructor to initialiPe pi using initialiPers.

    class circle

    int rad

    const float pi OO ;his is inalid

    public

    circle (int r, pie (B.4C,

    rad$r

    ?

    ?

    Co'$ Mem6e" F&'io'23

    if we want that a f

    n

    that should not be allowed tochange alue of classmember than we maEe them const.

    class circle

    int r

    float a

    public

    oid get (int ,

    r$?

    oid cal[area ( ,

    a$r M r M B.4C

    ?

    oid show ( , const

  • 7/23/2019 C++ NOTES COMPLET

    39/156

    count

  • 7/23/2019 C++ NOTES COMPLET

    40/156

    3$65

    PASSIN* OBEJCTS USIN* POINTER

    class 8o

    int l* b* h

    public

    oid get( ,

    OO same as preious

    ?

    int compare (8o M,?

    int 8o compare (8o Mp,

    int * 2

    $ lMbMh

    2$/lM pbM ph

    if ($ $ 2,

    return (K,

    if (>2,return (4,

    else

    return (=4,

    ?

    oid main( ,

    8o 84* 86

    84. get( ,

    86. get( ,

    84. show( ,86. show( ,

    int ans

    ans $ 84. compare (+86,

    if (ans$ $K,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    41/156

    else if (ans$$4,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    42/156

    84.get( ,

    86.get( ,

    84.show( ,86.show( ,

    int ans

    ans$84. compare (86,

    if (ans$ $K,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    43/156

    Emple23OO 7emonstrating the use of friend function

    class student

    int roll

    char grade

    float per

    public

    oid get( ,

    friend oid show (student,

    ?

    oid student get( ,

    cout > roll>> grade>>per

    ?

    oid show (student /,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    44/156

    ?

    oid get (student + /,

    cout > /.roll >> /.grade >> /.per

    ?

    oid student show( ,

    cout Ggrade >> Gper

    ?

    oid student show ( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    45/156

    Student s

    @et (+s,

    s.show( ,?

    A FUNCTION BEIN* FRIEND OF T,O CLASSES

    class 8eta OO forward declaration

    class :lpha

    int

    publicoid get( ,

    cout >

    ?

    friend oid compare (:lpha* 8eta,

    ?

    class 8eta

    int 2

    public

    oid set( ,

    cout >2

    ?

    friend oid compare (:lpha* 8eta,

    ?

    oid compare (:lpha :* 8eta 8,

    if (:* Y > 8. 3,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    46/156

    cout feet >> inches?

    7istance add (7istance +,

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    47/156

    ?

    Teturn (temp,

    ?oid main( ,

    7istance 74* 76* 7B

    74. get( ,

    76* get( ,

    7B $ 74. add (d6,

    7B. show( ,

    ?

    class 7istance

    int feet* inches

    public

    oid get( K

    OO same as preious

    ?

    oid add (distance +* distance +,

    oid show( ,

    OO same as preious

    ?

    ?

    oid distance add (distance +d4* distance +d6,

    Feet $ d4. feet ' d6* feet

    !nches $ d4. inches ' d6* inches

    if (inches >46,

    Feet $ feet ' inches O46

    !nches ) $ 46

    ?

    ?

    oid main( ,

  • 7/23/2019 C++ NOTES COMPLET

    48/156

    7istance d4* d6* dB d4.get( ,* d6.get( ,

    7B* add (d4* d6,

    7B. show( ,?

    ADDIN* T,O AF CLASS UISN* PRIED FUNCTION

    class distance

    int feet

    int inches public

    oid get( ,

    oid >feet >> inches

    ?

    oid show( ,

    OOsame?

    friend distance add (distance* distance ,

    ?

    distance add (distance /* distance A,

    distance temp

    temp. feet $ /.feet 'A.feet

    temp. inches$ /.inches ' A.inches

    if (temp. inches>$46,

    temp.feet '$ temp. inchesO46

    temp. inches ) $ 46

    ?

    return (temp,

    ?

    oid main( ,

  • 7/23/2019 C++ NOTES COMPLET

    49/156

    distance 74* 76* 7B

    74.get( ,76.get( ,

    7B $ add (74* 76,

    7B.show( ,

    ?

    OPERATOR O4ERLOADIN*

    !t is a mechanism using which a programmer can maEe built in

    operator of &'' act on objects of user defined classes much in the same wa2 liEe

    the2 act on ariables of primitie data t2pe. ;hus we can sa2 b2 using operator

    oerloading a programmer can enhance the worEing range of built in operator from

    primitie t2pe to non primitie t2pe also.

    ;he major adantage offered b2 operator oerloading is the simplicit2

    in readabilit2 of the call i.e. f ncall which are gien using operator oerloading are

    much more eas2 to interpret as compared to conentional function calls.

    "perator "erloading can be done

    4. UaEing use of member function

    6. maEing use of friend function.

    S!'23

    operator (arguments,

    friend (ret=t2pe> operator (,

    "erloading "f 1nar2 "perator :s Uember Function "f ;he class

    (/re !ncrement,

    class counter

    int count

  • 7/23/2019 C++ NOTES COMPLET

    50/156

    public

    counter( ,

    count $K

    ?

    counter (int c,

    count $ c

    ?

    oid operator ''( ,

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    51/156

    ?

    &ounter operator '' ( ,

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    52/156

    ?

    &ounter operator '' (int,

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    53/156

    ?

    friend oid operator '' (counter +,

    ?

    oid operator '' (counter +&,

    "r

    '' c.count counter temp

    return (&, temp. count $ c.count''

    ? return (temp,

    oid main ( ,

    counter &4 $ 4K* &6&4. show ( ,

    &6. $ ''&6

    &4. show ( ,

    &6. show( ,

    ?

    Noe23 when unar2 operators are oerloading using member function then

    dont accept an2 argument but when the2 are oerloaded using friend fnthen the2

    hae one argument of t2pe object (class,.

    class counter

    int count

    public

    friend counter operator '' (counter +,

    counter

    count $K

    ?counter (int i,

    count $ i

    oid show( ,

  • 7/23/2019 C++ NOTES COMPLET

    54/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    55/156

    oid show( ,

    cout $ O46,

    t.feet ' $ t.inchesO46

    t.inches ) $ 46

    ?

    Teturn (t,

    ?

    oid main ( ,

    7istance 74* 76* 7B

    74* get( ,76* get( ,

    7B $ 74'76

    7B. show( ,

    @etch( ,

    ?

    O/e"lo0i'- Bi'"! Ope"o" U$i'- 5"ie'0 F&'io'

    class 7istance

    int feet* inches

    public

    oid get( ,

    cout > feet>> inches

  • 7/23/2019 C++ NOTES COMPLET

    56/156

    ?

    oid show( ,

    cout $46,

    t.feet $ t.feet 't.inchesO46

    t.inches ) $ 46

    ?

    Teturn (t,

    ?

    oid main ( ,

    7istance 74* 76* 7B74.get( ,

    76.get( ,

    7B$74'd6

    7B.show( ,

    ?

    A$$i-'me'23

    76 $ 74'n

    7B $ n ' 74

    class 7istance

    int feet* inches

    public

    oid get( ,

  • 7/23/2019 C++ NOTES COMPLET

    57/156

    OO same as preious

    ?

    oid show( ,

    OO same as preious

    ?

    7istance 7istance operator ' (int n,

    7istance temp

    ;emp feet $ feet ' n

    ;emp. inches $ inches ' n

    if (temp. inches > $ 46,

    ;emp. feet ' $ temp. inches O 46

    ;emp. inches ) $ 46

    ?

    Teturn (temp,

    ?

    7istance operator ' (int /* 7istance A,

    7istance temp

    ;emp.feet $ /'A.feet;emp. inches$ /'A. inches

    if (temp. inches> $ 46,

    ;emp.feet $ temp. feet ' temp. inchesO46

    ;emp. inches ) $ 46

    ?

    Teturn (temp,

    ?

    oid main( ,

    7istance 74* 76* 7B

    int n

    cout > n

    74. get ( ,

  • 7/23/2019 C++ NOTES COMPLET

    58/156

    ! 76 $ 74'n OO can be done using member f n+ friend fn

    76. show( ,

    cout >n

    !! 7B $ n'74 OO not possible through member fn

    7B. show( ,

    ?

    Noe23

    !! can not be done using member function becoP n is an integer and onl2 an

    object can call function not integer. So ! call can be made using member function

    as well as friend function but !! can be done onl2 friend function

    A$$i-'me' 23 74'$76 using member fn+ friend fn

    "erloading Telational "perators

    if (74$ $ 76,

    &ompiler if (74.operator $ $ (76, ,

    class 7istance

    int feet* inches

    public

    oid get( ,

    cout > feet >> inches

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    59/156

    int * 2

    Y$ feet +46 ' inches

    3 $ 7. feet M46 ' 7.inchesif ($ $ 2,

    Teturn (4,

    else

    Teturn (K,

    ?

    oid main( K

    7istance 74* 76

    74. get( ,

    76. get( ,74. show( ,

    76.show( ,

    if (74$ $ 76,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    60/156

    strJKL $ 0K

    ?

    string (char M/,

    strcp2 (str* /,

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    61/156

    4> .(dot operator, member access operator (alread2 oerloaded,

    6> (scope resolution operator,

    it alread2 worEs on classes we oerloadonl2 those operators which worEs on primitine and not on non prin

    B> ] (conditional "perator,!t reGuires there arguments oerloading of

    operators atmost can taEe 6 argument

    C> M. /ointer to member operator

    Q> SiPe of operator

    DYNAMIC MEMORY ALLOCATION (DMA)

    MALLOC NE,

    4. !t is a function !t is a operator

    6. %ecessar2 to include the header file header file not reGuired

    B. !t taEes no* of b2tes as parameter !t taEes no. of elements

    TeGuired as parameter

    C. brings memor2 from local heap bring has no such memor2 store

    Q. Uaimum memor2 that malloc %ew has no such memor2

    can allocate $ RCN8 limitation

    R. Ualloc retirens the address of %ew automaticall2 conerts :llocate blocE in from of (oidM, according to the datat2pe gien

    ;hus its return alue has to be t2pe thus no need to eplicitl2 t2pe

    &asted accordingl2 cast its return alue.

    . when malloc fails it returns null when new fails it returns Pero

    5. Ualloc onl2 creates a new blocE on the other hand operator new

    8ut not call the constructor of the can allocate memor2 as well as

    class for initialiPed the data member call the class for initialiPing the

    "f that blocE members of blocE created. Such

    &onstructors are called d2namic

    &onstructors.

    S!' 5o" 'e73

  • 7/23/2019 C++ NOTES COMPLET

    62/156

    (i, new

    (ii, new JintL

    Alloio'$ O5 Ne 23

    4. int M / new allows programmer to initialiPe the memor2

    /$ new int (4K, allocated but malloc does not proide na2 such feature

    cout

  • 7/23/2019 C++ NOTES COMPLET

    63/156

    D:/ which creates a d2namic arra2 of user defined siPe :ccept alues from user

    in that arra2 + then find largest element in that arra2 along with its position.

    Finall2 it should displa2 the largest element + delete the arra2.

    oid main ( ,

    int n

    cout > n

    int Mp

    p$ new int JnL

    if (p$ $ K,

    cout ma,

    ma $ M(p'i,

    pos $ i

    ?

    ?cout

  • 7/23/2019 C++ NOTES COMPLET

    64/156

    D:/ to create a class cOa string haing a character pointer /. proide constructor in

    the class which accept int as parameter and allocates d2namic arra2 of characters

    of special siPe. %ow proide following member fn

    in the class.

    4. get string which prompts users to enter string in d2namic arra2.

    6. show string which displa2 the string

    B. Teerse string which accept an object as parameter and copies the

    Teerse of string stored in calling object in object

    /assed as parameter.

    class string

    char Mp int n

    public

    string (int,

    string ( ,

    oid showstr( ,

    oid reersestr ( string +,

    oid getstr( ,

    ?

    string string (int siPe,

    p $ new char JsiPe '4L

    if (p$ $K,

    eit (4,

    n $ siPe '4

    ?

    oid string getstr( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    65/156

    for (i$K* j$strlen (p,=4j>$K j==* i'',

    s.pJiL $ pJjL?

    s. pJiL $ 0K

    ?

    oid string showstr( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    66/156

    float sal

    public

    oid get( ,

    cout > age >> name >> sal

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    67/156

    class -mp

    int age

    char nameJ6KL

    float sal

    oid get( ,

    cout > age>> name>> sal

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    68/156

    -nhance the aboe program so that after accepting records of n emplo2ees. 3our

    program prompts the user to enter another name + displa2 the record of that

    emplo2ee onl2. if emplo2ee name is not aailable then program should displa2 themessage record not found.

    class -mp

    int age

    char name J6KL

    float sal

    public

    oid get( ,

    cout > age>> name>> sal

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    69/156

    cout > name >> sal

    ?

    -mp (int !* char Mj * float E,

  • 7/23/2019 C++ NOTES COMPLET

    70/156

    :ge $ !

    Strcp2 (name* j,Sal $ E

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    71/156

    INHERITANCE

    1. Si'-le I'#e"i'e %. M&li le/el I'#e"i'e

    : 8ase class : indirect base class of &

    8 7eried class 8 base class of &

    c

    B. M&liple I'#e"i'e . Hie""#il I'#e"i'e

    : 8 :

    & 8 &

    . H!6"i0 I'#e"i'e o" M&lip# I'#e"i'e

    :

    8 &

    7

    S!' 5o" I'#e"i'e=

    class public O priate O protected < clas=name>

  • 7/23/2019 C++ NOTES COMPLET

    72/156

    7eried class Uode of !nheritance 8ase class

    class 8o

    int l* b* h

    public

    oid get( ,

    cout > l>>b>>h

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    73/156

    Ae$$6ili! R&le$ ,#e' Mo0e O5 I'#e"i'e I$ p&6li

    Dhen a base class is inheritance in public mode then =

    4. :ll the public members of base class become public members of deried

    class i.e. the2 can be accessed through the function of deried class as well

    as b2 objects of deried class.

    6. :ll the protected members of base class become protected members of

    derie class + thus can onl2 be accessed through functions of deried class

    but not b2 the object (main, of deried class.

    B. :ll priate members of base remain priate to their own class + thus can

    neither be accessed through functions of derie class nor b2 object of derieclass.

    EARLY BINDIN*23

    !t is a prours which is eecuted during compilation in which

    compiler binar2 the fnbod2 with the fncall i.e. een before program starts

    eecuting decision regarding the fncall and fnbod2 to be eecuted are taEen b2 the

    compiler since this happens before eecution* we can sa2 it is earl2 binding. -arl2

    binding is alwa2s an action until + unless Ee2word irtual is used infront of the fn

    return t2pe. !t is done on the basis of there criterias=

    (i, function name or

    (ii, calling object t2pe or

    (iii, function parameter t2pe

    in other words earl2 binding is alwa2s in action in normal fncalls*

    oerloaded fncalls and oerloaded operators

    ;he major benefit of earl2 binding is speed of eecution i.e. functioncalls which are bound using earl2 binding get eecuted at a fastest speed as

    compared to late binding fncalls.

    O4ERRIDIN*

  • 7/23/2019 C++ NOTES COMPLET

    74/156

    ;he function oerriding is a term which is used when a derie class

    contains a function with the same protot2pe as its bass class. !n other words* fn

    proided b2 base class has same protot2pe in derie class but in different bod2.

    O/e""i0i'- O/e"lo0i'-

    Scope must be different alwa2s in same class

    82 the classes related b2 means at single leel

    !nheritance

    /rotot2pe of functions protot2pe must be different

    Uust be same .

    ,#e' Mo0e O5 I'#e"i'e I$ P"oee0

    Dhen base class is inheritance in protected mode then=

    4. :ll public members of base class becomes protected member of derie class

    i.e. the2 can be accessed onl2 through function of deried class but not b2

    object of derie class.

    6. :ll protected members of base class become protected members of derie

    class i.e. the2 two can onl2 be accessed through functions of deried classbut not b2 object of derie class.

    B. :ll priate members of base class remains priate to their own class + thus

    neither accessible through the object nor through functions of derie class.

    class %um

    protected

    int a* b

    public

    oid get( ,

    cout > a >> b

    ?

    oid show( ,

  • 7/23/2019 C++ NOTES COMPLET

    75/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    76/156

    4. :ll the public members of base class become priate members of derie

    class i.e. the2 can be onl2 accessed through fnof derie class but not b2 the

    objects of derie class.6. :ll the protected members of base become priate members of derie class

    i.e. the2 two can onl2 be accessed through the function of derie class but

    not b2 objects of derie class.

    B. :ll priate members of base class remain priate to their own class + thus

    can neither be accessed b2 fnnor b2 objects of deries class.

    class %um

    protected

    int a* bpublic

    oid get( ,

    cout > a >> b

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    77/156

    ?

    oid displa2 ( ,

    show( ,

    cout a >> b

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    78/156

    protected

    int cpublic

    oid set( ,

    get( ,

    ?

    oid displa2( ,

    show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    79/156

    7iff%um obj

    obj. accept( ,obj. add( ,

    obj. diff( ,

    obj. print( ,

    ?

    P"o-"m

    class counter

    protectedint count

    public

    oid init (int a,

    count $a

    ?

    oid operator ''( ,

    count ''?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    80/156

    7eccounter 7

    7.int(4K,

    7.show( ,''7

    7.show( ,

    = = 7

    7. show( ,

    ?

    A$$i-'me' 23

    &reate a class cOa arra2 which contains an integer arra2 of siPe 4K. ;he

    class should hae two member functions called get arr and showarr* which should

    accept alues in the arra2 + displa2 its alues respectiel2. %ow create a derieclass of arra2 cOa sortarr* the class should accept a string as parameter if string

    contains asc then sorting should be done in ascending order + if it contains

    desc sorting should be done in descending order.

    Finall2 create the function main which should contain menu drie

    interface for the user haing Q options=

    (i, input

    (ii, displa2(iii, sort in ascending

    (i, sort in descending

    (, Guit

    Sol&io' #include

    #include

    class :rra2

    protected

    int aJQLpublic

    oid get( ,

    oid displa2( ,

    ?

    oid :rra2 get( ,

  • 7/23/2019 C++ NOTES COMPLET

    81/156

    int i

    cout aJiL

    ?

    oid :rra2 displa2 ( ,

    for (int i$K i

  • 7/23/2019 C++ NOTES COMPLET

    82/156

    for (j$K j

  • 7/23/2019 C++ NOTES COMPLET

    83/156

    breaE

    case C sr.descsort( ,

    breaEcase Q

    eit(4,

    ?

    ? while (ch V$Q,

    getch( ,

    ?

    MULTIPLE INHERITANCE

    class base4

    protected

    int a

    public

    oid get( ,

    cout >a?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    84/156

    ?

    oid displa2( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    85/156

    class base4

    protectedint a

    public

    oid get( ,

    cout >a

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    86/156

    set( ,

    ?

    oid add( ,

    c$ a'b

    ?

    oid print( ,

    Dill show show( , base4 show( ,

    -rror ambiguit2 base6 show( ,

    error

    ?

    ?oid main( ,

    7r obj

    obj. accept( ,

    obj. add( ,

    obj. print( ,

    ?

    Role O5 Co'$"&o" 8 De$"&o" I' I'#e"i'e

    :s a basic rule in inheritance* it a base class contains a constructor and

    destructor as well as derie class also contains constructor + destructor* then when

    object of derie class is created* constructor of base class gets eecuted first

    followed b2 constructor of derie class and the destructor is called in reerse order

    i.e. the destructor of derie class is called first followed b2 the destructor of base

    class.

    ;hus we can sa2 constructor are alwa2s called in the order of inheritance and

    destructor are called in reerse order.

    class 8ase

    public

    8ase ( ,

  • 7/23/2019 C++ NOTES COMPLET

    87/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    88/156

    a $ !

    b $ j

    ?oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    89/156

    8ase4 (int, base6 (int,

    oid show( , oid displa2(,

    class 8ase4

    protected

    int a

    public

    8ase (int i,

    a $ i

    ?oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    90/156

    dr (int p* int G, 8ase4 (p,* 8ase6(G,

    c$a'b?

    oid print ( ,

    show ( ,

    displa2( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    91/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    92/156

    obj4. print( ,

    obj6. print( ,

    ?

    Noe

    &onstructors can not be inherited =

    &onstructors are special member fnwhich are eclusiel2 used for

    initialing priate data members of class. %ow since the priate data of class is not

    passed on to its derie classes* so the functions which eplicitl2 initialiPe then

    (constructor, are not inherited. Same is the case with destructor*

    HIERARCHIAL INHERITANCE

    class %um

    protected

    int a* b

    public

    %um (int i* int j,

    a $ ib $ j

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    93/156

    oid show( ,

    %um show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    94/156

    public

    int a

    ?class dr4 irtual public 8ase

    public

    int b

    ?

    class dr6 irtual pubic 8ase

    public

    int c

    ?class drB public dr4* public dr6

    public

    int d

    ?

    oid main( ,

    drB obj

    obj. a $4Kobj. b $ 6K

    obj. c $BK

    obj.d$ obj a ' obj.b ' obj. c

    cout

  • 7/23/2019 C++ NOTES COMPLET

    95/156

    public

    int c

    ?class dr protected 7ata

    public

    7ata & OO bring & in public mode instead of protected

    ?

    Foreg=

    class 8anE

    public oid deposit( ,

    oid withdraw( ,

    oid int=cal( ,

    ?

    class saing Z acct /riate 8anE

    public

    8anE deposit

    8anE with draw?

    POLYMORPHISM

    class 8ase

    public

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    96/156

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    97/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    98/156

    ;his is because pointer of base class can access onl2 those function of

    derie class which are oerridden in the derie class but not those which are

    hidden or added b2 derie class.

    I'e"'l ,o"Gi'- O5 4i"&l F&'io'

    class : 4TABLE FOR A

    int a +: f6( ,

    public

    oid f4( , +: fB( ,

    ?

    irtual oid f6( , obj4 I/;T a

    ?

    ? 4TABLE FOR B

    class 8 public : +8 f6( ,

    int

    public +: fB( ,

    oid fB( ,

    oid f6( , obj6 I/;T

    ?

    oid main( , siPe of class : $ C b2tes

    : obj4

    : Mptr 6 b2tes for ariable a

    ptr $ + obj4 6 b2tes for I/;T

    ptr f6( ,

  • 7/23/2019 C++ NOTES COMPLET

    99/156

    ptr $ + obj6

    ptr fB( ,

    ptr

    fC( ,? ;his will not be eecuted since this is not irtual + compiler

    will go to -arl2 binding table for base class : where there is no function fC.

    4TABLE23

    for eer2 class in &'' which contains at least one irtual function a

    special looE up table for storing addresses of there irtual function is created which

    is Enown as I;:8- or irtual ;able. ;hus in short I;:8- is a table containing

    addresses of irtual functions of the class as well as irtual function of base class if

    the2 are not oerridden. ;his table is then consulted b2 the compiler when a call

    for irtual function is encountered during runtime.

    44PTR23 (4i"&l /oi0 Poi'e",

    for eer2 class which contains a irtual function special pointer is

    created b2 the compiler Enown as II/;T* which is used for pointing to the irtual

    table. ;hus eer2 object of class containing irtual function has its siPe

    incremented b26. %ow wheneer compiler encounters call for irtual function

    through a pointer* it first refers to the address of object to which pointer is pointing

    from their it reads the address contained in II/;T of the object the which is the

    address of I;:8- of class. astl2 within the I:;8- it eecutes the irtualfunction called. ;hus irtual function enhances the siPe of code as reduces the

    eecution speed but proides a fleible wa2 of designing program which can

    respond to the changes which accur at run time.

    class 7ata

    int a

    7ata (int ,

    public

    static 7ata get"bject( ,oid show( ,

    ?

    7ata 7ata (int i,

    a $ i

  • 7/23/2019 C++ NOTES COMPLET

    100/156

    ?

    7ata 7ata getobject( ,

    7ata 7(4K,

    return (7,

    ?

    oid 7ata show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    101/156

    int dim4* dim6

    public

    oid get( ,

    cout > dim4>> dim6

    ?

    ?

    class Tectangle public Figure

    public

    oid area( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    102/156

  • 7/23/2019 C++ NOTES COMPLET

    103/156

    Iirtual 7estructor=

    class 8ase

    protected

    int Mp

    public base( ,

    p$new int J4KL

    cout

  • 7/23/2019 C++ NOTES COMPLET

    104/156

    7estructor can not be declared as pure irtual. Since it is not possible to leae

    empt2 bod2 of destructor since at the time of call of destructor same actiit2 mustbe performed.

    FILE HANDLIN* (STREAM I

  • 7/23/2019 C++ NOTES COMPLET

    105/156

    Steap TeGuired for Driting 7ata !n File

    Sep1 &reate the object of "f stream class

    -g= "f stream obj

    Sep% &onnect the object with file on 2our s2stem.

    Sep write the data through the object in file.

    Sep&lose the file.

    C++ Impleme' O5 A6o/e Sep$

    Sep1 (a, "f stream obj

    Sep% obj. "pen ( 7ata. ;t,

    Sep (a, obj

  • 7/23/2019 C++ NOTES COMPLET

    106/156

    if stream obj

    "bj.open ( 7ata. ;t* ios in,

    "rif stream obj (7ata.tt* ios in,

    C"ei'- O6e O5 5$"em l$$

    4. fstream obj

    "bj.open ( 7ata.tet* ios out ^ ios in,

    6. 1sing &onstructor

    Fstream obj ( 7ata.tt* ios out ^ ios in,

    D:/ to create a file cOa message. tet :ccept a line of tet from user + write itin file character b2 character

    #include

    #include

    #include

    #include

    oid main ( ,

    "f stream out ( Uessage. ;et,

    if (Vout,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    107/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    108/156

    oid main( ,

    Fstream %isha ( 7ata* ios out ^ ios in,if (V %isha,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    109/156

    Stream obj6 (Uessage6. tt* ios out ^ ios in,

    char ch

    if (V "bj4,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    110/156

    eit (4,

    ?

    char tet J5KLcout > n

    cout

  • 7/23/2019 C++ NOTES COMPLET

    111/156

    "bj

  • 7/23/2019 C++ NOTES COMPLET

    112/156

    BINARY I age >> name>> sal

    ?

    oid show( ,

  • 7/23/2019 C++ NOTES COMPLET

    113/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    114/156

    e.get( ,obj.write ((char M, +-* siPe of (-mp,,

    cout > choice

    ? while ( ch $ $ 2,

    "bj. seeEg (K,

    Dhile (opj.read (char M, +-* siPe of (emp,,

    -.show( ,

    getch( ,

    obj.close( ,?

    T!pil Fe&"e O5 Re023

    Noe= if in an2 ane program* we want to read file successiel2 to eof

    we must clear the flag

    "bj. seeEg (K,

    "bj. clear( ,

    A. D:/ to write multiple records of t2pe emp in a file. :ccept a name from user

    + displa2 the record of the emplo2ee b2 searching it with in the file and displa2

    the appropriate message

    oid main( ,

    int flat $ K

    -mp -

    Fstream obj4 ( -Eta* tt* ios out ^ ios in ^ ios trunc ^

    !os binar2,if (V "bj,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    115/156

    7o

    -.get( ,"bj.write ( ( char M, +-* siPe of (-mp,,

    cout name

    "bj. seeEg(K,

    Dhile (obj.read (char M, +-* siPe of (emp,,

    if ( V ( - $ $ name, ,

    -. show( ,

    Flag $4

    8reaE

    ?

    ?

    if ( V flag, or if (flag $ $K,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    116/156

    oid main ( ,

    ifstream in ( Tecords.dal* ios in ^ ios binar2,if ( V in,

    cout temp

    !n.seeEg(K,

    Dhile (in.read ( (char M, +-* siPe of (emp, ,

    if ( ( - $ $temp, $ $K,

  • 7/23/2019 C++ NOTES COMPLET

    117/156

    -.get( ,

    !n.seeEg (=4 M siPe of (-mp,* ios cur,!n.write ( ( char M, +-* siPe of (emp,,

    8reacE

    ?

    ?

    !n.clear( ,

    !n.seeEg(K,

    Dhile (in.read ( ( char M, +-* siPe of (-mp, ,

    -.show( ,

    int operator $ $(char Mn,

    Teturn (strcmp (temp* ptr, ,

    ?

    ?

    A$$i-'me' 23

    4. D:/ to delete record gien b2 user from file.

    6. D:/ to update just the salar2 of emplo2ee whose name gien b2 user.

    TEMPLATES

    ;emplates are a techniGue proide b2 &'' using which a programmer

    can define a single function in which last to be carried out is mentioned but the

    datat2pe is not mentioned. 7uring runtime b2 looEing at the call of the function +

    its parameter t2pe the compiler generates specific ersion of that function to act

    according to parameter passed.

    ;hus in other words* we can sa2 templates are generic functions

    which at the time of creation are onl2 told what to do + during run time the2

    become aware on what t2pe of argument it has to be done.

  • 7/23/2019 C++ NOTES COMPLET

    118/156

    Temple$ "e o5 o !pe

    (i, Function ;emplate(ii, class ;emplate

    S!' F&'io' Temple

    ;emplate

    < function=name> (< t2pe=name> ,

    Emple

    ;emplate

    oid displa2 ( ; n,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    119/156

    oid main ( ,

    int a* bcout > a >> b

    Swap (a* b,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    120/156

    char * 2

    cin >> >>2

    cout

  • 7/23/2019 C++ NOTES COMPLET

    121/156

    ; a

    ; b

    ; cpublic

    oid get( ,

    cin >> a >> b

    ?

    oid add ( ,

    & $ a'b

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    122/156

    ;4 a

    ;6 b

    publicoid get( ,

    cin >> a >>b

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    123/156

    ?

    ;emplate

    oid stacE push (; n,

    if (tos $ $ C,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    124/156

    O/e"lo0i'- O5 A$$i-'me' Ope"o"

    class string

    char Mp

    public

    string (int,

    oid set string (char M,

    oid resetstrign ( char M,

    oid displa2 ( ,

    string( ,

    ?string string (int n,

    /$new int Jn'4L

    ?

    oid string setstring (char Mstr,

    Strcp2 (p* str,

    ?

    oid string resetstring (char Ms,

    Strcp2 (p* s,

    ?

    oid string displa2 ( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    125/156

    S44. setstring ( Hello 1ser,

    S6$s4

    S4. displa2 ( ,S6. displa2 ( , oid string operator $(string

    S4. resetstring ( Delcome, +s,

    S4. displa2 ( ,

    S6. displa2( , strcp2 9p* s.p,

    ? $s.

    ?

    Noe23

    when eer the class is containing d2namic pointer then it is necessar2 to

    oerload assignment operator

    :t the time of oerloading $ it is necessar2 to pass parameter b2 reference.

    ( eg ( string + s, ,.Noe 23

    for cascading of assignment operator it is necessar2 that return t2pe must be

    string

    string string operator $ (string +s,

    string (p* s* p,

    Y$ s*

    Teturn (M this,

    ?

    Noe 23

    A. wh2 do we oerload assignment operator]

    :ns= ;he default assignment operator proided b2 &'' is used for cop2ing oneobject of class to another but it copies the alue bit b2 bit i.e. the alue contained in

    eer2 bit of source object are copied in eer2 bit of destination object. ;his

    behaiour is perfect if source cosle is not containing an2 pointer to d2namicall2

    alloeated blocE* but if it is so then the default eGul to ($, operator will simpl2

    cop2 the address stored in the pointer with in source object to pointer in the

    destination object. ;his will maEe two the defferent pointers point to same location

  • 7/23/2019 C++ NOTES COMPLET

    126/156

    which ma2 cause multiple problems liEe the destructor calling delete operator for

    the same memor2 to oercome this* a programmer should oerload the eGual ($,

    operator + proide his own definition for cop2ing the data pointer b2 pointer ofsource object rather than address.

    O/e"lo0i'- O5 I'$e"io' A'0 E"io' Ope"o"

    /rotot2 "f (istream * -mp+,

    friend ostream + operator > (istream + in* -mp + p,

    !n >> /.age >> /.sal >> /.nameTeturn (in,

    ?

    "stream + operator

  • 7/23/2019 C++ NOTES COMPLET

    127/156

    A. Dhat is the need of oerloading insertion and etraction operator]

    :ns= !n &''* all primitie t2pes liEe int* float* char etc. are displa2 on consoleusing predefined object cout along with insertion operator (

  • 7/23/2019 C++ NOTES COMPLET

    128/156

    &onstructor (8asic t2pe

    OO steps to conert basic to user defined

    ?

    &onersion 8etween 1ser 7efined ;" 8asic

    "perator primitie t2pe of &'' ( , return t2pe

    OO steps to conert basic to user defined

    Teturn (,

    ?

    Emple1ser ;o 8asic + 8asic ;o 1ser

    class Ueter

    float length

    public

    Ueter ( ,

    ength $ K.K

    ?

    Ueter (float cm,

    ength $ &UO4KK

    ?

    "perator float( ,

    Flaot ans$ length M 4KK.KTeturn (ans,

    ?

    oid accept meters( ,

    cout > length

  • 7/23/2019 C++ NOTES COMPLET

    129/156

    ?

    oid show meter( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    130/156

    N M $ 4K

    ! = =

    ?

    Co'/e"$io' F"om U$e" De5i'e0 To U$e" De5i'e0

    4. &onersion Toutine !n Source "bject=

    "perator ( ,

    OOroutines to conert source to destination

    Teturn ( ,

    ?

    6. &onersion Toutine !n 7estination "bject=

    &onstructor ( ,

    OOroutines to conert source to diction

    ?

    &onersion From 1ser 7efined ;o 1ser 7efined 82 /lacing &onersion Toutine

    !n Source "bject

    class Tadian

    float rad

    public

    Tadian ( ,

    Tad $ K.K

    ?

    Tadian (float r,

    Tad $ r

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    131/156

    ?

    class 7egree

    float deg

    public

    7egree( ,

    8eg $ K.K

    ?

    "perator Tadian ( ,

    float $ deg M /!O45K

    Tadian obj $ Teturn (obj ,

    ?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    132/156

    public

    7egree( ,

    7eg $K.K

    ?

    oid show( ,[

    cout

  • 7/23/2019 C++ NOTES COMPLET

    133/156

    7egree 7

    7.get7ata( ,

    Tadian T $ 7 OO Tadian T (7,7. show( ,

    T.show( ,

    ?

    A$$i-'me'23

    #include

    #include

    class hour

    float time

    public

    Hour( ,

    ;ime $ K.K

    ?

    Hour (double ,

    ;ime $OBRKK?

    "perator float( ,

    float $ time M BRKK

    Teturn(,

    ?

    oid accept( ,

    cout > time?

    oid show( ,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    134/156

    oid main( ,

    Hour t4$BRKKHour t6

    ;6. accept( ,

    float $ t6

    cout

  • 7/23/2019 C++ NOTES COMPLET

    135/156

    cin. @et (ch,

    Dhile (ch V $0n,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    136/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    137/156

    "ld width aurrent width

    oid main( ,

    cout. Didth(C,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    138/156

    cout

  • 7/23/2019 C++ NOTES COMPLET

    139/156

    oid main ( ,

    cout.self (ios left* ios adjust field,cout.fill (M,

    cout. precision (6,

    cout.width (R,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    140/156

    ? 6K.CK

    Emple 23oid main ( ,

    cout.setf (ios showpoint,

    cout.setf (ios show pos,

    cout.setf (ios internal* ios adjust field,

    cout.precison(B,

    cout.width (4K,

    cout

  • 7/23/2019 C++ NOTES COMPLET

    141/156

    cin >> a

    cout

  • 7/23/2019 C++ NOTES COMPLET

    142/156

    float f $ 466.BCBC

    cout

  • 7/23/2019 C++ NOTES COMPLET

    143/156

    ?

    ?

    oid main ( ,

    :rra2 obj(Q,

    int

    for (int i$K i>

    "bj.fill (!* , or obj JiL $

    ?

    for (! $ K i

  • 7/23/2019 C++ NOTES COMPLET

    144/156

    ;emp.l $ l ' this l

    ;emp.b $ b ' this b

    ;emp.h $ h ' this

    hTeturn (temp,

    ?

    oid main ( ,

    8o 84

    84.get( ,

    8o 86

    86 $ 84 (Q* B* 9, OO 86 $ 84. operator ( , (Q* B* 9,

    84. show( ,

    86. show( K?

    *RAPHICS MODE PRO*RAMMIN*

    Steps TeGuired for 7esigning @raphics Uode /rogram

    4> &oncert the displa2 monitor from tet mode to graphics mode.

    6> /erform the reGuired graphics operation liEe filling coloring* drawing etc.B> Finall2 close the graphics mode and restore character mode.

    &onerting &haracter ;o /iels

    4. oid init gragraph (int M* int M* char M,

    7rier resolution path of 8@! file

    "r

    Uode

    6. 7rawing using built in functions.B. oid close graph( ,

    oid restorecrtmode( ,

    P"o-"m= D:/ to couert monitor displa2 mode from char to piel and print

    welcome message.

  • 7/23/2019 C++ NOTES COMPLET

    145/156

    #include

    #include

    #include oid main( ,

    int gd* gm* ec

    @d$ 7-;-&;

    !nttgraph (+gd* +gm* &00;&008@!,

    -c$ graphresult( ,

    if (ecV$gr"E, or (ecV$K,

    /rintf (error in initialising,

    eit(4,?

    &leardeice

    "uttet ( welcome to graphics,

    @etch( ,

    &losegraph( ,

    Testarectmode( ,

    ?

    Noe23

    int getma( , returns the maimum alue of Z coordinate.

    int getma2( ,= returns the minimum alue of if Z coordinate.

    :ssignment= Uodif2 the aboe code so that now code displa2 the

    Uessage at the necter of screen.

    oid main ( ,

    int gd* gm* ec

    @d $ 7-;-&;!nitgraph (+gd* + gm* &00;&008@!,

    -c$graphresult( ,

    if (ec V $ gr"E,

    /rintf ( error,

    eit(4,

  • 7/23/2019 C++ NOTES COMPLET

    146/156

    ?

    &leardeice( ,

    int a $ getma( ,int b $ getma2( ,

    "uttetr2 ( ( aO6,* (bO6,* welcome to graphics,

    @etch( ,

    &losegraph( ,

    Testorecrtmode( ,

    ?

    A. D:/ to accept user name + then conert screen to graphics mode + displa2 the

    name gien b2 user at the center of the screen on char at a time.

    oid main ( ,

    char strJ6KL

    int gd* gm* ec

    @d $ 7-;-&;

    !nitgraph (+gd* +gm* c00;&008@!,

    -c $ graphresult( ,

    if (ecV$gr"E,

    /rintf ( error,

    eit(4,

    ?

    &leardeicl ( , printf (enter name,

    Uoe to (getma( , O6* getma2( ,O6,

    for (i$K strJiL i'',

    Spritf (msg* )c* strJiL,

    "ut tet (msg,

    7ela2 (4KKK,?

    ?

    &hanging the font st2le and siPe

    4. oid settetst2le (int font* int dir* int charsiPe,

    description of parameters

  • 7/23/2019 C++ NOTES COMPLET

    147/156

    5o' default Z F"%; (K,

    ;T!/-Y Z F"%; (4,SU: Z F"%; (6,

    S:%S Z S-T!; Z F"%; (B,

    @";H!& Z F"%; (C,

    S&T!/; Z F"%; (Q,

    S!U/-Y Z F"%; (R,

    /T!/-Y Z S&T!/; Z F"%; (,

    &"U/-Y Z F"%; (5,

    -1T"/-:% Z F"%; (9,

    8"7 Z F"%; (4K,

    Di" horiP Z dir (K,

    Iert Z dir (4,

    C#"$ie 2= 4 to 4K

    4 being siPe of 5 5 piel.

    P"o-"m23

    oid main( ,

    char M font Z st2le J L $ 7efualt Z font ====* 8"7=

    F"%;,

    int gd* gm* ec char msgJ6KL

    @d $ 7-;-&;

    !nitgraph (+gd* +gm* & 00 ;&00 8@!,

    -c $ graphresult ( ,

    if (ec V $ K,

    /rintf ( error,

    eit (4,?

    &leardeicl ( ,

    Uoe to (getma( ,O6* getma2( ,O6,

    for (4$K i

  • 7/23/2019 C++ NOTES COMPLET

    148/156

    /rintf (msg $ shi = )s* font=st2le JiL,

    Settet st2le (!* K* 4,

    "uttetr2 (getma( ,O6* getma2( ,O6* msg,@etch( ,

    &leardeicl ( ,

    ?

    &leardeicl( ,

    Testorecrtdeicl ( ,

    ?

    DRA,IN* IN *RAPHICS

    4. 5o" D"i'- Li'e=

    a. oid line (int 4* int 24* int 6* int 26,

    b. oid linerel (int* int,

    c. oid line to (int* int,

    Emple =

    oid mai% ( ,

    int gd* gm* ec* * 2@d $ 7-;-&;

    !nitgraph ( +gd* +gm* c 00;&008@!,

    -c $ graphresult( ,

    if (ec V $ K,

    /rintf ( error,

    eit (4,

    ?

    &leardeicl ( ,Y$getma( ,O6 2$getma2( ,O6

    ine (K* K* * 2,

    @etch ( ,

    &losegraph( ,

    Testorertmode( ,

  • 7/23/2019 C++ NOTES COMPLET

    149/156

    A.D:/ which draws a line from 6K* BK to 4KK piels further + displa2 the

    wordinate of line at its end point.

    oid main ( ,

    int gd* gm* ec* * 2

    @d $ 7-;-&;

    !nitgraph (+gd* + gm* c 00;&008@!,

    -c $ graphresult ( ,

    if (ecV $ K,

    /rintf ( error,

    eit (4,?

    &leardericl ( ,

    Uoe to (6K* BK,

    Sprintf (msg* )d )d* get ( ,* get2( , ,

    "uttet (msg,

    inerel (4KK* 4KK,

    Sprintf (msg* )d )d* get( ,* get 2( , ,

    "uttet (msg,

    ?

    D"i'- Li'ee I' Spei5i ,i0# 8 S!le

    oid setline st2le (int st2le* int pattern* int thicEness,

    S!le= %o &onstant meaning

    K S"!7=!%- solid line

    4 7";;-7 Z!%-

    6 &-%;-T=!%- line withdot + dash

    B 7:SH-7=!%- line with dashes C 1S-T8!;=!%-

    Pe"'23 !t is alwa2s Pero ecept when first parameter is userbit line.

    T#iG'e"$23 4 Z ;H!&N=D!7;H

    B Z %"TU=D!7;H

  • 7/23/2019 C++ NOTES COMPLET

    150/156

    oid main( ,

    int gd* gm* ec* !

    char M st2le J L $ solidline?* 7otted=line*====* 7ashedline?

    @d $ 7-;-&;

    !nitgraph ( +gd* + gm* & 00;&008@!,

    -c$graphresult( ,

    if (ec V$ groE,

    /rintf ( error,

    eit(4,

    ?7eardice( ,

    for (i$K i

  • 7/23/2019 C++ NOTES COMPLET

    151/156

    D"i'- A"$ O" Ci"le$ O" Re'-le$

    4. oid arc (int * int 2* intstangle* int endangle* int rad,

    6. oid piesline (int * int 2* int stangle* int endangle* int rad,

    B. oid circle (int * int 2* int rad,

    C. oid rectangle (int left* int top* int right* int bottom,Q. oid bar (int left* in top* int right* int bottom,

    Filli'- Im-e ,i# Di55e"e' Pe"'$

    oid set color (int,

    oid setfillst2le (int pattern* int st2le,

    Pe"'2= ;he pattern parameter signifies the pattern in which filing is to bemade.

    Nme /l&e Re$&l

    4. -mpt2 Fill K 8acEground color

    6. S"!7=F! 4 Solid Filling

    B. !%-=F!- 6 ==============

    =============

    C. !S:SH[F!- B

    Q. S:SH=F! C

  • 7/23/2019 C++ NOTES COMPLET

    152/156

    R 8N:SH=F!- Q

    . ;8NS:SH=F!- R

    H:;&H=F!-

    YH:;&H=F!- 5

    !%;-T-:I-=F!- 9

    D!7-=7";=F!- 4K

    &"S-=7";=F!- 44

    D:/ which draws a sectangle with white outline + red fill color + should displa2

    the fitling in all of the twele patterns one at a time. :lso name of pattern should

    be displa2ed with in the rectangle.

    oid main ( ,

    int gd* gm* ec* left* right* top* bottom* !char M st2le J L $ -U/;3=F!-* S"!7=F!-* ====*

    &"S-7=7";=F!-?

    @d$7-;-&;

    !nitgraph (+gd* +gm* &00;&008@!,

    -c$grapgresult( ,

    if (ec V$K,

    /rintf (error,

    eit (4,

    ?&leardeicl ( ,

    eft $ getma( ,O6=4KK

    ;op$getma2( ,O6=4KK

    Tight Z getma( ,O6'4KK

    8ottom$getma2( ,O6

    for(i$K i

  • 7/23/2019 C++ NOTES COMPLET

    153/156

    Setfill st2le (!* T-7,

    8ar(left* top* right bottom,Tectangle (left* top* right* bottom,

    "uttet2 (left'QK* top'QK* st2leJiL,

    ?

    ?

    FILLIN* CIRCLES 8 TRIAN*LESS

    4. oid floodfill (int * int 2* int boundar2,

    (* 2, a point with in the figure which has to be filled using flodfill also Enown

    as sad.

    8oundar2 color= &olor at which filling should stop.

    Filling &ircles Dith 7ifferent /attern

    oid main ( ,

    char M pattern $ -U/;3=F!-* ========* &"S-=7";=F!-?

    int gd* gm* ec* * 2* K@d$7-;-&;

    !nitgraph (+gd* +gm* &00;&008@!,

    -c$graphresult( ,

    if (ecV $ gr"E,

    /rintf ( error,

    eit(4,

    ?

    &leardeice( ,

    Y$getma( ,O6 2$getma2( ,O6

    for (i$K i

  • 7/23/2019 C++ NOTES COMPLET

    154/156

    Floodfill (* 2* @T--T,

    Setcolor (DH!;-,

    "uttet2 (=QK* 2=QK* patterJiL,@etch( ,

    &leardiice( ,

    ?

    @etch ( ,

    Testorecrtdeice( ,

    ?

    So"i'- '0 D"i'- Im-e$ O' S"ee'

    4. oid getline (int* int* int* int* oid M,6. int imagesiPe (int* int* int* int,

    B. oid putimage (int* int* oid M* int option,

    oid getimage (int* int* int* int* oid M,

    copies the bit image of specified

    postion in memoer2

    4stparameter indicates left coordinates

    6nd

    parameterer

    to Brdparameter right

    Cthparameter bottom

    Qthparameter pointer to an arra2 large enough to store bit pattern.

    oid putimage Jint * int 2* oid M are* int iption,

    &opies or outputs the image pattern form memoer2 to the specified

    portion on the screen.

    Y $ starting left coordinate

    3 $ starting top coordinate

    :rr $ maner un which color of the resultant piel is to be decided* taEing into

    consideration the piels stored in memor2 + the piels stores on screen.

    int image siPe (int* int* int* int,=

  • 7/23/2019 C++ NOTES COMPLET

    155/156

    Teturns no. of b2tes reGuired to store the image* on an2 Eind of error

    return =4

    oid main ( ,

    int gd* gm* ec

    char M buffer* msg J6KL

    int siPe =Kf=image

    @d $ 7-;-&;

    !nitgraph ( +gd* +gm* &00;&008@!,

    -c $ graphresult( ,

    if (ec V $K,

    /rintf (error,

    eit (4,

    ?

    Tectangle (4QK* 4QK* 6KK* 6KK,

    SiPe=of=image $ image siPe (4QK* 4QK* 6KK* 6KK,

    if (siPe=of=image $ $ =4,

    "uttet2 (getma( ,O6* getma2( ,O6* -rror,

    @etch( ,&lose graph( ,

    eit(4,

    ?

    8uffer $ (char M, malloc (siPe=of=image M siPe of (char, ,

    if (buffer $ $ %1,

    "uttet2 (getma( ,O6* getma2( ,O6* &an not allocate memor2,

    @ecth( ,

    eit(4,

    &lose graph( ,?

    @etimage (4QK* 4QK* 6KK* 6KK* buffer,

    ine (6KK* 66K* 66K* 66K,

    /utimage (4Q* 6KK* buffer* &"/3=/1;,

    @etch( ,

    &losegraph( ,

  • 7/23/2019 C++ NOTES COMPLET

    156/156

    Testore crtdeice( ,

    ?

    4ALUES CREEN EMORY OUTPUT

    COPY3PUT "% "% "%

    "FF "% "%

    "% "FF "FF

    "FF "FF "FF

    OR3PUT "% "% "FF

    "FF "% "%

    "% "FF "%"FF "FF "FF

    OR3PUT "% "% "%

    "FF "% "%

    "% "FF "%

    "FF "FF "FF