19
ADVANCED PROGRAMMING CHAPTER 4: INTRODUCTION TO INTRODUCTION TO CLASSES, OBJECTS, METHODS AND STRINGS Dr Shahriar Bijani Winter 2016

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

Embed Size (px)

DESCRIPTION

4.2 C LASSES, O BJECTS, M ETHODS, P ROPERTIES AND I NSTANCE V ARIABLES Review of classes: Car example Methods describe the mechanisms that perform a tasks (e.g. acceleration) Hide complex tasks from the user ; a driver does not need to know how the accelerator works but can use it. Classes must be defined before use ; a car must be built before it can be driven Many objects can be created from the same class ; many cars can be built from same engineering diagram

Citation preview

Page 1: 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

ADVANCED PROGRAMMINGCHAPTER 4: INTRODUCTION TO INTRODUCTION TO CLASSES, OBJECTS, METHODS AND STRINGS

Dr Shahriar BijaniWinter 2016

Page 2: 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

2

REFERENCES Visual C# 2012 How to Program, Paul Deitel

& Harvey Deitel, 5th Edition, Prentice Hall.

Page 3: 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

4.2 CLASSES, OBJECTS, METHODS, PROPERTIES AND INSTANCE VARIABLES

Review of classes: Car exampleMethods describe the mechanisms that

perform a tasks (e.g. acceleration)Hide complex tasks from the user; a

driver does not need to know how the accelerator works but can use it.

Classes must be defined before use; a car must be built before it can be driven

Many objects can be created from the same class; many cars can be built from same engineering diagram

Page 4: 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

4.2 CLASSES, OBJECTS, METHODS, PROPERTIES AND INSTANCE VARIABLES

Method calls send messages to an object to perform tasks; pressing the gas pedal sends a message to the car to accelerate

Objects have attributes; cars have color and speed gauge, each car knows how much fuel is in its own tank, but

not how much is in the tanks of other cars.

Properties, get Accessors and set Accessors The attributes are not necessarily accessible

directly. The car manufacturer does not want drivers to access the car’s engine to observe the amount of fuel in its tank.

Page 5: 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

5

A CLASS EXAMPLE1 // Fig. 4.1: GradeBook.cs2 // Class declaration with one method.3 using System;45 public class GradeBook6 {7 // display a welcome message to the GradeBook user89 {10 Console.WriteLine( "Welcome to the Grade Book!" );11 } // end method DisplayMessage12 } // end class GradeBook

Page 6: 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

6

CREATE AND CALL AN OBJECT1 // Fig. 4.2: GradeBookTest.cs2 // Create a GradeBook object and call its DisplayMessage method.3 public class GradeBookTest4 {5 // Main method begins program execution6 public static void Main( string[] args )7 {8 // create a GradeBook object and assign it to myGradeBook9 GradeBook myGradeBook = new GradeBook();1011 // call myGradeBook's DisplayMessage method12 myGradeBook.DisplayMessage();13 } // end Main14 } // end class GradeBookTest

Fig. 4.2: Create a GradeBook object and call its DisplayMessage method.

Welcome to the Grade Book!

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

7

4.4 DECLARING A METHOD WITH A PARAMETER

Page 8: 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

8

4.4 DECLARING A METHOD WITH A PARAMETER

Page 9: 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

9

Page 10: 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

10

UML CLASS DIAGRAMS UML class diagram of class GradeBook with a public

DisplayMessage operation

Fig 4.6 GradeBook has a public DisplayMessage operation with a courseName parameter of type string.

Page 11: 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

11

4.5 INSTANCE VARIABLES AND PROPERTIES

Page 12: 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

12

Page 13: 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

14

Software Engineering Observation 4.2 Precede every field and method declaration with an

access modifier. Generally, instance variables should be declared private

and methods and properties should be declared public. No access modifier = private declare a private method, if it will be accessed only by

other methods of the class. Software Engineering Observation 4.3

private member variables and public methods and properties helps debugging

because problems with data manipulations are localized to the class’s methods and properties, since the private member variables are accessible only to these methods and properties.

Page 14: 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

15

UML CLASS DIAGRAM WITH A PROPERTY

Page 15: 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

16

4.8 AUTO-IMPLEMENTED PROPERTIESpublic string CourseName { get; set; }

the C# compiler can automatically create a private instance variable, and the get and set accessors for it.

Unlike a user-defined property, an auto-implemented property, must have both a get and a set accessor.

Page 17: 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

18

4.9 VALUE TYPES VS. REFERENCE TYPES Reference Types

A reference variable contains the address of a location in memory.

GradeBook myGradeBook = new GradeBook();

Reference-Type Instance Variables Initialized to null

string is a reference type. value null is not an empty string ( "" or string.Empty)

Page 18: 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

19

4.10 INITIALIZING OBJECTS WITH CONSTRUCTORS

Constructors methods can initialize an object C# requires a constructor call for every

object that is created. The new operator calls the constructor.

Page 19: 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

20

4.11 FLOATING-POINT NUMBERS AND TYPE DECIMAL type float represent precision with 7 significant digits. type double represent precision with15–16 significant

digits Type decimal provides 28–29 significant digits. To type a decimal literal, you must type the letter “M”

or “m”