370 likes | 472 Views
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
E N D
4 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design
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
Chapter Objectives (continued) • Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design
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
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
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
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
Class Diagram Figure 4-1 Student class diagram created in Visual Studio C# Programming: From Problem Analysis to Program Design
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
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
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
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
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
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
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
Accessor and Mutator Examples Accessor publicdouble GetNoOfSquareYards( ) { return noOfSquareYards; } publicvoid SetNoOfSquareYards(double squareYards) { noOfSquareYards = squareYards; } Mutator C# Programming: From Problem Analysis to Program Design
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
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
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
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
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
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
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
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
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
RealEstateInvestment Example Figure 4-8 Problem specification for RealEstateInvestment example C# Programming: From Problem Analysis to Program Design
Data for the RealEstateInvestment Example C# Programming: From Problem Analysis to Program Design
Data for the RealEstateInvestment Example (continued) C# Programming: From Problem Analysis to Program Design
RealEstateInvestment Example (continued) Figure 4-9 Prototype C# Programming: From Problem Analysis to Program Design
RealEstateInvestment Example (continued) Figure 4-10 Class diagrams C# Programming: From Problem Analysis to Program Design
RealEstateInvestment Example (continued) C# Programming: From Problem Analysis to Program Design
RealEstateInvestment Example (continued) Figure 4-11 Structured English for the RealEstateInvestment example C# Programming: From Problem Analysis to Program Design
Class Diagram Figure 4-12 RealEstateInvestment class diagram C# Programming: From Problem Analysis to Program Design
Test and Debug Figure 4-13 Output from RealEstate Investment example C# Programming: From Problem Analysis to Program Design
Coding Standards • Naming Conventions • Classes • Properties • Methods • Constructor Guidelines • Spacing Guidelines C# Programming: From Problem Analysis to Program Design
Chapter Summary • Components of a method • Class methods • Parameters • Predefined methods • Value and nonvalue-returning methods C# Programming: From Problem Analysis to Program Design
Chapter Summary (continued) • Properties • Instance methods • Constructors • Mutators • Accessors • Types of parameters C# Programming: From Problem Analysis to Program Design