Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Classes and Objects

Similar presentations


Presentation on theme: "Introduction to Classes and Objects"— Presentation transcript:

1 Introduction to Classes and Objects
4 Introduction to Classes and Objects

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.

3 Introduction Classes, Objects, Methods, Properties and Instance Variables Declaring a Class with a Method and Instantiating an Object of a Class Declaring a Method with a Parameter Instance Variables and Properties UML Class Diagram with a Property Software Engineering with Properties and set and get Accessors Value Types vs. Reference Types Initializing Objects with Constructors Floating-Point Numbers and Type decimal (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document Wrap-Up

4 Outline GradeBook.cs

5 Outline GradeBookTest.cs

6 Fig. 4.3 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation.

7 Outline GradeBook.cs

8 Outline GradeBookTest.cs

9 Fig. 4.6 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation with a courseName parameter of type string.

10 Outline GradeBook.cs (1 of 2)

11 Outline GradeBook.cs (2 of 2)

12 Software Engineering Observation 4.2
Precede every field and method declaration with an access modifier. As a rule of thumb, instance variables should be declared private and methods and properties should be declared public. If the access modifier is omitted before a member of a class, the member is implicitly declared private by default. (We will see that it is appropriate to declare certain methods private, if they will be accessed only by other methods of the class.)

13 Good Programming Practice 4.1
We prefer to list the fields of a class first, so that, as you read the code, you see the names and types of the variables before you see them used in the methods of the class. It is possible to list the class’s fields anywhere in the class outside its method declarations, but scattering them can make code difficult to read.

14 Good Programming Practice 4.2
Placing a blank line between method and property declarations enhances application readability.

15 Outline GradeBookTest.cs (1 of 2)

16 Outline GradeBookTest.cs (2 of 2)

17 Fig. 4.9 | UML class diagram indicating that class GradeBook has a public CourseName property of type string and one public method.

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.

19 Software Engineering Observation 4.4
A variable’s declared type (e.g., int, double or GradeBook) 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 account1 indicates that account1 is a variable that can refer to an Account object.

20 Fig. 4.11 | Reference type variable.

21 Outline GradeBook.cs (1 of 2)

22 Outline GradeBook.cs (2 of 2)

23 Outline GradeBookTest.cs (1 of 2)

24 Outline GradeBookTest.cs (2 of 2)

25 Error-Prevention Tip 4.1 Unless default initialization of your class’s instance variables is acceptable, provide a constructor to ensure that your class’s instance variables are properly initialized with meaningful values when each new object of your class is created.

26 Fig | UML class diagram indicating that class GradeBook has a constructor with a name parameter of type string.

27 Outline Account.cs (1 of 2)

28 Outline Account.cs (2 of 2)

29 Outline AccountTest.cs (1 of 2)

30 Outline AccountTest.cs (2 of 2)

31 Fig. 4.17 | string format specifiers.

32 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 leave the instance variables unchanged and generate an error. We demonstrate how to gracefully generate errors in Chapter 12, Exception Handling.


Download ppt "Introduction to Classes and Objects"

Similar presentations


Ads by Google