Chapter 6 OOP: Creating Object-Oriented ProgramsObjectivesObjectives cont.Why Multi-Tier Architecture?Object-Oriented ProgrammingObjectsAn example of Student Class (UML)Example of an Instance of StudentObject-Oriented TerminologyExample of Expose Properties/Methods/EventsObject-Oriented Terminology cont.Slide 12Slide 13Reusable ClassesMulti-Tier PartitioningPowerPoint PresentationClassesExample code:More Examples:Specifying a NamespaceDesigning Your Own ClassProperty BlocksProperty Blocks cont.Read-Only PropertiesWrite-Only PropertiesClass MethodsConstructors and DestructorsConstructorsOverloading the ConstructorCreating a New Class – Step-by-StepCreating a New Object Using a ClassCreating a New Object Using a Class cont.Defining and Using a New Object – Step-by-Step cont.Slide 34Single-Step the ExecutionShow MeChapter 6OOP: Creating Object-Oriented ProgramsProgramming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.2ObjectivesUse the object-oriented terminology correctlyCreate a two-tier application that separates the user interface from the business logicDifferentiate between a class and an objectCreate a class that has properties and methodsUse property accessor methods to set and retrieve private properties of a class3Objectives cont.Declare object variables and assign values to the properties with a constructor or property accessor methodInstantiate an object in a project using your classDifferentiate between static members and instance membersUnderstand the purpose of the constructor and destructor methodsInherit a new class form your own classApply visual inheritance by deriving a form from another form4Why Multi-Tier Architecture?ReuseEase of MaintenanceChanges to any Layer do not affect other layersEase of DistributionChanges to Business Logic layer immediately available5Object-Oriented ProgrammingObjects have properties and methods and generate eventsClasses for all objects are predefined or defined by a programmerTemplate – cookie cutter analogyClasses must be created before objects can be createdObject-oriented programming (OOP) is currently the most accepted style of programming6ObjectsCreate a new object type by creating a new class with properties and methodsProperties are characteristicsMethods are actionsTo create a new object you instantiate an object of the classThe new object is an instance of the classAn instance of a class (an Object) properties have unique valuesTo call a method, refer to Object.Method7An example of Student Class (UML)StudentstudentNumberstudentNamemarkForMathsmarkForEnglishmarkForScienceenterMarkscalculateAverageMarkPropertie sMethodsClass8Example of an Instance of StudentStudentstudentNumber = 765906studentName = Bill SamsonmarkForMath = 85markForEnglish = 90markForScience = 60enterMarkscalculateAverageMarkPropertie sMethodsInstance9Object-Oriented TerminologyEncapsulation is the combination of characteristics of an object along with its behaviorsOne “package” holds the definition of all properties, methods, and eventsCannot make up new properties or methodsAlso called data hidingCreator of the class decides which properties/methods/events to expose to the user (programmer)Private hides properties/methods/eventsPublic exposes them10Example of Expose Properties/Methods/Events11Object-Oriented Terminology cont.Inheritance is the ability to create a new class from an existing classCreate a new class from an existing class to add or modify class variables and methodsOriginal class is the base class, superclass, or parent classInherited class is the subclass, derived class, or child class12Object-Oriented Terminology cont.In the statementpublic class frmSample : System.Windows.Forms.Formthe colon (:) indicates inheritance, Systems.Windows.Forms is the base class and frmSample is the derived classThe purpose of inheritance is reusabilityCreate a hierarchy of classes by placing common code in a base class then creating other derived classes from it13Object-Oriented Terminology cont.Polymorphism is the ability to take on many shapes or formsPolymorphism allows methods with the same names to have different implementationsPolymorphism uses overloading and overridingIn overriding, a subclass has a method identically named in the base class, and the subclass method overrides the method in the base class14Reusable ClassesA big advantage of object-oriented programming over traditional programming is the ability to reuse classesUse a new class in multiple projectsSimilar to working with built-in controls in the toolbox15Multi-Tier PartitioningPresentation layer: Contains components dealing with user interfaces and user interactionBusiness logic layer: Contains components that work together to solve business problemsData layer: Used by the business layer to persist state permanently, usually one or more databasesEach function of a multitier application is coded in a separate component1617ClassesUse the new keyword to create an instance of a class which is called instantiating an objectGeneral form is new className();Examples:Font MyFont = new Font(“Arial”,12); <-InstantiationmessageLabel.Font = MyFont; <-AssignmentmessageLabel.Font = new Font(“Arial”,12); <-both18Example code: private System.Windows.Forms.TextBox txtSample; <- Declaring a variable of type textboxthis.txtSample = new System.Windows.Forms.TextBox(); <- Instantiating Assigning property valuesthis.txtSample.Location = new System.Drawing.Point(72, 72); this.txtSample.Name = "txtSample"; this.txtSample.TabIndex = 0; this.txtSample.Text = "textBox1";19More Examples: Student FirstStudent;Student SecondStudent;Creates two variables of type Student – not usable until instantiatedFirstStudent = new Student();SecondStudent = new Student();Now that they are initiated they are usableFirstStudent.studentNumber = “765906”;SecondStudent.studentNumber =“876940”;Assigning property values20Specifying a NamespaceA fully qualified name includes the complete namespace and class nameExample: System.Windows.Forms.TextBox where System.Windows.Forms is the namespaceEntire namespace not required for System, System.Windows.Forms, and System.DrawingAdd a using statement at beginning of code file to specify the namespace to be used then only refer to class name21Designing Your Own ClassAnalyze the characteristics and behaviors that your object needsCharacteristics become propertiesBehaviors become methods22Property BlocksDefine
View Full Document