OOPS Concept Ppt

Embed Size (px)

Citation preview

  • 8/13/2019 OOPS Concept Ppt

    1/70

    PROGRAMMING 3 (Common to CSE & IT)

    Aim: To understand the concepts of object-oriented programming and master OOP using C++

    Unit I9

    Object oriented programming concepts ! objects ! c"asses ! methods and messages ! abstraction and encapsu"ation! inheritance ! abstract c"asses ! po"#morphism Introduction to C++ ! c"asses ! access specifiers ! function anddata members ! defau"t arguments ! function o$er"oading ! friend functions ! const and $o"ati"e functions !static members ! Objects ! pointers and objects ! constant objects ! nested c"asses ! "oca" c"asses

    Unit II9

    Constructors ! defau"t constructor ! Parameteri%ed constructors ! Constructor ith d#namic a""ocation ! cop#constructor ! destructors ! operator o$er"oading ! o$er"oading through friend functions ! o$er"oading theassignment operator ! t#pe con$ersion ! e'p"icit constructor

    Unit III9

    unction and c"ass temp"ates - E'ception hand"ing ! tr#-catch-thro paradigm ! e'ception specification ! terminateand ne'pected functions ! ncaught e'ception

    Unit IV9

    Inheritance ! pub"ic* pri$ate* and protected deri$ations ! mu"tip"e inheritance ! $irtua" base c"ass ! abstract c"ass !composite objects untime po"#morphism ! $irtua" functions ! pure $irtua" functions ! TTI ! t#peid ! d#namiccasting ! TTI and temp"ates ! cross casting ! do n casting

    Unit V9

    Streams and formatted I,O ! I,O manipu"ators - fi"e hand"ing ! random access ! object seria"i%ation ! namespaces -std namespace ! .SI String Objects ! standard temp"ate "ibrar#

    Total: 4

    TE!T BOO"S:/ 0 Tri$edi* 1Programming ith .SI C++2* O'ford ni$ersit# Press* 3445RE#ERENCES:/ Ira Poh"* 1Object Oriented Programming using C++2* Pearson Education* SecondEdition eprint 34463 S 0 7ippman* 8osee 7ajoie* 0arbara E 9oo* 1C++ Primer2* ourth Edition*Pearson Education* 344:; 0 Stroustrup* 1The C++ Programming "anguage2* Third edition* Pearson

  • 8/13/2019 OOPS Concept Ppt

    2/70

    Unit I

    Object oriented programming concepts !objects ! c"asses ! methods and messages! abstraction and encapsu"ation !

    inheritance ! abstract c"asses !po"#morphismIntroduction to C++ ! c"asses ! access

    specifiers ! function and data members !defau"t arguments ! function o$er"oading! friend functions ! const and $o"ati"efunctions ! static members ! Objects !pointers and objects ! constant objects !nested c"asses ! "oca" c"asses

  • 8/13/2019 OOPS Concept Ppt

    3/70

    ;

    ObjectsAn o$%&'t i( an &n'a)(*lation o+ $ot, +*n'tion( an- -ata

    Objects are an Abstraction represent real world entities Classes are data types that define shared common properties or

    attributes Objects are instances of a class "" objects ha$e attributes (characteristics)* this is sometimes referred

    to as state Objects have State

    have a value at a particular time Objects have Operations

    associated set of operations called methods that describe how tocarry out operations (behavior)

    Objects have Messages request an object to carry out one of its operations by sending it a

    message messages are the means by which we exchange data between objects

  • 8/13/2019 OOPS Concept Ppt

    4/70

    Cla((&( Cla(( :

  • 8/13/2019 OOPS Concept Ppt

    5/70

    c"asses E$er# object be"ongs to (is an instance of) a

    c"ass n object ma# ha$e fie"ds * or $ariab"es

    The c"ass describes those fie"ds

    n object ma# ha$e methods The c"ass describes those methods n bstract >ata T#pe ( >T) bund"es

    together? some data* representing an object or @thing@ the operations on that data

    The operations defined b# the >T are theonly operations permitted on its data

    E'am "e? a Checkin Account * ith o erations

  • 8/13/2019 OOPS Concept Ppt

    6/70

    Prob"em? printing student mar= sheet C"ass student ea" or"d entities? student* teacher* subject* mar= sheet etc

    C"ass studentAPub"ic ?

    int o"".oB string nameB string addressB $oid Print>etai"s() A cout ro""noB cout nameB cout addressB D

    DB

  • 8/13/2019 OOPS Concept Ppt

    7/70

    TV.0/ Cla(( an- 3In(tan'&(

  • 8/13/2019 OOPS Concept Ppt

    8/70

    Object-Oriented Concept The rectang"e area prob"em >efine a c"ass? ect

    >ata? idth* "ength unctions? compute area()

    n object? an instance of the c"assect To So"$e the prob"em* create an object

    of ect* and reFuest this object to returnthe area of the rectang"e

  • 8/13/2019 OOPS Concept Ppt

    9/70

    Encapsu"ation

    class Circle{

    private:int radius

    public: Circle(int r);

    // The area of a circleint compute_area();

    };

    class Trian le{

    private:int ed ea! ed eb! ed ec;

    public: Trian le (int a! int b! int

    c);

    // The area of a trian leint compute_area();

    };

  • 8/13/2019 OOPS Concept Ppt

    10/70

    "#ample Codeclass $ect{

    private: int %idth! len th;public:

    $ect (int %! int l){ %idth & %; len th & l;}

    int compute_area(){ return %idth'len th;}

    }

    main(){

    $ect rect ( !*); int #;

    #&rect +compute_area();cout,,#,,endl;

    }

  • 8/13/2019 OOPS Concept Ppt

    11/70

    M&t,o-( M&((a1&(

    G S&n-in1 a m&((a1& i( o* a o+:! int& a'tin1 it, o$%&'t(5! mani)*latin1 an o$%&'t6( (tat&5

    T&ll( t,& o$%&'t ,at to -o it, it(&l+E7am)l&: To ',an1& t,& ',ann&l on TV

    ! 8& *(& t,& ',ann&l (&l&'tion $*tton( T,i((&n-( a m&((a1& t,at & ant to (&l&'t an& ',ann&l

    ! T,& TV &()on-( to t,& m&((a1& $(&l&'tin1 an- &7&'*tin1 a method.

    ! T,& TV no &'&i &( a n& (i1nal ,i', i(t,& ',ann&l & (&l&'t&-

  • 8/13/2019 OOPS Concept Ppt

    12/70

    M&t,o-! T&ll( t,& o$%&'t ,o to &()on- to a m&((a1&O* TV.0/ o$%&'t( &()on- to t,&+ollo in1 m&((a1&(:

    ! T* n t,& t&l& i(ion (&t on o o++ ! C,an1& t,& ',ann&l! C,an1& t,& ol*m&

  • 8/13/2019 OOPS Concept Ppt

    13/70

  • 8/13/2019 OOPS Concept Ppt

    14/70

    Characteristics of OOP7

    En'a)(*lation: Combining data structure ithactions >ata structure? represents the properties* the states* or

    characteristics of objects ctions? permissib"e beha$iors that are contro""ed through the

    member functions>ata hiding? Process of ma=ing certain datainaccessib"e

    In,& itan'&: bi"it# to deri$e ne objects fromo"d ones permits objects of a more specific c"ass to inherit the properties

    (data) and beha$iors (functions) of a more genera",base c"ass abi"it# to define a hierarchica" re"ationship bet een objects

    Pol mo ),i(m: bi"it# for different objects tointerpret functions different"#

  • 8/13/2019 OOPS Concept Ppt

    15/70

    O-O Princip"es and C++Constructs

    O-O Concept C++ Construct(s)

    bstraction C"assesEncapsu"ation C"assesInformation Hiding Pub"ic and Pri$ate

    9embersPo"#morphism Operator

    o$er"oading* tem "ates $irtua"

  • 8/13/2019 OOPS Concept Ppt

    16/70

    OOP eatures

    6 major features in OOP encapsu"ation

    information hiding inheritance o$er"oading

  • 8/13/2019 OOPS Concept Ppt

    17/70

    Encapsu"ation an o$%&'t &n'a)(*lat&( $ot, it(

    att i$*t&( m&t,o-( imp"ications?

    an attribute, method is attached to an object,c"ass

    hen #ou mention an attribute, methods* #ouha$e to specif# hich object, c"ass it comesfrom

    h# encapsu"ation hen #ou get ho"d of an object* #ou a"so get

    ho"d of its data & beha$iour components good for reuse

  • 8/13/2019 OOPS Concept Ppt

    18/70

    Information Hiding an o$%&'t 'an ,i-& it( int& nal -&tail(

    e g #ou donJt =no ho #our mobi"eJse"ectronics or=s e'cept punching the buttons

    can se"ecti$e"# sho some detai"s to theoutside or"d e g #our mobi"e on"# sho s the number it dia"s

    defines an 1interface2 to interact ith the

    outside or"d e g #our mobi"e interacts ith #our throughthe buttons & screen

  • 8/13/2019 OOPS Concept Ppt

    19/70

  • 8/13/2019 OOPS Concept Ppt

    20/70

    Inheritance

    a 'la(( ma $& (imila to anot,&'la(( $*t $&in1 mo & ()&'iali(&- e g the c"ass 1student2 is simi"ar to the

    c"ass 1person2 but 1student2 is morespecia"ised

    a person has attributes "i=e? se'* age* name

    a student has a"" these + a student no

  • 8/13/2019 OOPS Concept Ppt

    21/70

    Inheritance (contJd) a subclass

    e'tends, specia"ises a superclass inherits attributes & methods from its

    superclass

    ma# ha$e more attributes, methods than itssuperclass ma# change the content, procedure of an

    inherited method i e same method name, signature but different

    beha$iour h# inheritance reuse e'isting c"ass definition customise, specia"ise if needed

  • 8/13/2019 OOPS Concept Ppt

    22/70

    O$er"oading different functions, procedures, methods

    can ha$e the same name pro$ided that the parameters are of different

    t#pes gi$ing a uniFue 1signature2

    the s#stem i"" figure out hich one to in$o=e e g #ou can ha$e 3 procedures* both named

    1call 2* ta=ing a 1dog2 or 1person2 object as

    parameter respecti$e"# >epending on #ou gi$eit a 1dog2 or 1person2 object as the parameter* 8a$a i"" =no hich one to use

  • 8/13/2019 OOPS Concept Ppt

    23/70

  • 8/13/2019 OOPS Concept Ppt

    24/70

    Encapsu"ation is the mechanism that bindstogether code and data* and =eeps both safefrom outside interference or misuse

    / 0oth data and member functions treated assing"e unit3 bstract focuses on beha$ior of object*encapsu"ation focuses on actua" imp"ementation

    ; Encapsu"ation achie$ed through data hiding6 or abstractions to or=* imp"ementationsmust be encapsu"ated

  • 8/13/2019 OOPS Concept Ppt

    25/70

    -bstract T.pes

    class hape{

    public: hape();

    // Calculate the area for// this shapevirtual int compute_area() & 0;

    };

  • 8/13/2019 OOPS Concept Ppt

    26/70

    0ST CTIO. .>E.C PS 7 TIO.

    Definition : 1ata "ncapsulation or 2nformation 3idin is theconcealin of the implementation details of a data ob4ectfrom the outside %orld+

    Definition : Data Abstraction is the separation bet%eenthe specification of a data object and its implementation + Definition : - data type is a collection of objects and a set

    of operations that act on those ob4ects+

    Definition : -n abstract data type (ADT) is a data t.pethat is or ani5ed in such a %a. that the specification of theob4ects and the specification of the operations on theob4ects is separated from the representation of the ob4ectsand the implementation of the operation

  • 8/13/2019 OOPS Concept Ppt

    27/70

    d$antages of >ata bstraction and>ata Encapsu"ation

    Simp"ification of soft arede$e"opment Testing and >ebugging

    eusabi"it# 9odifications to the representation ofa data t#pe

  • 8/13/2019 OOPS Concept Ppt

    28/70

    -bstract Classes

    6a. Contain abstract methods ome methods and data ma. be definedabstract class Coloured hape {private Colour c; // stora e allocated for each

    public abstract void dra%();public Colour etColour() {return c;}public abstract void erase();

    } 7o instances allo%ed ubclasses must implement all abstract methods or the. are also abstract

  • 8/13/2019 OOPS Concept Ppt

    29/70

    Florida Community College at Jacksonville

    Object-oriented Principle -

    Inheritance Inheritance is the process by which one object

    acquires the properties of another object. By use ofinheritance, an object need only define all of itscharacteristics that make it unique within its class, itcan inherit its eneral attributes from its parent.

    29 of 10 slides

    !ccount

    "heckin #ort a e $oan

    COP 2551 Object Oriented Programming OO Conce!ts Overvie"

  • 8/13/2019 OOPS Concept Ppt

    30/70

    Florida Community College at Jacksonville

    Object-oriented Principle %

    &ncapsulation &ncapsulation is the mechanism that binds to ether

    the code and the data it manipulates, and keeps bothsafe from outside interference and misuse.

    #0 of 10 slides

    A Class

    Pri'ate 'ariablesand methods

    Public 'ariables and methods

    Public 'ariables is not recommended

    COP 2551 Object Oriented Programming OO Conce!ts Overvie"

  • 8/13/2019 OOPS Concept Ppt

    31/70

    Encapsu"ation Hides the imp"ementation detai"s of a

    c"ass orces the user to use an interface to

    access data

    9a=es the code more maintainab"e PI doc as an e'amp"e

  • 8/13/2019 OOPS Concept Ppt

    32/70

  • 8/13/2019 OOPS Concept Ppt

    33/70

    27T$ 1

  • 8/13/2019 OOPS Concept Ppt

    34/70

    =asic C "#tension from Ccomments

    /* You can still use the old comment style, *//* but you must be very careful about mixing them */// It's best to use this style for 1 line or partial lines/* And use this style when your comment

    consists of multiple lines */

    cin and cout (and >include ,iostream+h?)cout !"ello#$char name%1& $cin (( name$cout !"i, # name #, nice name)# endl$

    cout endl$ // print a blan line declarin variables almost an.%here // declare a variable when you need it for +int 1$ -$ ..

    0 char a m2$

    cout a$

  • 8/13/2019 OOPS Concept Ppt

    35/70

    C7 SSES

  • 8/13/2019 OOPS Concept Ppt

    36/70

    Kraphica" epresentationof C"asses

    co"ournamehere

    attributes(data

    component)

    methods(proceduralcomponent)

    interfaceto the

    outside world a"=s imbar=

    the Do1 c"assthe P& (on c"ass

    nameo n

    a"=instruct

    rite emai"

  • 8/13/2019 OOPS Concept Ppt

    37/70

    Kraphica" epresentationof Objects

    co"our? hite,bro nname? 7assiehere? nnur

    a"=s imbar=

    name? a$io n? 7assie

    a"=instruct

    rite emai"

    a$i* a P& (on object Lassie * a Do1 objectco"our? #e""o

    name? Pa=ihere? '''

    a"=s imbar=

    Pa=i* a Do1 object

    L* another Do1 object

    name? ajao n? nu""

    a"=instruct

    rite emai"

    aja* a P& (on object

    L* another Do1 objectL* another P& (on object

  • 8/13/2019 OOPS Concept Ppt

    38/70

    ccess specifiers Specif# hether the data defined i"" be

    a$ai"ab"e to the users of the objects of thec"ass

    Pub"ic Pri$ate Protected

    Pub"ic? the data defined under pub"ic isa$ai"ab"e to objects Pri$ate? data defined under pri$ate is not

    a$ai"ab"e to objects

    C"ass student

  • 8/13/2019 OOPS Concept Ppt

    39/70

    APub"ic ? int o"".oB pri$ate? string nameB string addressB

    pub"ic?

    $oid Print>etai"s() A cout ro""noB cout nameB

    cout addressB DDB $oid main()A student student/B student/ o"".oM 5B student/ nameM 1N2B ,, does not or= since name is pri$atestudent/ addressM 1 2B ,, does not or= since name is pri$ateStudent/ Print>etai"s()BD

  • 8/13/2019 OOPS Concept Ppt

    40/70

    unction and data member unctions can be defined either inside or outside the

    c"ass >efining function members outside c"ass/ $er# simp"e to define functions outside c"ass3 If the functions are big define outside

    Static data members of c"ass/ Static members are stored at a "ocation here the# areretained throughout the e'ecution of the program andare not stored ith c"ass objects

    3 Stored on"# as a sing"e cop#! simi"ar to memberfunctions

    ; "" the static data members are initia"i%ed to %ero at thetime of dec"aration

    C"ass student ,,defining function members outside c"ass

  • 8/13/2019 OOPS Concept Ppt

    41/70

    APub"ic ? int o"".oB string nameB string addressB

    $oid Print>etai"s()B ,,protot#pe DB

    $oid student ?? Print>etai"s() A

    cout ro""noB cout nameB cout addressB DDB $oid main()

    A student student/B student/ o"".oM 5B student/ nameM 1N2Bstudent/ addressM 1 2BStudent/ Print>etai"s()BD

    c"ass student

  • 8/13/2019 OOPS Concept Ppt

    42/70

    A pub"ic? static int Passing9ar=B int Subject9ar=Q:RB boo" fai"B $oid >isp"a#9ar=s() A for (int iM4Bi :B++i) A cout 19ar=s of subject .o 2B cout i 1is2 Subject9ar=QiRB D

    D $oid Set9ar=s (int 9ar=sQ:R

    A for (int iM4B i :B ++i) A Subject9ar=sQiRM9ar=sQiRB DD boo" Chec=Passing()A fai"Mfa"seB

    for (int iM4Bi :B ++i) A if (Subject9ar=QiR Passing9ar=)fai"M trueB

    D if (fai") cout 1congratu"ations ou are passing n2B e"se cout 1sorr# ou are fai"ing n2B

    return fai"B

  • 8/13/2019 OOPS Concept Ppt

    43/70

  • 8/13/2019 OOPS Concept Ppt

    44/70

    D&+a*lt A 1*m&nt(

    defau"t argument is a $a"ue gi$enin the +*n'tion -&'la ation thatthe compi"er automatica""# inserts ifthe ca""er does not pro$ide a $a"uefor that argument in the +*n'tion'all5

    S#nta'?

    return_type f (@! type # & default_value !@);

  • 8/13/2019 OOPS Concept Ppt

    45/70

    D&+a*lt A 1*m&nt((E'amp"es)

    The defau"t $a"ue of the 3 nd argument is 3

    This means that if the programmerca""s )o (')* the compi"er i""rep"ace that ca"" ith )o ('*3)*returning ' 3

    double pow (double #! int n&9)

    // computes and returns #n

  • 8/13/2019 OOPS Concept Ppt

    46/70

    D&+a*lt A 1*m&nt(( u"es)

    Once an argument has a defau"t$a"ue* a"" the arguments after itmust ha$e defau"t $a"ues

    Once an argument is defau"ted in afunction ca""* a"" the remainingarguments must be defau"ted

    int f (int #! int .&0! int n)// ille al

    int f (int #! int .&0! int n& )// le al

  • 8/13/2019 OOPS Concept Ppt

    47/70

    unction o$er"oading unction redefined ith different set of

    argumentsEN? add(f"oat* f"oat) dd(int* int) dd (int* int* int) unction o$er"oading is usefu" hen simi"ar

    function is reFuired to be ca""ed ith either$ariab"e number of arguments or arguments ofdifferent t#pe or both

  • 8/13/2019 OOPS Concept Ppt

    48/70

    8,at i( a # i&n- #*n'tion;

  • 8/13/2019 OOPS Concept Ppt

    49/70

    friend function is used for accessing the non-pub"ic members of a c"ass c"asscan a""o non-member functions and other c"asses to access its o n pri$ate data*b# ma=ing them friends Thus* a friend function is an ordinar# function or a memberof another c"ass

  • 8/13/2019 OOPS Concept Ppt

    50/70

    riend functionc"ass c"ass/Apri$ate?int numB

    pub"ic?$oid get()Acout @.umber?@BcinXXnumBDfriend $oid disp"a#(c"ass/ c")BDB$oid disp"a#(c"ass/ c/)Acout C/ . 9BDint main()

    Ac"ass/ c"sBc"s get()Bdisp"a#(c"s)BD

    ,,static member friend function matri' mu"tip"ication

  • 8/13/2019 OOPS Concept Ppt

    51/70

    Zinc"ude iostream hXZinc"ude conio hX c"ass matri'$ectorA

    static int aQ;RQ;RBstatic int bQ;RBstatic int cQ;RBpub"ic?$oid getmatri'($oid)B$oid get$ector($oid)Bfriend int mu"tip"#(matri'$ector m$)

    DB int matri'$ector??aQ;RQ;RBint matri'$ector??bQ;RBint matri'$ector??cQ;RB $oid matri'$ector??getmatri'($oid)Acout @ n enter the matri' $a"ue n@Bfor(int iM4Bi ;Bi++)A

    for(int jM4Bj ;Bj++)AcinXXaQiRQjRBD

    DD

    $oid matri'$ector??get$ector($oid)A

  • 8/13/2019 OOPS Concept Ppt

    52/70

    Acout @ n@B

    cout @ n enter the $ector ?@Bfor(int =M4B = ;B=++)

    AcinXXbQ=RB

    DD int mu"tip"#(matri'$ector m$)Acout @matri' - $ector mu"tip"ication n@Bcout @ n the $ector n@B

    for(int mM4Bm ;Bm++)Acout @ n@Bcout m$ bQmRBD

    cout @ n the matri' n@B

    for(int iM4Bi ;Bi++)Afor (int jM4B j ;B j++)Acout m$ aQiRQjRBcout @ t@B

    D

    cout @ n@B

  • 8/13/2019 OOPS Concept Ppt

    53/70

    cout @ n@B Dfor(int eM4Be ;Be++)A

    for(int dM4Bd ;Bd++)

    Am$ cQeRMm$ cQeR+m$ aQeRQdR[m$ bQdRBDD

    cout @ n the resu"t is n@Bfor(int nM4Bn ;Bn++)A

    cout m$ cQnRBcout @ n@BDreturn 4BD int main()Ac"rscr()Bmatri'$ector m$Bm$ get$ector()Bm$ getmatri'()Bmu"tip"#(m$)Bgetch()Breturn 4BD

    ' ( & $& * ' i (

  • 8/13/2019 OOPS Concept Ppt

    54/70

    'on(t m&m$& +*n'tion( function* hich guarantees not to

    modif# the in$o=ing object

    If the bod# of the const functioncontains a statement that modifies thein$o=ing object* the program does notcompi"e

    One e'ception here is the mutab"emember mutab"e data member can bemodified b# const function

  • 8/13/2019 OOPS Concept Ppt

    55/70

  • 8/13/2019 OOPS Concept Ppt

    56/70

    \o"ati"e functions

    member function in$o=ed b# a$o"ati"e object

    $o"ati"e object ]s $a"ue can bechanged b# e'terna" parametershich are not under the contro" ofthe program

    olatil& m&m$& +*n'tion( >ec"are a member function ith the$ " i" ifi h i b "" d f "# f $ " i"

  • 8/13/2019 OOPS Concept Ppt

    57/70

    $o"ati"e specifier to ensure that it can be ca""ed safe"# for a $o"ati"eobject?

    c"ass 0 A int 'Bpub"ic?$oid f() $o"ati"eB ,, $o"ati"e member function DB

    int main() A $o"ati"e 0 bB ,, b is a $o"ati"e object

    b f()B ,, ca"" a $o"ati"e member function safe"#D

    The object b is dec"ared $o"ati"e Ca""ing a non-$o"ati"e memberfunction from this object is unsafe* because b^s state might ha$ebeen changed b# a different thread in the meantime

    To ensure that f() can be ca""ed safe"# for a $o"ati"e object* it^s

  • 8/13/2019 OOPS Concept Ppt

    58/70

    Pointers and objects

    int x = 10;int *p;

    p = &x;

    p ets the address of x in memor.+

    p

    x10

  • 8/13/2019 OOPS Concept Ppt

    59/70

  • 8/13/2019 OOPS Concept Ppt

    60/70

  • 8/13/2019 OOPS Concept Ppt

    61/70

    ""ocating memor# using n&

    Point *p = new Point(5, 5);

    new a""ocates space to ho"d the object new ca""s the objectJs constructor new returns a pointer to that object

    > "" ti # i

  • 8/13/2019 OOPS Concept Ppt

    62/70

    >ea""ocating memor# using-&l&t&

    // allocate memoryPoint *p = new Point(5, 5);

    // !ree t"e memory#elete p;

    or e$er# ca"" to new * there must bee'act"# one ca"" to #elete

  • 8/13/2019 OOPS Concept Ppt

    63/70

    sing n& ith arra#s

    int x = 10;int* n$m%1 = new int 10'; // oint* n$m%2 = new int x'; // o

    Initia"i%es an arra# of /4 integers on theheap

    A )oint& 'an )oint to an o$%&'t ' &at&- $ a

  • 8/13/2019 OOPS Concept Ppt

    64/70

    ) ) $ $'la((5

    O$%&'t )oint& ( a & *(&+*l in ' &atin1 o$%&'t( at

    *n tim&5 student s/B student [ptr M &s/B s/ getdata()B

    s/ sho ()B eFui$a"ent to ptr-Xgetdata()B ptr-X sho ()B or ([ptr) sho ()B e can a"so create the objects using pointers and

    ne operator student [ptr M ne studentB This a""ocates enough memor# for the data members in

    the object structure and assigns the address of thememor# space to ptr

  • 8/13/2019 OOPS Concept Ppt

    65/70

    constant objects const student s/('*#)B ,, object s/ is constant n# attempt to modif# the $a"ues of ' and # i""

    generate compi"e time error constant object can ca"" on"# constant member

    functions $oid Print>etai"s()const A cout ro""noB

    cout nameB cout addressBD

    nested c"asses

  • 8/13/2019 OOPS Concept Ppt

    66/70

    nested c asses It is an another a# of inheriting properties of one

    c"ass into another rom this e can understand that an object can be

    co""ection of man# other objects* that is a c"ass cancontain objects of other c"asses as its members

    c"ass a"pha(L)B c"ass beta(L)B c"ass gamma A

    a"pha a/B beta b/B DB"" objects of gamma c"ass i"" contain the objects a/

    and b/ This =ind of re"ationship is ca""ed

    "oca" c"asses

  • 8/13/2019 OOPS Concept Ppt

    67/70

    oca c asses C"asses can be defined and used inside a function or a b"oc= Such c"asses are

    ca""ed "oca" c"asses $oid test(int a) ,,function AL

    c"ass student ,,"oca" c"ass A

    L ,,c"ass definitionL DBLL student s/(a)B ,,create student objectLLD7oca" c"asses can use g"oba" $ariab"es and static $ariab"es dec"ared inside the

    functionEnc"osing function cannot access the pri$ate member of a "oca" c"ass

  • 8/13/2019 OOPS Concept Ppt

    68/70

  • 8/13/2019 OOPS Concept Ppt

    69/70

    C++ - d$ance E'tension

    C++ a""o function o$er"oading In C++* functions can use the same

    names* ithin the same scope* if

    each can be distinguished b# itsname and signature

    The signature specifies the

    number* t#pe* and order of theparameters The name p"us signature * then*

    uniFue"# identifies a function

  • 8/13/2019 OOPS Concept Ppt

    70/70

    Take Home Message There are man# different =inds of

    programming paradigms* OOP is oneamong them

    In OOP* programmers see the e'ecutionof the program as a co""ection ofdia"oging objects

    The main characteristics of OOP7 inc"udeencapsu"ation* inheritance* andpo"#morphism .ot on"# OOP7 can doOOP* b t " th