Transcript Chapter 4
4
Creating Your
Own Classes
C# Programming: From Problem Analysis to Program Design
3rd Edition
C# Programming: From Problem Analysis to Program Design
1
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
2
Chapter Objectives (continued)
• Work through a programming example that
illustrates the chapter’s concepts
C# Programming: From Problem Analysis to Program Design
3
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
4
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 Design
5
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 Design
6
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 Design
7
Class Diagram
Figure 4-1 Student class diagram created in Visual Studio
C# Programming: From Problem Analysis to Program Design
8
Class Diagram (continued)
• 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
9
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
Figure 4-3 Auto generated
code from Student class
diagram
C# Programming: From Problem Analysis to Program Design
10
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
C# Programming: From Problem Analysis to Program Design
11
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
12
Constructor (continued)
• 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
13
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
14
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
15
Accessor and Mutator Examples
public double GetNoOfSquareYards( )
{
return noOfSquareYards;
}
Accessor
public void SetNoOfSquareYards(double squareYards)
{
Mutator
noOfSquareYards = squareYards;
}
C# Programming: From Problem Analysis to Program Design
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
RealEstateInvestment Example
Figure 4-8 Problem specification for RealEstateInvestment example
C# Programming: From Problem Analysis to Program Design
25
Data for the
RealEstateInvestment Example
C# Programming: From Problem Analysis to Program Design
26
Data for the
RealEstateInvestment Example
(continued)
C# Programming: From Problem Analysis to Program Design
27
RealEstateInvestment Example
(continued)
Figure 4-9 Prototype
C# Programming: From Problem Analysis to Program Design
28
RealEstateInvestment Example
(continued)
Figure 4-10 Class diagrams
C# Programming: From Problem Analysis to Program Design
29
RealEstateInvestment Example
(continued)
C# Programming: From Problem Analysis to Program Design
30
RealEstateInvestment
Example (continued)
Figure 4-11 Structured English
for the RealEstateInvestment
example
C# Programming: From Problem Analysis to Program Design
31
Class Diagram
Figure 4-12 RealEstateInvestment class diagram
C# Programming: From Problem Analysis to Program Design
32
Test and Debug
Figure 4-13 Output from RealEstate Investment example
C# Programming: From Problem Analysis to Program Design
33
Coding Standards
• Naming Conventions
– Classes
– Properties
– Methods
• Constructor Guidelines
• Spacing Guidelines
C# Programming: From Problem Analysis to Program Design
34
Chapter Summary
• Components of a method
• Class methods
– Parameters
• Predefined methods
• Value- and nonvalue-returning methods
C# Programming: From Problem Analysis to Program Design
35
Chapter Summary (continued)
• Properties
• Instance methods
– Constructors
– Mutators
– Accessors
• Types of parameters
C# Programming: From Problem Analysis to Program Design
36