Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
OOP-Creating Object-Oriented Programs
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Advanced Object-Oriented Programming Features
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
A First Program Using C#
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 8: Writing Graphical User Interfaces
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
JavaScript, Fourth Edition
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Introduction to Object-Oriented Programming Lesson 2.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object-Oriented Programming: Inheritance and Polymorphism.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Object-Oriented Programming: Classes and Objects.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 1: An Introduction to Visual Basic 2015
About the Presentations
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
Presentation transcript:

Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

6- 2 Objectives Use the object-oriented terminology correctly Create a two-tier application that separates the user interface from the business logic Differentiate between a class and an object Create a class that has properties and methods Use property accessor methods to set and retrieve private properties of a class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Objectives cont. Declare object variables and assign values to the properties with a constructor or property accessor method Instantiate an object in a project using your class Differentiate between static members and instance members Understand the purpose of the constructor and destructor methods Inherit a new class form your own class Apply visual inheritance by deriving a form from another form

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Object-Oriented Programming Objects have properties and methods and generate events Classes for all objects are predefined or defined by a programmer Object-oriented programming (OOP) is currently the most accepted style of programming

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Objects Create a new object type by creating a new class with properties and methods –Properties are characteristics –Methods are actions To create a new object you instantiate an object of the class The new object is an instance of the class To call a method, refer to Object.Method

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Object-Oriented Terminology Encapsulation is the combination of characteristics of an object along with its behaviors –One “package” holds the definition of all properties, methods, and events –Cannot make up new properties or methods –Also called data hiding

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Object-Oriented Terminology cont. Inheritance is the ability to create a new class from an existing class –Create a new class from an existing class to add or modify class variables and methods –Original class is the base class, superclass, or parent class –Inherited class is the subclass, derived class, or child class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Object-Oriented Terminology cont. In the statement public class Form1 : System.Windows.Forms.Form the colon (:) indicates inheritance, Systems.Windows.Forms is the base class and Form1 is the derived class The purpose of inheritance is reusability Create a hierarchy of classes by placing common code in a base class then creating other derived classes from it

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Object-Oriented Terminology cont. Polymorphism is the ability to take on many shapes or forms Polymorphism allows methods with the same names to have different implementations Polymorphism uses overloading and overriding In overriding, a subclass has a method identically named in the base class, and the subclass method overrides the method in the base class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Reusable Classes A big advantage of object-oriented programming over traditional programming is the ability to reuse classes Use a new class in multiple projects Similar to working with built-in controls in the toolbox

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Multitier Applications Each function of a multitier application is coded in a separate component Three-tier applications include: –Presentation tier – Present the user interface –Business tier – Handle the data –Data tier – Retrieve and store data in a database An “n-tier” application is the expansion of three-tier model

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Classes Use the new keyword to create an instance of a class which is called instantiating an object General form is new className(); Examples: Font MyFont = new Font(“Arial”,12); messageLabel.Font = MyFont; messageLabel.Font = new Font(“Arial”,12);

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Specifying a Namespace A fully qualified name includes the complete namespace and class name –Example: System.Windows.Forms.TextBox where System.Windows.Forms is the namespace Entire namespace not required for System, System.Windows.Forms, and System.Drawing Add a using statement at beginning of code file to specify the namespace to be used then only refer to class name

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Designing Your Own Class Analyze the characteristics and behaviors that your object needs –Characteristics become properties –Behaviors become methods

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Property Blocks Define private member variables to store values for the class The class controls access to its properties through accessor methods in a property block –get accessor method used to retrieve a property value –set accessor method used to assign a value to a property set statement uses value keyword to refer to incoming value for property Property block must be public

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Property Blocks cont. Property Block – General Form private DataType MemberVariable; public DataType Property Name { get { return MemberVariable; } set { //statements, such as validation MemberVariable = value; }

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Read-Only Properties Write a property block that contains only a get to create a read-only property private decimal decExtendedPrice; public decimal ExtendedPrice { get { return decExtendedPrice; } {

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Write-Only Properties Write a property block that contains only a s et to create a write-only property private decimal decPrice; public decimal Price { set { decPrice = value; } {

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Class Methods Create methods of the new class by coding public methods within the class Methods declared with private are available only within the class Methods declared with public are available to external objects created from this or other classes

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Constructors and Destructors A constructor is a method that executes automatically when an object is instantiated A destructor is a method that executes automatically when an object is destroyed A constructor has the same name as the class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Constructors Executes before any other code in the class Must be public Empty constructor automatically created when you add a new class to project If class does not have a constructor, the compiler creates a method called the default constructor with an empty argument list

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Overloading the Constructor Create overloaded methods in your class by giving the same name to multiple methods, each with a different argument list A parameterized constructor requires arguments Use the this keyword to refer to the current class in the class code

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Creating a New Class – Step-by-Step In this step-by-step tutorial, you will create a new class to hold book sale information for R ‘n R. 1.Begin the Project 2.Begin a New Class 3.Define the Class Properties 4.Add the Property Blocks 5.Add the Method 6.Add the Constructor 7.Add General Remarks

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Creating a New Object Using a Class Creating a class defines a new type but does not create any objects Two steps to create a new object of a class 1.Declare a variable for the new object 2.Instantiate the object using the new keyword Use public or private followed by the class name and identifier to refer to the object of the class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Creating a New Object Using a Class cont. Declare the variable private BookSale bookSaleObject; Create the object bookSaleObject = new BookSale(); Combine declaration and instantiation of an object in the same statement BookSale bookSaleObject = new BookSale(); Should enclose instantiation in try/catch block if converting and passing values from a textbox

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Defining and Using a New Object – Step-by-Step To continue the step-by-step tutorial for the BookSale class, the next step is to design the form for the user interface. The form has text boxes for the user to enter the title, quantity, and price, a menu choice to calculate the sale (the extended price), and another menu item to exit.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Defining and Using a New Object – Step-by-Step cont. In the Calculate Sale event handler, you will create an instance of the BookSale class and assign the input values for title, quantity, and price to the properties of the BookSale object. The ExtendedPrice property in the BookSale class retrieves the amount of the sale, which appears in a label on the form.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Defining and Using a New Object – Step-by-Step cont. Placing all calculations in a separate class is a good thing. You are seeing your first example of dividing a program into a Presentation tier and a Business tier. 1.Create the Form 2.Add Comments 3.Declare the New Object 4.Write the Code 5.Save Your Work 6.Run the Project

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Single-Step the Execution A quick and easy way to debug is to single- step program execution Must be in break time to single-step –Place a breakpoint in code –Run the program –When program stops at breakpoint, press the F11 key to single-step through

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Instance Variables versus Static Variables Instance members exist once for each object of a class Static members exist once for all objects of a class Methods, variables, and properties can be declared as instance or static Access static members without instantiating an object of the class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Instance Variables versus Static Variables cont. Must reference static members with ClassName.Property or ClassName.Name.Method Use the static keyword to create a static member [public|private] static Datatype VariableName; [public|private] static Datatype MethodName(ArgumentList); Must use static keyword on property block if you will retrieve the property without first creating an instance of the class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Adding Static Properties to the Step-by-Step Tutorial You will now make the BookSale class calculate the total of all sales and a count of the number of sales. You will need static properties for the sales total and sales count in the class. Then on the form, you will add a menu option for Summary that displays the totals in a message box.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Adding Static Properties to the Step-by-Step Tutorial cont. 1.Add Static Properties to the Class 2.Modify the Code to Calculate the Totals 3.Modify the Form 4.Test the Program 5.Save This Version of the Program

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Destructors Write processing to occur when an object goes out of scope in a destructor Destructor is a method with the same name as the class preceded by a tilde (~) Destructor method automatically calls the Object.Finalize method from the base class of the object Destructor called as part of the garbage collection by the CLR

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Garbage Collection Garbage collection feature of.NET CLR cleans up unused components Garbage collector periodically checks for unreferenced objects and releases all memory and system resources used by the objects Microsoft recommends you rely on garbage collection to release resources

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Inheritance A new class can inherit from a.NET class or one of your own classes Inheritance statement format: public class NewClass : BaseClass Inheritance clause must follow the class header before any comments

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Overriding Methods A new method overrides the base class method with the same name Derived class will use the new method, not the method in the base class with the same name To override a method in C# –Declare the original method with the virtual or abstract keyword –Declare the new method with the override keyword

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Overriding Methods cont. Examples: –Base Class protected virtual decimal CalculatedExtendedPrice() –Inherited Class protected overrride decimal CalculatedExtendedPrice() You can use the virtual, abstract, or override keywords on a method that can be overriden –Use virtual when writing a new method that has code –Use abstract for a method header for an empty method –Use override when the method is overriding a method in its base class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Accessing Properties Call the base class constructor from the derived-class constructor After assigning values to properties in the base class, you can refer to the properties in the base class from the derived class Read-only or write-only properties cannot be accessed by name from a derived class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Adding Inheritance to the Step-by-Step Tutorial This continuation of the chapter step-by-step tutorial includes a new subclass, overriding a method, and adding a new property. 1.Add the New Class 2.Add the Constructor 3.Add the New Property 4.Add a Constant 5.Override a Method 6.Allow a Method to be Inherited 7.Modify the Form to Use the Inherited Class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Creating a Base Class Strictly for Inheritance Include the abstract modifier in the class declaration of a base class that will be inherited and methods that will be overridden //Base Class public abstract class BaseClass { public abstract void SomeMethod() { //No code allowed here }

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Inheriting Form Classes Visual inheritance is designing one form then inheriting any other forms from the first Base class inherits from System.Windows.Forms.Form and new forms inherit from the base class Include design elements and controls, and write methods and declare variables in the base class Write methods in base class with virtual or abstract then write method with same name and override keyword in the subclass

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Create an Inherited Form Class 1.Select Project/Add Windows Form and enter name for new form; Modify inheritance clause to inherit from base form using project name as namespace or 2.Select Project/Add Inherited Form and type name of new form; Enter information into dialog box

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Coding for Events of an Inherited Class You cannot double-click on an inherited control to open the event-handling method Instead, you can either copy the method from the base class into the derived class as a start, or use the inherited event handler in the derived class

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Managing Multiclass Projects Form classes must be in separate files Multiple classes can be in one file Each C# file automatically contains a namespace statement –View and modify namespace in the Project Properties dialog box –Modify the namespace property at the top of each existing file Modify the namespace statement if you move or copy a class or form file to a new project or include a using statement in the class file

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Add an Existing Class File to a Project It is best to place the class file in the project folder but not required Select Project/Add Existing Item to add an existing file to a project Modify the namespace statement in the file to match the project

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Displaying Values on a Different Form Reference controls on another form using the identifier for the form instance Write the form reference as FormInstance.ControlName.Property If the form to which you refer is in a different namespace, you must also include a reference to the namespace or include a using statement

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Using the Object Browser The Object Browser will show names of objects, properties, methods, events and constants for: –C# objects –your own objects –objects available from other applications Choose View/Other Windows/Object Browser or drop down the window list from the View toolbar button Choose libraries/namespaces in the Browse list Icons represent properties, methods, events, constants, classes, and namespaces

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Examining C# and Your Own Classes You can see what elements are defined in the class, what is the base class, and which properties, methods, and events are inherited Double-click on a name in the Members list to jump to the definition of any property or method Select a form in the Objects list and double- click on method name to view

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Your Hands-On Programming Example This program must calculate books sales for R ‘n R, with a discount of 15 percent for students. The project will use the BookSale and StudentBookSale classes developed in the chapter step-by-step. Create a project with multiple forms that have a shared design element. Include a main form, an About form, and a Summary form that displays the sales summary information.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Your Hands-On Programming Example cont. Design a base form to use for inheritance and make the other three forms inherit from the base form. The About form and Summary form must have an OK button, which closes the form. The main form will have menus and no OK button.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary Objects have properties and methods and can trigger events. Create a new class that can be used to create new objects. Creating a new object is instantiating the object. The object is an instance of the class. Encapsulation is the combination of the characteristics and behaviors of an item into a single class definition. Polymorphism allows different classes of objects to have similarly named methods that behave differently.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. Inheritance provides a means to derive a new class based on an existing class. A big advantage of OOP is that classes can be reused in many applications. Multitier applications separate functions into tiers. Variables inside a class used to store properties should be private. Use property blocks with accessor methods ( get and set ) to make properties of a class available outside the class.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. Read-only properties have only a get accessor method. Write-only properties have only a set accessor method. Public methods of a class are available to other objects of the class and other classes. Use the new keyword to instantiate an object of a class. A constructor is a method that executes automatically when an object is created. A destructor executes automatically when the object is destroyed.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. A constructor method must be named the same as the class and may be overloaded. A parameterized constructor requires arguments to create a new object. Static members have one copy that is used by all objects of the class. Instance members have one copy for each instance of the object. Garbage collection checks for and destroys unreferenced objects, and releases resources. A subclass inherits all public and protected properties and methods of its base class.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved Summary cont. To override a base class method, it must be declared as virtual or abstract, and the new method must use the override keyword. Use visual inheritance to derive new forms from existing forms. Each project has a default namespace. Modify the namespace statement at the top of existing files. Refer to controls on a different form with form instance name, control name, and property. Use Object Browser to view classes, properties, methods, events, and constants in all classes.