Download presentation
Presentation is loading. Please wait.
Published byPhyllis Cole Modified over 9 years ago
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
2
R EFERENCES Visual C# 2012 How to Program, Paul Deitel & Harvey Deitel, 5th Edition, Prentice Hall. 2
3
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
4
4.2 C LASSES, O BJECTS, M ETHODS, P ROPERTIES AND I NSTANCE V ARIABLES 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 C LASS E XAMPLE 1 // Fig. 4.1: GradeBook.cs 2 // Class declaration with one method. 3 using System; 4 5 public class GradeBook 6 { 7 // display a welcome message to the GradeBook user 8 9 { 10 Console.WriteLine( "Welcome to the Grade Book!" ); 11 } // end method DisplayMessage 12 } // end class GradeBook 5
6
C REATE AND C ALL AN O BJECT 1 // Fig. 4.2: GradeBookTest.cs 2 // Create a GradeBook object and call its DisplayMessage method. 3 public class GradeBookTest 4 { 5 // Main method begins program execution 6 public static void Main( string[] args ) 7 { 8 // create a GradeBook object and assign it to myGradeBook 9 GradeBook myGradeBook = new GradeBook(); 10 11 // call myGradeBook's DisplayMessage method 12 myGradeBook.DisplayMessage(); 13 } // end Main 14 } // end class GradeBookTest Fig. 4.2: Create a GradeBook object and call its DisplayMessage method. 6 Welcome to the Grade Book!
7
4.4 D ECLARING A M ETHOD WITH A P ARAMETER 7
8
8
9
9
10
UML C LASS D IAGRAMS 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. 10
11
4.5 I NSTANCE V ARIABLES AND P ROPERTIES 11
12
12
13
1 // Fig. 4.8: GradeBookTest.cs 2 // Create and manipulate a GradeBook object. 3 using System; 4 5 public class GradeBookTest 6 { 7 // Main method begins program execution 8 public static void Main( string [] args ) 9 { 10 // create a GradeBook object and assign it to myGradeBook 11 GradeBook myGradeBook = new GradeBook(); 12 13 // display initial value of CourseName 14 Console.WriteLine( "Initial course name is: '{0}'\n", 15 ); 16 17 // prompt for and read course name 18 Console.WriteLine( "Please enter the course name:" ); 19 20 Console.WriteLine(); // output a blank line 21 22 // display welcome message after specifying course name 23 24 } // end Main 25 } // end class GradeBookTest 13
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. 14
15
UML C LASS D IAGRAM WITH A P ROPERTY 15
16
4.8 A UTO -I MPLEMENTED P ROPERTIES public 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. 16
17
4.9 V ALUE T YPES VS. R EFERENCE T YPES 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 17 Value types bool byte char decimal double enum float int long sbyte short struct uint ulong ushort
18
4.9 V ALUE T YPES VS. R EFERENCE T YPES 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) 18
19
4.10 I NITIALIZING O BJECTS WITH C ONSTRUCTORS Constructors methods can initialize an object C# requires a constructor call for every object that is created. The new operator calls the constructor. 19
20
4.11 F LOATING -P OINT N UMBERS AND T YPE 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” 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.