t4 - object oriented programming with c 2.0

Upload: huynguyenquang

Post on 05-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    1/64

    C# and .NET Technology

    Object OrientedProgramming with C# 2.0

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    2/64

    C#.NET/ 2 of 106

    Objectives

    Essentials of Object-OrientedProgramming

    Classes and Objects

    Using Encapsulation

    C# and Object Orientation

    Defining Object-Oriented Systems

    The Object Hierarchy

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    3/64

    C#.NET/ 3 of 106

    Objectives

    Use Constructors In C#

    Use Destructors In C#

    Explain the working of Garbage Collector

    Discuss Method Overloading

    Discuss Operator Overloading

    Use Inheritance in C#

    Discuss Overriding

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    4/64

    C#.NET/ 4 of 106

    Objectives

    Discuss Polymorphism

    Use Virtual Functions

    Discuss relationship between polymorphism and

    inheritance Discuss Abstract Base classes

    Discuss the concept of Interfaces

    Use Interfaces Discuss Properties

    Discuss Indexers

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    5/64

    Object OrientedProgramming Overview

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    6/64

    C#.NET/ 6 of 106

    1. Object Oriented ProgrammingHistory

    Object-oriented programming has roots that can betraced to the 1960s:

    As hardware and software became increasinglycomplex, quality was often compromised.

    Researchers studied ways to maintain software qualityand developed object-oriented programming in part toaddress common problems by strongly emphasizingdiscrete, reusable units of programming logic.

    The methodology focuses on data rather thanprocesses, with programs composed of self-sufficientmodules (objects) each containing all the informationneeded to manipulate its own data structure.

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    7/64C#.NET/ 7 of 106

    1. Object Oriented ProgrammingHistory

    The Object-Oriented methodology focuses on datarather than processes, with programs composed ofself-sufficient modules (objects) each containing all theinformation needed to manipulate its own data

    structure. This is in contrast to the existing modular

    programming which had been dominant for manyyears that focused on the functionof a module

    An object-oriented program may thus be viewed as acollection of cooperating objects, as opposed to theconventional model, in which a program is seen as alist of tasks (subroutines) to perform

    http://en.wikipedia.org/wiki/Modular_programminghttp://en.wikipedia.org/wiki/Modular_programminghttp://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Modular_programminghttp://en.wikipedia.org/wiki/Modular_programming
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    8/64C#.NET/ 8 of 106

    2. OOP Fundamental Concepts

    Fundamental Concepts in OOP are: Class Object Instance Method Message passing Inheritance Abstraction

    Encapsulation Polymorphysm De-coupling

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    9/64

    I. Classes and Objects

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    10/64C#.NET/ 10 of 106

    1. What is a class?

    A class define the abstract characteristics of athing (object), including the thing'scharacteristics (its attributes, fields orproperties) and the thing's behaviors (thethings it can do, or methods, operations orfeatures).

    For example, the class Dog would consist oftraits shared by all dogs, such as: Breed and fur color (characteristics), and

    The ability to bark and sit (behaviors)

    http://en.wikipedia.org/wiki/Field_%28computer_science%29http://en.wikipedia.org/wiki/Property_%28programming%29http://en.wikipedia.org/wiki/Method_%28computer_science%29http://en.wikipedia.org/wiki/Method_%28computer_science%29http://en.wikipedia.org/wiki/Property_%28programming%29http://en.wikipedia.org/wiki/Field_%28computer_science%29
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    11/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    12/64C#.NET/ 12 of 106

    An example: A class with its attributes,

    fields, properties, methods,

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    13/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    14/64C#.NET/ 14 of 106

    2.1. Compare Properties and PublicFields

    Properties can be recognizeby IDEs (such as VisualStudio.NET) but public fields

    Properties are the hybrid offields and methods

    A property is like a clock-knob: when you turn it, the

    hour hand and the minutehand and the second handare automatically adjusted

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    15/64C#.NET/ 15 of 106

    2.2. Constructor

    Whenever a class or struct is created, itsconstructor is called. A class or struct mayhave multiple constructors that take differentarguments.

    Constructors enable the programmer to setdefault values, limit instantiation, and writecode that is flexible and easy to read.

    If you do not provide a constructor for yourobject, C# will create one by default that

    instantiates the object and sets membervariables to the default values Static classes and structs can also have

    constructors

    http://msdn.microsoft.com/en-us/library/0b0thckt.aspxhttp://msdn.microsoft.com/en-us/library/ah19swz4.aspxhttp://msdn.microsoft.com/en-us/library/ah19swz4.aspxhttp://msdn.microsoft.com/en-us/library/0b0thckt.aspx
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    16/64C#.NET/ 16 of 106

    2.3. Destructor

    Destructors are used to destruct instances of classes. Destructors cannot be defined in structs. They are only used

    with classes.

    A class can only have one destructor.

    Destructors cannot be inherited or overloaded.

    Destructors cannot be called. They are invokedautomatically.

    A destructor does not take modifiers or have parameters.

    The destructor implicitly calls Finalize on the base class ofthe object.

    http://msdn.microsoft.com/en-us/library/system.object.finalize.aspxhttp://msdn.microsoft.com/en-us/library/system.object.finalize.aspx
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    17/64C#.NET/ 17 of 106

    2.4. Indexers

    Indexers allow you to index a class or astruct instance in the same way as anarray

    An indexer is declared like a propertyexcept that the name of the member isthis followed by a parameter list written

    between the delimiters [ and ]

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    18/64C#.NET/ 18 of 106

    Indexer example

    BEFORE

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    19/64C#.NET/ 19 of 106

    Indexer example

    NOW

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    20/64C#.NET/ 20 of 106

    3. Access Modifiers

    Access modifiers are keywords used to specify the declaredaccessibility of a member or a type. This section introduces the fouraccess modifiers: Public: The public keyword is an access modifier for types and type

    members. Public access is the most permissive access level. There areno restrictions on accessing public members

    Protected: The protected keyword is a member access modifier. A

    protected member is accessible from within the class in which it isdeclared, and from within any class derived from the class that declaredthis member.

    Internal: Internal members are accessible only within files in the sameassembly. A common use of internal access is in component-baseddevelopment because it enables a group of components to cooperate in aprivate manner without being exposed to the rest of the application code.

    Private: The private keyword is a member access modifier. Privateaccess is the least permissive access level. Private members areaccessible only within the body of the class or the struct in which they aredeclared. Nested types in the same body can also access those privatemembers.

    http://msdn.microsoft.com/en-us/library/yzh058ae%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/bcd5672a%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/st6sy9xe%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/st6sy9xe%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/bcd5672a%28VS.71%29.aspxhttp://msdn.microsoft.com/en-us/library/yzh058ae%28VS.71%29.aspx
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    21/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    22/64C#.NET/ 22 of 106

    partial class definition example

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    23/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    24/64

    C#.NET/ 24 of 106

    4. What is an Object?

    An Object is a pattern of a class.

    Exp: The class of Dog defines all

    possible dogs by listing thecharacteristics and behaviors they canhave; the object Lassie is one particular

    dog, with particular versions of thecharacteristics. A Dog has fur; Lassiehas brown-and-white fur.

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    25/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    26/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    27/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    28/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    29/64

    C#.NET/ 29 of 106

    7. static keyword example

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    30/64

    C#.NET/ 30 of 106

    7. static keyword

    The static modifier on a class means that the classcannot be instantiated, and that all of its members arestatic.

    A static member has one version regardless of howmany instances of its enclosing type are created.

    A constant is implicitly a static member.

    A static member cannot be referenced through aninstance. Instead, it is referenced through the typename

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    31/64

    C#.NET/ 31 of 106

    8. Nested Classes

    A class defined within a class or struct iscalled a nested class

    http://msdn.microsoft.com/en-us/library/0b0thckt%28VS.80%29.aspxhttp://msdn.microsoft.com/en-us/library/ah19swz4%28VS.80%29.aspxhttp://msdn.microsoft.com/en-us/library/ah19swz4%28VS.80%29.aspxhttp://msdn.microsoft.com/en-us/library/0b0thckt%28VS.80%29.aspx
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    32/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    33/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    34/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    35/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    36/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    37/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    38/64

    C#.NET/ 38 of 106

    Inheritance - Examples

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    39/64

    C#.NET/ 39 of 106

    2. Single and Multiple inheritances

    Single inheritance: deriving from onebase class

    Multiple inheritance: deriving from two ormore base classes

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    40/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    41/64

    C#.NET/ 41 of 106

    4. Operator Over-loading

    Like C++, C# allows you to overload operatorsfor use on your own classes. This makes itpossible for a user-defined data type to look asnatural and be as logical to use as a

    fundamental data type

    To overload an operator, you write a functionthat has the name operator followed by thesymbol for the operator to be overloaded

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    42/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    43/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    44/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    45/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    46/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    47/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    48/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    49/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    50/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    51/64

    C#.NET/ 51 of 106

    8. new keyword examples

    In the following example, new keyword

    is used as a modifier:

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    52/64

    C#.NET/ 52 of 106

    9. Why use inheritances?

    Enables a series of classes to be relatedtogether according to their commonparts:

    common members only specified once, inbase class

    can be accessed in objects of all classesderived from the base class

    Increases efficiency of production andflexibility

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    53/64

    IV. Polymorphism

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    54/64

    C#.NET/ 54 of 106

    1. What is the polymorphism?

    Polymorphism is derived from two Greek words. Poly (meaningmany) and morph (meaning forms). Polymorphism means manyforms

    Understanding Polymorphism is crucial to any OO languageprofessional , be it a Java , C++ or C# programmer

    If a Dog is commanded to speak(), it may emit a bark, while if aPig is asked to speak(), it may respond with an oink. Both inheritspeak() from Animal, but their subclass methods override themethods of the superclass, known as overriding polymorphism

    We implement polymorphism by using virtual and overridekeywords

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    55/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    56/64

    C#.NET/ 56 of 106

    3. override and virtual keywords

    override and virtual keywords are used toimplement polymorphism in C#.NET

    The override modifier is required to extend ormodify the abstract or virtual implementation ofan inherited method, property, indexer, orevent

    The virtual keyword is used to modify amethod, property, indexer, or event declarationand allow for it to be overridden in aderived class

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    57/64

    C#.NET/ 57 of 106

    4. Why polymorphism?

    Why include polymorphism in aprogramming language?

    Because it significantly reducesprogramming effort. In particular,polymorphism allows you to write one

    method for a variety of types, rather thanto write one method per type

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    58/64

    V. Abstractions:Abstract Base Classes &

    Interfaces

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    59/64

    C#.NET/ 59 of 106

    1. What is the abstraction?

    Abstraction (from the Latin abs, meaning awayfromand trahere, meaning to draw) is theprocess of taking away or removing

    characteristics from something in order toreduce it to a set of essential characteristics

    In object-oriented programming, abstraction isone of three central principles (along with

    encapsulation and inheritance)

    http://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.htmlhttp://searchnetworking.techtarget.com/sDefinition/0,,sid7_gci212060,00.htmlhttp://searchcio-midmarket.techtarget.com/sDefinition/0,,sid183_gci212351,00.htmlhttp://searchcio-midmarket.techtarget.com/sDefinition/0,,sid183_gci212351,00.htmlhttp://searchnetworking.techtarget.com/sDefinition/0,,sid7_gci212060,00.htmlhttp://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.htmlhttp://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.htmlhttp://searchsoa.techtarget.com/sDefinition/0,,sid26_gci212681,00.html
  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    60/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    61/64

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    62/64

    4 Why must make everything

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    63/64

    C#.NET/ 63 of 106

    4. Why must make everythingabstract?

    The importance of abstraction is derived fromits ability to hide irrelevant details and from theuse of names to reference objects.

    Abstraction is essential in the construction ofprograms. It places the emphasis on what anobject is or does rather than how it isrepresented or how it works. Thus, it is the

    primary means of managing complexity inlarge programs

    5. Differences between Abstract

  • 7/31/2019 T4 - Object Oriented Programming With C 2.0

    64/64

    5. Differences between AbstractClasses Vs. Interfaces

    The biggest difference between anabstract class and an interface is: Aclass may implement an unlimited

    number of interfaces, but may inheritfrom only one abstract