 2007 Pearson Education, Inc. All rights reserved. 1 4 4 Introduction to Classes and Objects.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Jozef Goetz,  2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2015 credits:
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Programming: Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Introduction to Classes and Objects
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Introduction to Classes and Objects
4 Introduction to Classes and Objects.
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Object Oriented Programming in java
Introduction to Classes and Objects
Classes and Objects Systems Programming.
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects

 2007 Pearson Education, Inc. All rights reserved. 2 You will see something new. Two things. And I call them Thing One and Thing Two. — Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility. — Karl Marx Your public servants serve you right. — Adlai E. Stevenson Knowing how to answer one who speaks, To reply to one who sends a message. — Amenemope

 2007 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What classes, objects, methods, instance variables and properties are.  How to declare a class and use it to create an object.  How to implement a class's behaviors as methods.  How to implement a class's attributes as instance variables and properties.  How to call an object's methods to make them perform their tasks.  The differences between instance variables of a class and local variables of a method.  How to use a constructor to ensure that an object's attributes are initialized when the object is created.  The differences between value types and reference types.  How to use properties to ensure that only valid data is placed in attributes.

 2007 Pearson Education, Inc. All rights reserved Introduction 4.2Classes, Objects, Methods and Instance Variables 4.3Declaring a Class with a Method and Instantiating an Object of a Class 4.4Declaring a Method with a Parameter 4.5Instance Variables and Properties 4.6Value Types and Reference Types 4.7Initializing Objects with Constructors 4.8Validating Data with Set Accessors in Properties 4.9(Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 4.10Wrap-Up

 2007 Pearson Education, Inc. All rights reserved Introduction Classes – Properties – Methods – Constructors

 2007 Pearson Education, Inc. All rights reserved Classes, Objects, Methods, Properties and Instance Variables Class provides one or more methods Method represents task in a program – Describes the mechanisms that actually perform its tasks – Hides from its user the complex tasks that it performs – Method call tells method to perform its task Classes contain one or more attributes – Specified by instance variables – Carried with the object as it is used

 2007 Pearson Education, Inc. All rights reserved Declaring a Class with a Method and Instantiating an Object of a Class Class GradeBook – Keyword Public is an access modifier – Class declarations include: Access modifier Keyword Class The corresponding End Class Statement

 2007 Pearson Education, Inc. All rights reserved Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Class GradeBook – Method declarations Keyword Public indicates method is available to the public Keyword Sub indicates that there is no return type Access modifier, name of method, parentheses and return type, comprise the method header Naming convention is to capitalize the first letter of every word

 2007 Pearson Education, Inc. All rights reserved. 9 Outline GradeBook.vb The method header for DisplayMessage() When method is called, it outputs to screen

 2007 Pearson Education, Inc. All rights reserved Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Module GradeBookTest – Any class or module that contains a Main method can be used to execute an application – Visual Basic is extensible Programmers can create new classes – Class instance creation expression Keyword New Then name of class to create and parentheses – Calling a method Object’s name, then dot separator (. ) Then method’s name and parentheses

 2007 Pearson Education, Inc. All rights reserved. 11 Outline GradeBookTest.vb Use class instance creation expression to create object of class GradeBook Call method DisplayMessage using GradeBook object

 2007 Pearson Education, Inc. All rights reserved Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) UML class diagrams (Fig. 4.3) – Top compartment contains name of the class – Middle compartment contains class’s attributes or instance variables – Bottom compartment contains class’s operations or methods Plus sign indicates Public modifier

 2007 Pearson Education, Inc. All rights reserved. 13 Fig. 4.3 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation.

 2007 Pearson Education, Inc. All rights reserved Declaring a Method with a Parameter Method parameters (Fig. 4.4) – Additional information passed to a method – Supplied in the method call with arguments Input method (Fig. 4.5) – Console.ReadLine reads a line of input

 2007 Pearson Education, Inc. All rights reserved. 15 Outline GradeBook.vb Method header DisplayMessage that takes a courseName argument of type String Output message with the argument when method is called

 2007 Pearson Education, Inc. All rights reserved. 16 Outline GradeBookTest.vb Call ReadLine method to read a line of input and assigns it to nameOfCourse Call DisplayMessage with an argument

 2007 Pearson Education, Inc. All rights reserved Declaring a Method with a Parameter (Cont.) Parameters specified in method’s parameter list – Part of method header – Uses a comma-separated list – Keyword ByVal The argument is passed by value UML class diagram (Fig. 4.6) – Parameters specified by parameter name followed by a colon and parameter type

 2007 Pearson Education, Inc. All rights reserved. 18 Fig. 4.6 | UML class diagram indicating that class GradeBook has a DisplayMessage operation with a courseName parameter of type String.

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties Variables declared in the body of method – Called local variables – Can only be used within that method Variables declared in a class declaration – Called fields or instance variables – Each object of the class has a separate instance of the variable

 2007 Pearson Education, Inc. All rights reserved. 20 Outline GradeBook.vb Instance variable courseNameValue Get accessor for property courseNameValue Set accessor for property courseNameValue Calls the Get accessor of property CourseName

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) Predefined constant identifiers – vbCrLf Represents a combination of carriage return and linefeed character Outputting this constant’s value causes subsequent text to display at the beginning of the next line – vbTab A constant that represents a Tab character

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) Private keyword – Used for most instance variables – Private variables and methods are accessible only to methods of the class in which they are declared – Declaring instance variables Private is known as information hiding

 2007 Pearson Education, Inc. All rights reserved. 23 Software Engineering Observation 4.1 Precede every instance variable declaration, method declaration and property declaration with an access modifier. In most cases, instance variables should be declared Private, and methods and properties should be declared Public. If these modifiers are omitted, instance variables are Private by default, and methods and properties are Public by default. (We will see in Section 9.2 that it is appropriate to declare certain methods and properties Private if they should be accessed only by other methods and properties of the class.)

 2007 Pearson Education, Inc. All rights reserved. 24 Software Engineering Observation 4.2 Declaring the instance variables of a class as Private and the methods of the class as Public facilitates debugging, because problems with data manipulations are localized to the class’s methods and properties.

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) Property Declaration – Declaration consist of an access modifier, keyword Property, name with parentheses, and type – Get and Set allows you to access and modify private variables outside of the class, respectively – Contain a Get accessor, Set accessor, or both – After defining a property, you can use it like a variable ( object_Name.Property_Name ) Get and Set Accessors – Get accessor contains a Return statement – Set accessor assigns a value to its corresponding instance variable

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) Default initial value – Provided for all fields not initialized 0 for numeric/value type variables Nothing for String s and reference types

 2007 Pearson Education, Inc. All rights reserved. 27 Outline GradeBookTest.vb Calls the Get accessor of property CourseName Calls the Set accessor of property CourseName Calls the Get accessor of property CourseName

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) UML Diagram – Model properties in the UML as attributes: Public is indicated by the “+” sign > Property’s name “:” property’s type If the property only contains a Get accessor, then place “{ReadOnly}” after the property’s type – Modeling Private instance variables that are not properties: Attribute’s name “:” attribute’s type Private is indicated by the “-” sign

 2007 Pearson Education, Inc. All rights reserved. 29 Fig. 4.9 | UML class diagram indicating that class GradeBook has a courseNameValue attribute of type String, one property and one method.

 2007 Pearson Education, Inc. All rights reserved Instance Variables and Properties (Cont.) Public variable can be read or written by any property or method Private variables can only be access indirectly through the class’s non- Private properties – Class able to control how the data is set or returned – Allows for data validation Properties of a class should use class’s own methods to manipulate the class’s Private instance variables – Creates more robust class

 2007 Pearson Education, Inc. All rights reserved. 31 Software Engineering Observation 4.3 Accessing Private data through Set and Get accessors not only protects the instance variables from receiving invalid values, but also hides the internal representation of the instance variables from that class’s clients. Thus, if representation of the data changes (often, to reduce the amount of required storage or to improve performance), only the properties’ implementations need to change— the clients’ implementations need not change as long as the services provided by the properties are preserved.

 2007 Pearson Education, Inc. All rights reserved Value Types vs. Reference Types Types in Visual Basic – Value (primitive types except String ) Contains a value of that type List of Primitive Types in Appendix L – Reference (sometimes called non-primitive types) Objects Default value of Nothing Used to invoke an object’s methods and properties

 2007 Pearson Education, Inc. All rights reserved. 33 Fig | Value type variable.

 2007 Pearson Education, Inc. All rights reserved. 34 Fig | Reference type variable.

 2007 Pearson Education, Inc. All rights reserved Initializing Objects with Constructors Constructors – Initialize an object of a class – Visual Basic requires a constructor for every class – Visual Basic will provide a default no-argument constructor ONLY when none is provided – Called when keyword New is followed by the class name and parentheses – Constructors can also take arguments – Constructor header similar to regular Sub method header except the name is replaced with keyword New

 2007 Pearson Education, Inc. All rights reserved. 36 Outline GradeBook.vb Constructor to initialize courseNameValue variable

 2007 Pearson Education, Inc. All rights reserved. 37 Outline GradeBookTest.vb Call constructor to create first grade book object Create second grade book object

 2007 Pearson Education, Inc. All rights reserved Initializing Objects with Constructors (Cont.) UML class diagram – Constructors go in third compartment – Place “ >” before New and its arguments – By convention, place constructors first in their compartment

 2007 Pearson Education, Inc. All rights reserved. 39 Fig | UML class diagram indicating that class GradeBook has a constructor that has a name parameter of type String.

 2007 Pearson Education, Inc. All rights reserved Validating Data with Set Accessors in Properties Validations should be made in the Set accessor to check if the data is valid By default, the Get and Set accessor has the same access as the property, however they can vary. String – Length property returns the number of characters in the String – Substring returns a new String object created by copying part of an existing String object – To display a double quote, use two double quotes in a row

 2007 Pearson Education, Inc. All rights reserved. 41 Error-Prevention Tip 4.1 Unless default initialization of your class’s instance variables is acceptable, provide a constructor to ensure that these variables are properly initialized with meaningful values when each new object of your class is created.

 2007 Pearson Education, Inc. All rights reserved. 42 Outline GradeBook.vb (1 of 2 ) Uses the validation provided in the Set accessor

 2007 Pearson Education, Inc. All rights reserved. 43 Outline GradeBook.vb (2 of 2 ) Validation included value is valid! value is invalid: take substring!

 2007 Pearson Education, Inc. All rights reserved. 44 Outline GradeBookTest.vb (1 of 2 ) Argument is too longArgument is valid

 2007 Pearson Education, Inc. All rights reserved. 45 Outline GradeBookTest.vb (2 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 46 Error-Prevention Tip 4.2 The benefits of data integrity are not automatic simply because instance variables are made Private —the programmer must provide appropriate validity checking and report the errors.

 2007 Pearson Education, Inc. All rights reserved. 47 Error-Prevention Tip 4.3 Set accessors that set the values of Private data should verify that the intended new values are proper; if they are not, the Set accessors should place the Private instance variables into an appropriately consistent state.

 2007 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document Begin designing the ATM system – Analyze the nouns and noun phrases – Introduce UML class diagrams

 2007 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) Key nouns and noun phrases in requirements document – Some are attributes of other classes – Some do not correspond to parts of the system – Some are classes To be represented by UML class diagrams

 2007 Pearson Education, Inc. All rights reserved. 50 Fig | Nouns and noun phrases in the requirements document.

 2007 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Top compartment contains name of the class – Middle compartment contains class’s attributes or instance variables – Bottom compartment contains class’s operations or methods

 2007 Pearson Education, Inc. All rights reserved. 52 Fig | Representing a class in the UML using a class diagram.

 2007 Pearson Education, Inc. All rights reserved. 53 Fig | Class diagram showing an association among classes.

 2007 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Allows suppression of class attributes and operations Called an elided diagram – Solid line that connects two classes represents an association numbers near end of each line are multiplicity values

 2007 Pearson Education, Inc. All rights reserved. 55 Fig | Multiplicity types.

 2007 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document (Cont.) UML class diagrams – Solid diamonds attached to association lines indicate a composition relationship – Hollow diamonds indicate aggregation – a weaker form of composition

 2007 Pearson Education, Inc. All rights reserved. 57 Fig | Class diagram showing composition relationships.

 2007 Pearson Education, Inc. All rights reserved. 58 Fig | Class diagram for the ATM system model.

 2007 Pearson Education, Inc. All rights reserved. 59 Fig | Class diagram showing some composition relationships of a class Car.

 2007 Pearson Education, Inc. All rights reserved. 60 Fig | Class diagram for the ATM system model including class Deposit.