1 4 Introduction to Classes and Objects 2006

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

2 OBJECTIVES In this chapter you will learn: § What classes, objects, methods and instance variables 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 the methods 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 data is initialized when the object is created. § The differences between value types and reference types. 2006 Pearson Education, Inc. All rights reserved.

3 4. 1 Introduction 4. 2 Classes, Objects, Methods, Properties and Instance Variables 4. 3 Declaring a Class with a Method and Instantiating an Object of a Class 4. 4 Declaring a Method with a Parameter 4. 5 Instance Variables and Properties 4. 6 UML Class Diagram with a Property 4. 7 Software Engineering with Properties and Accessors 4. 8 Value Types vs. Reference Types 4. 9 Initializing Objects with Constructors 4. 10 Floating-Point Numbers and Type decimal 4. 11 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 4. 12 Wrap-Up set and get 2006 Pearson Education, Inc. All rights reserved.

Outline 4 Grade. Book. cs 2006 Pearson Education, Inc. All rights reserved.

Outline 5 Grade. Book. Test. cs 2006 Pearson Education, Inc. All rights reserved.

6 Fig. 4. 3 | UML class diagram indicating that class Grade. Book has a public Display. Message operation. 2006 Pearson Education, Inc. All rights reserved.

Outline 7 Grade. Book. cs 2006 Pearson Education, Inc. All rights reserved.

Outline 8 Grade. Book. Test. cs 2006 Pearson Education, Inc. All rights reserved.

9 Software Engineering Observation 4. 1 Normally, objects are created with new. One exception is a string literal that is contained in quotes, such as "hello". String literals are references to string objects that are implicitly created by C#. 2006 Pearson Education, Inc. All rights reserved.

10 Common Programming Error 4. 1 A compilation error occurs if the number of arguments in a method call does not match the number of parameters in the method declaration. 2006 Pearson Education, Inc. All rights reserved.

11 Common Programming Error 4. 2 A compilation error occurs if the types of the arguments in a method call are not consistent with the types of the corresponding parameters in the method declaration. 2006 Pearson Education, Inc. All rights reserved.

12 Fig. 4. 6 | UML class diagram indicating that class Grade. Book has a public Display. Message operation with a course. Name parameter of type string. 2006 Pearson Education, Inc. All rights reserved.

Outline 13 Grade. Book. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 14 Grade. Book. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 15 Grade. Book. Test. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 16 Grade. Book. Test. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

17 Fig. 4. 9 | UML class diagram indicating that class Grade. Book has a public Course. Name property of type string and one public method. 2006 Pearson Education, Inc. All rights reserved.

18 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. 2006 Pearson Education, Inc. All rights reserved.

19 Fig. 4. 10 | Value type variable. 2006 Pearson Education, Inc. All rights reserved.

20 Software Engineering Observation 4. 4 A variable’s declared type (e. g. , int, double or Grade. Book) indicates whether the variable is of a value or a reference type. If a variable’s type is not one of the thirteen simple types, or an enum or a struct type (which we discuss in Section 7. 10 and Chapter 16, respectively), then it is a reference type. For example, Account account 1 indicates that account 1 is a variable that can refer to an Account object. 2006 Pearson Education, Inc. All rights reserved.

21 Fig. 4. 11 | Reference type variable. 2006 Pearson Education, Inc. All rights reserved.

Outline 22 Grade. Book. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 23 Grade. Book. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 24 Grade. Book. Test. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 25 Grade. Book. Test. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

26 Fig. 4. 14 | UML class diagram indicating that class Grade. Book has a constructor with a name parameter of type string. 2006 Pearson Education, Inc. All rights reserved.

Outline 27 Account. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 28 Account. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 29 Account. Test. cs (1 of 2) 2006 Pearson Education, Inc. All rights reserved.

Outline 30 Account. Test. cs (2 of 2) 2006 Pearson Education, Inc. All rights reserved.

31 Fig. 4. 17 | string format specifiers. 2006 Pearson Education, Inc. All rights reserved.

32 Fig. 4. 18 | UML class diagram indicating that class Account has a public Balance property of type decimal, a constructor and a method. 2006 Pearson Education, Inc. All rights reserved.

33 Fig. 4. 19 | Nouns and noun phrases in the requirements document. 2006 Pearson Education, Inc. All rights reserved.

34 Fig. 4. 20 | Representing a class in the UML using a class diagram. 2006 Pearson Education, Inc. All rights reserved.

35 Fig. 4. 21 | Class diagram showing an association among classes. 2006 Pearson Education, Inc. All rights reserved.

36 Fig. 4. 22 | Multiplicity types. 2006 Pearson Education, Inc. All rights reserved.

37 Fig. 4. 23 | Class diagram showing composition relationships. 2006 Pearson Education, Inc. All rights reserved.

38 Fig. 4. 24 | Class diagram for the ATM system model. 2006 Pearson Education, Inc. All rights reserved.

39 Fig. 4. 25 | Class diagram showing some composition relationships of a class Car. 2006 Pearson Education, Inc. All rights reserved.

40 Fig. 4. 26 | Class diagram for the ATM system model including class Deposit. 2006 Pearson Education, Inc. All rights reserved.
- Slides: 40