Introduction to Classes and Objects

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

 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.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 4 Introduction to Classes, Objects, Methods and strings
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
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 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.
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.
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
Polymorphism, Interfaces & Operator Overloading
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Object Oriented Programming using Java - Class Instance Variables
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.
Classes & Objects: Examples
3-4-5 Introduction.
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
NAME 436.
Visual Programming Lecture 4.
Introduction to Classes and Objects
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Introduction to Classes and Objects 4 Introduction to Classes and Objects

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.

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

Outline GradeBook.cs

Outline GradeBookTest.cs

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

Outline GradeBook.cs

Outline GradeBookTest.cs

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

Outline GradeBook.cs (1 of 2)

Outline GradeBook.cs (2 of 2)

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.)

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.

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

Outline GradeBookTest.cs (1 of 2)

Outline GradeBookTest.cs (2 of 2)

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

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.

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.

Fig. 4.11 | Reference type variable.

Outline GradeBook.cs (1 of 2)

Outline GradeBook.cs (2 of 2)

Outline GradeBookTest.cs (1 of 2)

Outline GradeBookTest.cs (2 of 2)

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.

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

Outline Account.cs (1 of 2)

Outline Account.cs (2 of 2)

Outline AccountTest.cs (1 of 2)

Outline AccountTest.cs (2 of 2)

Fig. 4.17 | string format specifiers.

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.