Download presentation
Presentation is loading. Please wait.
1
Dr Shahriar Bijani Winter 2017
Advanced Programming Chapter 4: Introduction to Classes, Objects & Methods Dr Shahriar Bijani Winter 2017
2
References Visual C# 2012 How to Program, Paul Deitel & Harvey Deitel, 5th Edition, Prentice Hall.
3
4.2 Classes, Objects, Methods, Properties and Instance Variables
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 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 Example 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 { Console.WriteLine( "Welcome to the Grade Book!" ); 11 } // end method DisplayMessage 12 } // end class GradeBook
6
Create and Call an Object
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 { // create a GradeBook object & assign it to myGradeBook GradeBook myGradeBook = new GradeBook(); 10 // call myGradeBook's DisplayMessage method myGradeBook.DisplayMessage(); 13 } // end Main 14 } // 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
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
13
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
UML Class Diagram with a Property
15
4.8 Auto-Implemented Properties
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
4.9 Value Types vs. Reference 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 and struct type are value types Value types bool byte char decimal double enum float int long sbyte short struct uint ulong ushort
17
4.9 Value Types vs. 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
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.
19
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” decimal d = 2.0M
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.