39377219-notes-of-c

download 39377219-notes-of-c

of 33

Transcript of 39377219-notes-of-c

  • 8/12/2019 39377219-notes-of-c

    1/33

  • 8/12/2019 39377219-notes-of-c

    2/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    Q.2 Explain the compilation of C++ Program

    Prof. Bhanudas satam Genesis -!

    Pre"rocessor

    directives

    #iostream.h$

    Source.code

    % somename.c""&

    'an(ua(e

    Processor

    #Com"i)er$

    somename.o*

    'in,er

    somename.ee(et created

    C

    '

    Bui)t-in fn

    #'i*$

    #Procedure

    ta,en from

    'in,er$

  • 8/12/2019 39377219-notes-of-c

    3/33

  • 8/12/2019 39377219-notes-of-c

    4/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    Classes also encourage coherence, which means that a given class does one thing. >yincreasing coherence, a program becomes easier to understand, more simply

    organi?ed, and this better organi?ation is reflected in a further reduction in coupling.

    nheritance

    Inheritancemeans that a new class can be defined in terms of an e6isting class. #hereare three common terminologies for the new class the derivedclass, the childclass, or

    the subclass. #he original class is the baseclass, theparentclass, or the superclass.#he new child class inherits all capabilities of the parent class and adds its own fieldsand methods. $ltho inheritance is very important, especially in many libraries, is oftennot used in an application.

    Polymorphism

    Polymorphismis the ability of different functions to be invo

  • 8/12/2019 39377219-notes-of-c

    5/33

  • 8/12/2019 39377219-notes-of-c

    6/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    #here are following types of constructor

    "efault constructor 'on!/arameteri?ed constructor)

    $ default constructor is a constructor that either has no parameters, or if it hasparameters, all the parameters have default values.

    If no user!defined constructor e6ists for a class $ and one is needed, the compilerimplicitly declares a constructor $$(). #his constructor is an inline public memberof its class. #he compiler will implicitly define $$() when the compiler uses this

    constructor to create an ob:ect of type $. #he constructor will have no constructorinitiali?er and a null body.

    #he compiler first implicitly defines the implicitly declared constructors of the baseclasses and nonstatic data members of a class $ before defining the implicitly

    declared constructor of $. o default constructor is created for a class that has anyconstant or reference type members

    Parameteri(ed constructor

    $ parameteri?ed constructor is a constructor that either has parameters. Whenob:ect is created at that time we pass the parameter to ob:ect.

    0include 1iostream2

    using namespace std4class myclass3 int a, b4public

    myclass(int i, int :)3

    ai4b:4

    8

    void show() 3 cout 11 a 11 5 5 11 b4 884

    int main()3

    myclass ob(, E)4 ob.show()4

    Prof. Bhanudas satam Genesis -5

  • 8/12/2019 39377219-notes-of-c

    7/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    return 48

    Copy)constructor

    $ copy constructor is a special constructor in the C++programming language

    used to create a new ob:ectas a copyof an e6isting ob:ect. #his constructor ta

  • 8/12/2019 39377219-notes-of-c

    8/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    ',i4ate intcu,,ent)*)

    Q. Write short notes on Copy Constructor&

    $ copy constructor is a special constructorin the C++programming languageused tocreate a new ob:ect as a copyof an e6isting ob:ect. #his constructor ta

  • 8/12/2019 39377219-notes-of-c

    9/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    y = p.y;

    }

    . . .

    77+++ file m_',o-,am.c'' ++++++++++++++++++++++++++++++++++++

    . . .

    =oint ') 77 calls default const,ucto,

    =oint s + ') 77 calls co' const,ucto,.

    ' + s) 77 assi-nment? not co' const,ucto,.

    Q.3 %hat is class& and the ,arious categories of classes&

    In ob:ect!oriented programming, a class is a programming languageconstruct that isused as a blueprintto create ob:ects. #his blueprint includes attributesand methods

    that the created ob:ects all share.

    Categories of classes

    #here are many categories of classes depending on modifiers. ote that thesecategories do not necessarily categori?e classes into distinct partitions. "or e6ample,

    while it is impossible to have a class that is both abstract and concrete, a sealed classis implicitly a concrete class, and it may be possible to have an abstract partial class.

    Concrete classes

    $ concrete class is a class that can be instantiated. #his contrasts with abstract classes

    as described below.

    -stract classes

    $n abstract class, or abstract base class ($>C), is a class that cannot be instantiated.uch a class is only meaningful if the language supports inheritance. $n abstract class

    is designed only as a parent class from which child classes may be derived. $bstractclasses are often used to represent abstract concepts or entities. #he incomplete

    features of the abstract class are then shared by a group of subclasses which adddifferent variations of the missing pieces.

    $bstract classes are superclasses which contain abstract methods and are defined suchthat concrete subclasses are to e6tend them by implementing the methods. #hebehaviors defined by such a class are 5generic5 and much of the class will be undefinedand unimplemented. >efore a class derived from an abstract class can become

    concrete, i.e. a class that can be instantiated, it must implement particular methodsfor all the abstract methods of its parent classes.

    When specifying an abstract class, the programmer is referring to a class which haselements that are meant to be implemented by inheritance. #he abstraction of the

    class methods to be implemented by the subclasses is meant to simplify softwaredevelopment. #his also enables the programmer to focus on planning and design.

    Prof. Bhanudas satam Genesis -8

    http://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Blueprinthttp://en.wikipedia.org/wiki/Object_(object-oriented_programming)http://en.wikipedia.org/wiki/Attribute_(computing)http://en.wikipedia.org/wiki/Method_(computer_science)http://en.wikipedia.org/wiki/Instantiation_(computer_science)http://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Blueprinthttp://en.wikipedia.org/wiki/Object_(object-oriented_programming)http://en.wikipedia.org/wiki/Attribute_(computing)http://en.wikipedia.org/wiki/Method_(computer_science)http://en.wikipedia.org/wiki/Instantiation_(computer_science)
  • 8/12/2019 39377219-notes-of-c

    10/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    @ost ob:ect oriented programming languages allow the programmer to specify whichclasses are considered abstract and will not allow these to be instantiated.

    Q.! %hat are access specifier in class&

    In C++, there are three access specifiers for classes public, protected, and private.#hese are important for encapsulation. $ public member is one that can be accessed

    by any code. $ protected membercan only be accessed by methods in the classand itsderived class. $ private membercan only be accessed by methods in the class itself.

    @ember access determines if a class member is accessible in an e6pression ordeclaration. uppose 6 is a member of class $. Class member 6 can be declared to

    have one of the following levels of accessibility

    pulic 6 can be used anywhere without the access restrictions defined byprivate or protected.

    pri,ate 6 can be used only by the members and friends of class $.

    protected 6 can be used only by the members and friends of class $, and themembers and friends of classes derived from class $.

    @embers of classes declared with the

  • 8/12/2019 39377219-notes-of-c

    11/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    6.a *4 6.b E4 6.c 4 884

    int main()3 $ y4

    y.a -4 y.b J4

    y.c 4

    > ?4 ?.a '4

    ?.b ''4 ?.c 'G4

    8

    #he following table lists the access of data members $a $b, and $c in variousscopes of the above e6ample.

    /cope -00a -00 -00c

    function >f()o access. @ember $ais private.

    $ccess. @ember $bis public.

    $ccess. Class > inherits from $.

    function Cf()$ccess. Class C is a friendof $.

    $ccess. @ember $bis public.

    $ccess. Class C is a friend of $.

    ob:ect y in

    main()

    o access. @ember y.a isprivate.

    $ccess. @ember y.a ispublic.

    o access. @ember y.c isprotected.

    ob:ect ? in main()o access. @ember ?.a isprivate.

    $ccess. @ember ?.a ispublic.

    o access. @ember ?.c isprotected.

    Q. unctionmethod o,erloading and Constructor o,erloading&

    "unction@ethod overloading is a feature found in various programming languagessuch as C++and Kavathat allows the creation of several methodswith the same name

    which differ from each other in terms of the type of the input and the type of theoutput of the function.

    $n e6ample of this would be a s;uarefunction which ta

  • 8/12/2019 39377219-notes-of-c

    12/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    @ethod overloading is usually associated with statically!typedprogramming languageswhich enforce type checut what if you want to create an ob:ectin more than one wayM "or e6ample, suppose you build a class that can initiali?e itself

    in a standard way and also by reading information from a file. Nou need twoconstructors, one that ta

  • 8/12/2019 39377219-notes-of-c

    13/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    "or e6ample, you can refer to the particular class ob:ect that a member function iscalled for by using the this pointer in the body of the member function. #he following

    code e6ample produces the output a E

    0include 1iostream2using namespace std4

    struct P 3

    private int a4

    public void et&a(int a) 3

    #he AthisA pointer is used to retrieve A6ob:.aA hidden by the automatic variable AaA this!2a a4

    8 void /rint&a() 3 cout 11 5a 5 11 a 11 endl4 8

    84

    int main() 3 P 6ob:4 int a E4 6ob:.et&a(a)4

    6ob:./rint&a()48

    In the member function et &a(), the statement this!2a a uses the this pointer toretrieve 6ob:.a hidden by the automatic variable a.

    Q.17 %hat is inheritance&

    In ob:ect!oriented programming, inheritance is a way to form new classes(instances of

    which are called ob:ects) using classes that have already been defined.

    #he new classes,

  • 8/12/2019 39377219-notes-of-c

    14/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    generali?ation of 5apple5, 5orange5, 5mango5 and many others. 9ne can consider fruitto be an abstraction of apple, orange, etc. Conversely, since apples are fruit (i.e., an

    apple is!a fruit), apples may naturally inherit all the properties common to all fruit,such as being a fleshy container for the seed of a plant.

    Q.11 %hat is application of inheritance&

    -pplications of inheritance

    #here are many different aspects to inheritance. Different uses focus on different

    properties, such as the e6ternal behavior of ob:ects, internal structure of the ob:ect,

    structure of the inheritance hierarchy, or software engineering properties ofinheritance. ometimes itAs desirable to distinguish these uses, as itAs not necessarilyobvious from conte6t.

    /peciali(ation

    9ne common reason to use inheritance is to create speciali?ations of e6isting classesor ob:ects. #his is often called subtypingwhen applied to classes. In speciali?ation, thenew class or ob:ect has data or behavior aspects that are not part of the inherited

    class. "or e6ample, a 5>an< $ccount5 class might have data for an 5account number5,5owner5, and 5balance5. $n 5Interest >earing $ccount5 class might inherit 5>anertrand @eyer, the creator of theob:ect!oriented programming language %iffel, lists twelve different uses of inheritance,

    most of which involve some amount of implementation inheritance.

    Q.12 Explain different type of inheritance %ith the help of example&

    Q.13 %hat is o,erriding&

    @ethod overriding, in ob:ect oriented programming, is a language feature that allows asubclassto provide a specific implementation of a methodthat is already provided by

    one of its superclasses. #he implementation in the subclass overrides (replaces) theimplementation in the superclass.

    $ subclass can give its own definition of methods which also happen to have the samesignatureas the method in its superclass. #his means that the subclassAs method has

    the same name and parameterlist as the superclassAs overridden method. Constraints

    on the similarity of return type vary from language to language, as some languagessupport covarianceon return types.

    @ethod overriding is an important feature that facilitates polymorphismin the design

    of ob:ect!orientedprograms.

    ome languages allow the programmerto prevent a method from being overridden, or

    disallow method overriding in certain core classes. #his may or may not involve aninability to subclass from a given class.

    Prof. Bhanudas satam Genesis -4

    http://en.wikipedia.org/wiki/Override_(object-oriented_programming)http://en.wikipedia.org/w/index.php?title=Implementation_inheritance_(object-oriented_programming)&action=edit&redlink=1http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programminghttp://en.wikipedia.org/wiki/Delegation_patternhttp://en.wikipedia.org/wiki/C%2B%2Bhttp://en.wikipedia.org/w/index.php?title=Implementation_inheritance_(computer_science)&action=edit&redlink=1http://en.wikipedia.org/wiki/Is-ahttp://en.wikipedia.org/wiki/Has-ahttp://en.wikipedia.org/wiki/Bertrand_Meyerhttp://en.wikipedia.org/wiki/Eiffel_(programming_language)http://en.wikipedia.org/wiki/Object_oriented_programminghttp://en.wikipedia.org/wiki/Subclass_(computer_science)http://en.wikipedia.org/wiki/Method_(computer_science)http://en.wikipedia.org/wiki/Superclass_(computer_science)http://en.wikipedia.org/wiki/Method_signaturehttp://en.wikipedia.org/wiki/Parameter_(computer_science)http://en.wikipedia.org/wiki/Parameter_covariancehttp://en.wikipedia.org/wiki/Polymorphism_(computer_science)http://en.wikipedia.org/wiki/Object-orientedhttp://en.wikipedia.org/wiki/Computer_programhttp://en.wikipedia.org/wiki/Programmerhttp://en.wikipedia.org/wiki/Override_(object-oriented_programming)http://en.wikipedia.org/w/index.php?title=Implementation_inheritance_(object-oriented_programming)&action=edit&redlink=1http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programminghttp://en.wikipedia.org/wiki/Delegation_patternhttp://en.wikipedia.org/wiki/C%2B%2Bhttp://en.wikipedia.org/w/index.php?title=Implementation_inheritance_(computer_science)&action=edit&redlink=1http://en.wikipedia.org/wiki/Is-ahttp://en.wikipedia.org/wiki/Has-ahttp://en.wikipedia.org/wiki/Bertrand_Meyerhttp://en.wikipedia.org/wiki/Eiffel_(programming_language)http://en.wikipedia.org/wiki/Object_oriented_programminghttp://en.wikipedia.org/wiki/Subclass_(computer_science)http://en.wikipedia.org/wiki/Method_(computer_science)http://en.wikipedia.org/wiki/Superclass_(computer_science)http://en.wikipedia.org/wiki/Method_signaturehttp://en.wikipedia.org/wiki/Parameter_(computer_science)http://en.wikipedia.org/wiki/Parameter_covariancehttp://en.wikipedia.org/wiki/Polymorphism_(computer_science)http://en.wikipedia.org/wiki/Object-orientedhttp://en.wikipedia.org/wiki/Computer_programhttp://en.wikipedia.org/wiki/Programmer
  • 8/12/2019 39377219-notes-of-c

    16/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    In many cases, abstract classes are designed Q i.e. classes that e6ist only in order tohave speciali?ed subclasses derived from them. uch abstract classes have methods

    that do not perform any useful operations and are meant to be overridden by specificimplementations in the subclasses. #hus, the abstract superclass defines a commoninterface which all the subclasses inherit.

    Q.1! %hat is operator o,erloading&

    In computer programming, operator overloading (less commonly

  • 8/12/2019 39377219-notes-of-c

    17/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    int main$(8h,ee: a1? F? G$)

    cout

  • 8/12/2019 39377219-notes-of-c

    18/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    77 notice o,de, of o'e,ands tem'.& + & o'F.&) tem'. + o'F.)

    return tem')*

    77 4e,load asi-nment fo, =oint.=oint =ointo'e,ato,+=oint o'F$( & + o'F.&) + o'F.)

    return t!is) 77 i.e.? ,etu,n o/Cect that -ene,ated call*

    77 4e,load ',efi& 55 fo, =oint.=oint =ointo'e,ato,55$( &55)

    55)

    return t!is)*

    int main$( =oint o/110? F0$? o/F J? G0$? o/GK0? K0$)

    o/1.sho%$) o/F.sho%$)

    55o/1) o/1.sho%$) 77 dis'las 11 F1

    o/F + 55o/1) o/1.sho%$) 77 dis'las 1F FF o/F.sho%$) 77 dis'las 1F FF

    o/1 + o/F + o/G) 77 multi'le assi-nment o/1.sho%$) 77 dis'las K0 K0 o/F.sho%$) 77 dis'las K0 K0

    return 0)*

    Q.1 Explain 9inary operator o,erloading %ith help of example&

    use above e6ample (Write a program for addition of comple6 number)

    Q.1 Explain O,erloading arithmetic %ith help of example&

    #include usin names'ace std)

    Prof. Bhanudas satam Genesis -7

  • 8/12/2019 39377219-notes-of-c

    19/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    class 8h,ee: (int &? ? B)

    public8h,ee:$ ( & + + B + 0) *8h,ee:int i? int C? int D$ ( & + i) + C) B + D) *

    8h,ee: o'e,ato,58h,ee: o'F$) 77 o'1 is im'lied8h,ee: o'e,ato,+8h,ee: o'F$) 77 o'1 is im'lied8h,ee: o'e,ato,8h,ee: o'F$) 77 o'1 is im'liedvoid sho%$ )

    *)77 4e,load su/t,action.8h,ee: 8h,ee:o'e,ato,8h,ee: o'F$(8h,ee: tem')

    tem'.& + & o'F.&)tem'. + o'F.)tem'.B + B o'F.B)return tem')

    *77 4e,load 5.8h,ee: 8h,ee:o'e,ato,58h,ee: o'F$(8h,ee: tem')

    tem'.& + & 5 o'F.&)tem'. + 5 o'F.&)tem'.B + B 5 o'F.B)return tem')

    *

    77 4e,load assi-nment.8h,ee: 8h,ee:o'e,ato,+8h,ee: o'F$(& + o'F.&) + o'F.)B + o'F.B)return t!is)

    *

    77 Sho% X? Y? Z coo,dinates.void 8h,ee:sho%$(cout

  • 8/12/2019 39377219-notes-of-c

    20/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    cout

  • 8/12/2019 39377219-notes-of-c

    21/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    !o& fi,st!o&1O.0? 11.0? J.0$)

    i"fi,st!o& < 100000$( cout

  • 8/12/2019 39377219-notes-of-c

    22/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    In ob:ect!oriented programming, a virtual function or virtual method is one whosebehavior can be overriddenwithin an inheriting class by a function with the same

    signature. #his concept is a very important part of the polymorphismportion of ob:ect!oriented programming(99/).

    #he concept of the virtual function solves the following problem

    In 99/ when a derived class inherits from a base class, an ob:ect of the derived class

    may be referred to (or cast) as either being the base class type or the derived classtype. If there are base class functions overridden by the derived class, a problem thenarises when a derived ob:ect has been cast as the base class type. When a derivedob:ect is referred to as being of the baseAs type, the desired function call behavior is

    ambiguous.

    #he distinction between virtual and not virtual resolves this ambiguity. If the function

    in ;uestion is designated 5virtual5 in the base class then the derived classAs functionwould be called (if it e6ists). If it is not virtual, the base classAs function would be

    called.

    Rirtual functions overcome the problems with the type!field solution by allowing the

    programmer to declare functions in a base class that can be redefined in each derivedclass.

    $ pure virtual function or pure virtual method is a virtual function that is re;uired to beimplemented by a derived class that is not abstract. Classes containing pure virtualmethods are termed 5abstract45 they cannot be instantiated directly, and a subclassofan abstract class can only be instantiated directly if all inherited pure virtual methods

    have been implemented by that class or a parent class. /ure virtual methods typically

    have a declaration(signature) and no definition (implementation).$s an e6ample, an abstract base class 5@athymbol5 may provide a pure virtualfunction do9peration(), and derived classes 5/lus5 and 5@inus5 implement

    do9peration() to provide concrete implementations. Implementing do9peration()would not ma

  • 8/12/2019 39377219-notes-of-c

    23/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    In C++, pure virtual functions are declared using a special synta6 TU as

    demonstrated below.

    class $bstract 3

    public

    virtual void pure&virtual() 484

    #he pure virtual function declaration provides only the prototype of the method.$lthough an implementation of the pure virtual function is typically not provided in anabstract class, it may be included, although the definition may not be included at thepoint of declaration T'U. %very non!abstract child class is still re;uired to override the

    method, but the implementation provided by the abstract class may be called in thisway

    void $bstractpure&virtual() 3

    do something8

    class Child public $bstract 3 virtual void pure&virtual()4 no longer abstract, this class may be instantiated.84

    void Childpure&virtual() 3

    $bstractpure&virtual()4 the implementation in the abstract class is e6ecuted

    8

    Q.22 Explain dynamic allocation operators&

    In computer science, dynamic memory allocation is the allocation of memorystorage

    for use in a computer programduring the runtimeof that program.

    #he answer is dynamic memory, for which C++ integrates the operators new and

    delete.

    Operators ne% and ne%:;

    In order to re;uest dynamic memory we use the operator new. new is followedby a data type specifier and !if a se;uence of more than one element is re;uired! the

    number of these within brac

  • 8/12/2019 39377219-notes-of-c

    24/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    #he first e6pression is used to allocate memory to contain one single element of typetype. #he second one is used to assign a bloc< (an array) of elements of type type,

    where number&of&elements is an integer value representing the amount of these. "or

    Prof. Bhanudas satam Genesis -!3

  • 8/12/2019 39377219-notes-of-c

    25/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    e6ample

    int /o//)/o// + ne%int2J3)

    In this case, the system dynamically assigns space for five elements of type int andreturns a pointer to the first element of the se;uence, which is assigned to bobby.

    #herefore, now, bobby points to a valid bloc< of memory with space for five elementsof type int.

    #he first element pointed by bobby can be accessed either with the e6pression

    bobbyTU or the e6pression Vbobby. >oth are e;uivalent as has been e6plained in thesection about pointers. #he second element can be accessed either with bobbyT'U orV(bobby+') and so on...

    Nou could be wondering the difference between declaring a normal array and assigningdynamic memory to a pointer, as we have :ust done. #he most important difference is

    that the si?e of an array has to be a constant value, which limits its si?e to what wedecide at the moment of designing the program, before its e6ecution, whereas thedynamic memory allocation allows us to assign memory during the e6ecution of the

    program (runtime) using any variable or constant value as its si?e.

    #he dynamic memory re;uested by our program is allocated by the system from thememory heap. However, computer memory is a limited resource, and it can be

    e6hausted. #herefore, it is important to have some mechanism to chec< if our re;uestto allocate memory was successful or not.

    C++ provides two standard methods to chec< if the allocation was successful

    9ne is by handling e6ceptions. Ssing this method an e6ception of type bad&alloc isthrown when the allocation fails. %6ceptions are a powerful C++ feature e6plained later

    in these tutorials. >ut for now you should

  • 8/12/2019 39377219-notes-of-c

    26/33

  • 8/12/2019 39377219-notes-of-c

    27/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    cout > '2n3) * cout

  • 8/12/2019 39377219-notes-of-c

    28/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    Q.2! %hat is an interface&

    In the Kava programming language, an interface is a reference type, similar to a class,

    that can contain only constants, method signatures, and nested types. #here are nomethod bodies. Interfaces cannot be instantiatedQthey can only be implemented byclasses or e6tended by other interfaces.

    Defining an interface is similar to creating a new class

    'u/lic inte,face 'e,atea, (

    77 constant decla,ations? if an

    77 method si-natu,es int tu,n:i,ection di,ection? 77 An enum %ith 4alues RIQ98? EL8 dou/le ,adius? dou/le sta,tS'eed? dou/le endS'eed$)

    int chan-eanes:i,ection di,ection? dou/le sta,tS'eed? dou/le endS'eed$) int si-nal8u,n:i,ection di,ection? /oolean si-naln$) int -etRada,L,ontdou/le distance8oa,? dou/le s'eedfa,$) int -etRada,Rea,dou/le distance8oa,? dou/le s'eedfa,$) ...... 77 mo,e method si-natu,es*

    ote that the method signatures have no braces and are terminated with a semicolon.

    Q.2$ Explain string handling in ength#he length of a string is the number of characters that it contains. #o obtain this value,call the length( ) method, shown here

    int length( )

    #he following fragment prints X, since there are three characters in the string s

    char charsTU 3 AaA, AbA, AcA 84

    tring s new tring(chars)4ystem.out.println(s.length())4

    /tring Concatenation

    "or e6ample, the following fragment concatenates three strings

    tring age 554

    tring s 5He is 5 + age + 5 years old.54ystem.out.println(s)4

    Prof. Bhanudas satam Genesis -!7

  • 8/12/2019 39377219-notes-of-c

    29/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    #his displays the string He is years old.X9ne practical use of string concatenation is found when you are creating very long

    strings. Instead of letting long strings wrap around within your source code, you canbrea< them into smaller pieces, using the + to concatenate them.

    Here is an e6ample

    Ssing concatenation to prevent long lines.class ConCat 3

    public static void main(tring argsTU)3

    tring longtr 5#his could have been 5 +5a very long line that would have 5 +5wrapped around. >ut string concatenation 5 +5prevents this.54

    ystem.out.println(longtr)48

    8

    Q.2* Explain the stream nputOutput classes in

  • 8/12/2019 39377219-notes-of-c

    30/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    Prof. Bhanudas satam Genesis -29

  • 8/12/2019 39377219-notes-of-c

    31/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    Output/tream

    9utputtream is an abstract class that defines streaming byte output. $ll of the

    methods in this class return a void value and throw an I9%6ception in the case oferrors.

    method Description

    int write (byte bufferTU ) Writes a complete array of bytes to an output

    stream.

    void close( ) Closes the output stream. "urther write attempts

    will generate an I9%6ception.

    Q.2 %hat is an applet& Explain the s=eleton of applet&

    $n applet is a window!based program. $s such, its architecture is different from theso!called normal, console!based programs.

    -n -pplet /=eleton

    $ll but the most trivial applets override a set of methods that provides the basicmechanism by which the browser or applet viewer interfaces to the applet and controlsits e6ecution. "our of these methodsQinit( ), start( ), stop( ), and destroy( )Qare

    defined by $pplet. $nother, paint( ), is defined by the $W# Component class. Defaultimplementations for all of these methods are provided. $pplets do not need to override

    those methods they do not use. However, only very simple applets will not need todefine all of them. #hese five methods can be assembled into the s

  • 8/12/2019 39377219-notes-of-c

    32/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    the applet is restarted. V

    Prof. Bhanudas satam Genesis -2!

  • 8/12/2019 39377219-notes-of-c

    33/33

    Notes C++ /Java- S.Y.Bsc. Section I Genesis

    public void start() 3

    start or resume e6ecution8

    Called when the applet is stopped.

    public void stop() 3 suspends e6ecution

    8

    V Called when applet is terminated. #his is the last method e6ecuted. V

    public void destroy() 3 perform shutdown activities

    8

    Called when an appletAs window must be restored.public void paint(=raphics g) 3

    redisplay contents of window8

    8

    $lthough this s