Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.

Similar presentations


Presentation on theme: "C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition."— Presentation transcript:

1 C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition 4

2 C# Programming: From Problem Analysis to Program Design2 Chapter Objectives Become familiar with the components of a class Learn about the different methods and properties used for object-oriented development Write your own instance methods to include constructors, mutators, and accessors Call instance methods including constructors, mutators, and accessors

3 C# Programming: From Problem Analysis to Program Design3 Chapter Objectives ( continued ) Work through a programming example that illustrates the chapter’s concepts

4 C# Programming: From Problem Analysis to Program Design4 The Object Concept Solution is defined in terms of a collection of cooperating objects Class serves as template from which many objects can be created Abstraction –Attributes (data) –Behaviors (processes on the data)

5 Private Member Data All code you write is placed in a class When you define a class, you declare instance variables or fields that represent state of an object –Fields are declared inside the class, but not inside any specific method –Fields become visible to all members of the class, including all of the methods Data members are defined to have private access C# Programming: From Problem Analysis to Program Design5

6 Private Member Data ( continued ) public class Student { private int studentNumber; private string studentName; private int score1; private int score2; private int score3; private string major; C# Programming: From Problem Analysis to Program Design6

7 Add a Class Use the Project menu or the Solution Explorer Window Right-mouse click and select the option Add class Solution Explorer Window enables you to create a class diagram C# Programming: From Problem Analysis to Program Design7

8 Class Diagram C# Programming: From Problem Analysis to Program Design8 Figure 4-1 Student class diagram created in Visual Studio

9 Class Diagram ( continued ) C# Programming: From Problem Analysis to Program Design9 Figure 4-2 Student class diagram details After the class diagram is created, add the names of data members or fields and methods using the Class Details section

10 Class Diagram ( continued ) When you complete, the class details code is automatically placed in the file with each entry you add to the class diagram C# Programming: From Problem Analysis to Program Design10 Figure 4-3 Auto generated code from Student class diagram

11 C# Programming: From Problem Analysis to Program Design11 Writing Your Own Instance Methods Do not use static keyword –Static – class method Constructor –Do not return a value –void is not included –Same identifier as the class name –Overloaded methods –Default constructor No arguments Write one constructor and you lose the default one

12 Constructor public access modifier is always associated with constructors //Default constructor public Student ( ) { } //Constructor with one parameter public Student (int sID ) { studentNumber = sID; } C# Programming: From Problem Analysis to Program Design12

13 C# Programming: From Problem Analysis to Program Design13 Constructor ( continued ) Default values are assigned to variables of the value types when no arguments are sent to constructor

14 C# Programming: From Problem Analysis to Program Design14 Accessor Getters Returns the current value Standard naming convention → prefix with “get” –Accessor for noOfSquareYards is GetNoOfSquareYards( ) Properties serve purpose

15 Mutators Setters Normally includes one parameter Method body → single assignment statement Standard naming convention → prefix with ”Set” C# Programming: From Problem Analysis to Program Design15

16 C# Programming: From Problem Analysis to Program Design16 Accessor and Mutator Examples public double GetNoOfSquareYards( ) { return noOfSquareYards; } public void SetNoOfSquareYards(double squareYards) { noOfSquareYards = squareYards; } Accessor Mutator

17 Other Instance Methods No need to pass arguments to these methods Instance methods can directly access private data members Define methods as opposed to storing values that are calculated from other private data members C# Programming: From Problem Analysis to Program Design17

18 C# Programming: From Problem Analysis to Program Design18 Property Properties looks like a data field –More closely aligned to methods Standard naming convention in C# for properties –Use the same name as the instance variable or field, but start with uppercase character

19 C# Programming: From Problem Analysis to Program Design19 ToString( ) Method All user-defined classes inherit four methods from the object class –ToString( ) –Equals( ) –GetType( ) –GetHashCode( ) ToString( ) method is called automatically by several methods –Write( ) –WriteLine( ) methods Can also invoke or call the ToString( ) method directly

20 C# Programming: From Problem Analysis to Program Design20 ToString( ) Method ( continued ) Returns a human-readable string Can write a new definition for the ToString( ) method to include useful details public override string ToString( ) { // return string value } Keyword override added to provide new implementation details

21 C# Programming: From Problem Analysis to Program Design21 Calling Instance Methods – Constructor Calling the constructor ClassName objectName = new ClassName(argumentList); or ClassName objectName; objectName = new ClassName(argumentList); Keyword new used as operator to call constructor methods CarpetCalculator plush = new CarpetCalculator ( ); CarpetCalculator pile = new CarpetCalculator (37.90, 17.95); CarpetCalculator berber = new CarpetCalculator (17.95);

22 C# Programming: From Problem Analysis to Program Design22 Calling Accessor and Mutator Methods Method name is preceded by the object name berber.SetNoOfSquareYards(27.83); Console.WriteLine(“{0:N2}”, berber.GetNoOfSquareYards( )); Using properties PropertyName = value; and Console.Write(“Total Cost at {0:C} ”, berber.Price);

23 C# Programming: From Problem Analysis to Program Design23 Calling Other Instance Methods Call must match method signature If method returns a value, must be place for a value to be returned

24 Testing Your New Class Different class is needed for testing and using your class Test class has Main( ) in it Construct objects of your class Use the properties to assign and retrieve values Invoke instance methods using the objects you construct C# Programming: From Problem Analysis to Program Design24

25 C# Programming: From Problem Analysis to Program Design25 RealEstateInvestment Example Figure 4-8 Problem specification for RealEstateInvestment example

26 C# Programming: From Problem Analysis to Program Design26 Data for the RealEstateInvestment Example

27 C# Programming: From Problem Analysis to Program Design27 Data for the RealEstateInvestment Example ( continued )

28 C# Programming: From Problem Analysis to Program Design28 RealEstateInvestment Example ( continued ) Figure 4-9 Prototype

29 C# Programming: From Problem Analysis to Program Design29 RealEstateInvestment Example ( continued ) Figure 4-10 Class diagrams

30 C# Programming: From Problem Analysis to Program Design30 RealEstateInvestment Example ( continued )

31 C# Programming: From Problem Analysis to Program Design31 Figure 4-11 Structured English for the RealEstateInvestment example RealEstateInvestment Example ( continued )

32 Class Diagram C# Programming: From Problem Analysis to Program Design32 Figure 4-12 RealEstateInvestment class diagram

33 Test and Debug C# Programming: From Problem Analysis to Program Design33 Figure 4-13 Output from RealEstate Investment example

34 Coding Standards Naming Conventions –Classes –Properties –Methods Constructor Guidelines Spacing Guidelines C# Programming: From Problem Analysis to Program Design34

35 C# Programming: From Problem Analysis to Program Design35 Chapter Summary Components of a method Class methods –Parameters Predefined methods Value- and nonvalue-returning methods

36 C# Programming: From Problem Analysis to Program Design36 Chapter Summary ( continued ) Properties Instance methods –Constructors –Mutators –Accessors Types of parameters


Download ppt "C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition."

Similar presentations


Ads by Google