 2006 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

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 Introduction.
 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.
 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.
Writing Classes (Chapter 4)
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
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 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
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.
 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 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:
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
 2007 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
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: 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
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:

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

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

 2006 Pearson Education, Inc. All rights reserved. 3 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 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 set and get 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

 2006 Pearson Education, Inc. All rights reserved. 5 Outline GradeBook.cs

 2006 Pearson Education, Inc. All rights reserved. 6 Outline GradeBookTest.cs

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

 2006 Pearson Education, Inc. All rights reserved. 8 Outline GradeBook.cs

 2006 Pearson Education, Inc. All rights reserved. 9 Outline GradeBookTest.cs

 2006 Pearson Education, Inc. All rights reserved. 10 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. 11 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. 12 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. 13 Fig. 4.6 | UML class diagram indicating that class GradeBook has a public DisplayMessage operation with a courseName parameter of type string.

 2006 Pearson Education, Inc. All rights reserved. 14 Outline GradeBook.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 15 Outline GradeBook.cs (2 of 2)

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

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

 2006 Pearson Education, Inc. All rights reserved. 18 Good Programming Practice 4.2 Placing a blank line between method and property declarations enhances application readability.

 2006 Pearson Education, Inc. All rights reserved. 19 Outline GradeBookTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline GradeBookTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 21 Fig. 4.9 | UML class diagram indicating that class GradeBook has a public CourseName property of type string and one public method.

 2006 Pearson Education, Inc. All rights reserved. 22 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. 23 Fig | Value type variable.

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

 2006 Pearson Education, Inc. All rights reserved. 25 Fig | Reference type variable.

 2006 Pearson Education, Inc. All rights reserved. 26 Outline GradeBook.cs (1 of 2)

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

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

 2006 Pearson Education, Inc. All rights reserved. 29 Outline GradeBookTest.cs (2 of 2)

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

 2006 Pearson Education, Inc. All rights reserved. 31 Fig | UML class diagram indicating that class GradeBook has a constructor with a name parameter of type string.

 2006 Pearson Education, Inc. All rights reserved. 32 Common Programming Error 4.3 Using floating-point numbers in a manner that assumes they are represented precisely can lead to logic errors.

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

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

 2006 Pearson Education, Inc. All rights reserved. 35 Outline AccountTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 36 Outline AccountTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 37 Fig | string format specifiers.

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

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

 2006 Pearson Education, Inc. All rights reserved. 40 Fig | 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. 41 Fig | Nouns and noun phrases in the requirements document.

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

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

 2006 Pearson Education, Inc. All rights reserved. 44 Fig | Multiplicity types.

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

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

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

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