1 / 37

Creating Your Own Classes

4. Creating Your Own Classes. C# Programming: From Problem Analysis to Program Design 3rd Edition. Chapter Objectives. Become familiar with the components of a class Learn about the different methods and properties used for object-oriented development

madison
Download Presentation

Creating Your Own Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 C# Programming: From Problem Analysis to Program Design

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

  4. 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) C# Programming: From Problem Analysis to Program Design

  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 a private access C# Programming: From Problem Analysis to Program Design

  6. Private Member Data publicclass Student { privateint studentNumber; privatestring studentName; privateint score1; privateint score2; privateint score3; privatestring major; C# Programming: From Problem Analysis to Program Design

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

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

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

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

  11. 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 loose the default one C# Programming: From Problem Analysis to Program Design

  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 Design

  13. Constructor • Default values are assigned to variables of the value types when no arguments are sent to constructor C# Programming: From Problem Analysis to Program Design

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

  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 Design

  16. Accessor and Mutator Examples Accessor publicdouble GetNoOfSquareYards( ) { return noOfSquareYards; } publicvoid SetNoOfSquareYards(double squareYards) { noOfSquareYards = squareYards; } Mutator C# Programming: From Problem Analysis to Program Design

  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 Design

  18. 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 C# Programming: From Problem Analysis to Program Design

  19. 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 C# Programming: From Problem Analysis to Program Design

  20. 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 C# Programming: From Problem Analysis to Program Design

  21. Calling Instance Methods • 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); C# Programming: From Problem Analysis to Program Design

  22. 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); C# Programming: From Problem Analysis to Program Design

  23. 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); C# Programming: From Problem Analysis to Program Design

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

  25. 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 Design

  26. RealEstateInvestment Example Figure 4-8 Problem specification for RealEstateInvestment example C# Programming: From Problem Analysis to Program Design

  27. Data for the RealEstateInvestment Example C# Programming: From Problem Analysis to Program Design

  28. Data for the RealEstateInvestment Example (continued) C# Programming: From Problem Analysis to Program Design

  29. RealEstateInvestment Example (continued) Figure 4-9 Prototype C# Programming: From Problem Analysis to Program Design

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

  31. RealEstateInvestment Example (continued) C# Programming: From Problem Analysis to Program Design

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

  33. Class Diagram Figure 4-12 RealEstateInvestment class diagram C# Programming: From Problem Analysis to Program Design

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

  35. Coding Standards • Naming Conventions • Classes • Properties • Methods • Constructor Guidelines • Spacing Guidelines C# Programming: From Problem Analysis to Program Design

  36. Chapter Summary • Components of a method • Class methods • Parameters • Predefined methods • Value and nonvalue-returning methods C# Programming: From Problem Analysis to Program Design

  37. Chapter Summary (continued) • Properties • Instance methods • Constructors • Mutators • Accessors • Types of parameters C# Programming: From Problem Analysis to Program Design

More Related