Upload
phyllis-cole
View
217
Download
0
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
ADVANCED PROGRAMMINGCHAPTER 4: INTRODUCTION TO INTRODUCTION TO CLASSES, OBJECTS, METHODS AND STRINGS
Dr Shahriar BijaniWinter 2016
2
REFERENCES Visual C# 2012 How to Program, Paul Deitel
& Harvey Deitel, 5th Edition, Prentice Hall.
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
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.
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
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!
7
4.4 DECLARING A METHOD WITH A PARAMETER
8
4.4 DECLARING A METHOD WITH A PARAMETER
9
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.
11
4.5 INSTANCE VARIABLES AND PROPERTIES
12
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.
15
UML CLASS DIAGRAM WITH A PROPERTY
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.
17
4.9 VALUE TYPES VS. REFERENCE TYPES
Value Types A value type variable contains a value of that type! C#’s simple types (like int and double) are all value
types.
int count = 7;
All simple types, enum or a struct type are value types
Value typesbool
byte
char
decimal
double
enum
float
int
long
sbyte
short
struct
uint
ulong
ushort
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)
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.
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”