OOP3_1

Embed Size (px)

Citation preview

  • 8/11/2019 OOP3_1

    1/76

    GVC Ths. Nguyn Minh o

    BM CNPM - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    2/76

    C# is a simple, modern, general-purpose,oriented programming language developMicrosoft within its .NET initiative led by AHejlsberg.

    This tutorial will teach you basic C# prograand will also take you through various advconcepts related to C# programming languag

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    3/76

    STRINGS

    In C#, you can use strings as array of characters, however, mopractice is to use thestring keyword to declare a string variablekeyword is an alias for the System.String class.

    3

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    4/76

    You can create string object using one of thefollowing methods:By assigning a string literal to a Stringvariable

    By using a String class constructor

    By using the string concatenation operator (+)

    By retrieving a property or calling a method threturns a string

    By calling a formatting method to convert a valobject to its string representation

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    5/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    6/76

    The String class has the following two properties:

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    7/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    8/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    9/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    10/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    11/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    12/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    13/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    14/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    15/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    16/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    17/76

    STRUCTURES

    In C#, a structure is a value type data type. It helps you to mavariable hold related data of various data types. Thestruct keywfor creating a structure.

    17

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    18/76

    Structures are used to represent a record. Suppose ywant to keep track of your books in a library. You miwant to track the following attributes about each bo

    Title

    Author

    Subject Book ID

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    19/76

    To define a structure, you must use the struct staThe struct statement defines a new data type, withan one member for your program.

    For example, here is the way you would declare thstructure:

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    20/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    21/76

    You have already used a simple structure namedStructures in C# are quite different from that in traC or C++. The C# structures have the following featStructures can have methods, fields, indexers, pro

    operator methods, and events.

    Structures can have defined constructors, bdestructors. However, you cannot define aconstructor for a structure. The default constrautomatically defined and can't be changed.

    Unlike classes, structures cannot inherit other stror classes.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    22/76

    You have already used a simple structure namedStructures in C# are quite different from that in traC or C++. The C# structures have the following feat

    Structures cannot be used as a base for other stror classes.

    A structure can implement one or more interfacesStructure members cannot be specified as avirtual, or protected.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    23/76

    You have already used a simple structure namedStructures in C# are quite different from that in traC or C++. The C# structures have the following feat

    When you create a struct object using theNew oit gets created and the appropriate constructor is

    Unlike classes, structs can be instantiated withouthe New operator.

    If the New operator is not used, the fields willunassigned and the object cannot be used untilfields are initialized.

    GVC ThS. Nguyn Minh

    o - Khoa CNTT -

    HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    24/76

    Classes and Structures have the followingdifferences:

    classes are reference types and structs aretypes

    structures do not support inheritancestructures cannot have default constructor

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    25/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    26/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    27/76

    ENUMS

    An enumeration is a set of named integer constants. An enumerdeclared using the enum keyword.

    C# enumerations are value data type. In other words, enumerationown values and cannot inherit or cannot pass inheritance.

    27

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    28/76

    ENUM

    The general syntax for declaring an enumeration is:

    Where, The enum_name specifies the enumeration type name.

    The enumeration list is a comma-separated list of identifier

    Each of the symbols in the enumeration list stands for anvalue, one greater than the symbol that precedes it. Bythe value of the first enumeration symbol is 0.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    29/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    ENUM

  • 8/11/2019 OOP3_1

    30/76

    CLASSES

    When you define a class, you define a blueprint for a data type. This dodefine any data, but it does define what the class name means, that is, whathe class will consist of and what operations can be performed on such an oare instances of a class. The methods and variables that constitute a clamembers of the class.

    30

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    31/76

    A class definition startswith the keyword classfollowed by the classname; and the class body,enclosed by a pair of curly

    braces.

    Following is the generalform of a class definition:

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    32/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    33/76

    Please note that,Access specifiers specify the access rules for the memwell as the class itself, if not mentioned then the defauspecifier for a class type is internal. Default accessmembers isprivate.

    Data type specifies the type of variable, and retu

    specifies the data type of the data, the method returns To access the class members, you will use the dot (.) o

    The dot operator links the name of an object with thea member.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    34/76

    GVC ThS. Nguyn Minh o - KhoaCNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    35/76

    A member function of a class is a function thatdefinition or its prototype within the class definitany other variable. It operates on any object of the

    which it is a member, and has access to all the mema class for that object.

    Member variables are attributes of an object (fromperspective) and they are kept private to impencapsulation. These variables can only be accessethe public member functions.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    36/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    37/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    38/76

    A classconstructor is a special member funca class that is executed whenever we creaobjects of that class.

    A constructor will have exact same name

    class and it does not have any return type

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    39/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    40/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    41/76

    A default constructor does not haveparameter but if you need a constructor caparameters.

    Such constructors are called paramet

    constructors. This technique helps you toinitial value to an object at the time of its crea

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    42/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    43/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    44/76

    Adestructor is a special member function ofthat is executed whenever an object of its clasout of scope. Adestructor will have exact samas the class prefixed with a tilde (~) and it canreturn a value nor can it take any parameters.

    Destructor can be very useful for releasing resbefore coming out of the program like closinreleasing memories etc. Destructors canninherited or overloaded.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    45/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    46/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    47/76

    We can define class members as staticthestatic keyword. When we declare a member of astatic, it means no matter how many objects of the ccreated, there is only one copy of the static member.

    The keyword static implies that only one instancemember exists for a class. Static variables are u

    defining constants because their values can be retriinvoking the class without creating an instance ofvariables can be initialized outside the member funclass definition. You can also initialize static variablethe class definition.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    48/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    49/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    50/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    51/76

    INHERITANCE

    One of the most important concepts in object-oriented programminheritance. Inheritance allows us to define a class in terms of anwhich makes it easier to create and maintain an application. This aan opportunity to reuse the code functionality and fast implementati

    51

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    52/76

    When creating a class, instead of writing comnew data members and member functionprogrammer can designate that the new classinherit the members of an existing class. This eclass is called the base class, and the new creferred to as thederived class.

    The idea of inheritance implements thA relationship. For example, mammal IS AdogIS-A mammal hence dogIS-A animal as wso on.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    53/76

    A class can be derived from more than one cinterface, which means that it can inherit dafunctionsfrommultiple base classor interface.

    The syntax used in C# for creating derived classfollows:

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    54/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    55/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    56/76

    The derived class inherits the base class mvariables and member methods. Therefosuper class object should be created befosubclass is created.

    You can give instructions for supinitialization in the member initialization list.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    57/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    58/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    59/76

    C# does not support multiple inherHowever, you can use interfaces to impmultiple inheritance.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    60/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    61/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    62/76

    POLYMORPHISM

    The word polymorphism means having many forms. In obje

    programming paradigm,polymorphism is often expressed as 'on

    multiple functions'.

    62

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    63/76

    The word polymorphism means havingforms. In object-oriented programming parpolymorphism is often expressed as 'one intmultiple functions'.

    Polymorphism can be static or dynamic. In

    polymorphism the response to a functdetermined at the compile time. In dypolymorphism , it is decided at run-time.

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    64/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    The mechanism of linking a function with anduring compile time is called early bindinalso called static binding.

    C# provides two techniques to implementpolymorphism.These are:

    Function overloading

    Operator overloading

  • 8/11/2019 OOP3_1

    65/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    You can have multiple definitions for thefunction name in the same scope.

    The definition of the function must differ fromother by the types and/or the number of arguin the argument list.

    You cannot overload function declarationdiffer only by return type.

  • 8/11/2019 OOP3_1

    66/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    67/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    68/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    C# allows you to create abstract classes thused to provide partial class implementationinterface. Implementation is completed wderived class inherits from it.

    Abstract classes contain abstract methods,

    are implemented by the derived class. The dclasses have more specialized functionality.

  • 8/11/2019 OOP3_1

    69/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    Please note the following rules about abclasses:

    You cannot create an instance of an abstract cla

    You cannot declare an abstract method outsabstract class

    When a class is declared sealed, it caninherited, abstract classes cannot be declared

    ABSTRACT C

  • 8/11/2019 OOP3_1

    70/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    ABSTRACT C

    ABSTRACT C

  • 8/11/2019 OOP3_1

    71/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    ABSTRACT C

  • 8/11/2019 OOP3_1

    72/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

    When you have a function defined in a clayou want to be implemented in an inhclass(es), you usevirtual functions.

    The virtual functions could be implemdifferently in different inherited class and the

    these functions will be decided at runtime.

    Dynamic polymorphism is implemby abstract classes andvirtual functions.

  • 8/11/2019 OOP3_1

    73/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    74/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    75/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM

  • 8/11/2019 OOP3_1

    76/76

    GVC ThS. Nguyn Minh o - Khoa CNTT -HSPKT TP.HCM