Download - Csc253 chapter 09
![Page 1: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/1.jpg)
Microsoft Visual C# 2010Fourth Edition
Chapter 9Using Classes and Objects
![Page 2: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/2.jpg)
Microsoft Visual C# 2010, Fourth Edition 2
Objectives
• Learn about class concepts
• Create classes from which objects can be instantiated
• Create objects
• Create properties, including auto-implemented properties
![Page 3: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/3.jpg)
Microsoft Visual C# 2010, Fourth Edition 3
Objectives (cont'd.)
• Learn about using public fields and private methods
• Learn about the this reference
• Write constructors and use them
• Use object initializers
• Overload operators
![Page 4: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/4.jpg)
Microsoft Visual C# 2010, Fourth Edition 4
Objectives (cont'd.)
• Declare an array of objects and use the Sort() and BinarySearch() methods with them
• Write destructors
• Understand GUI application objects
![Page 5: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/5.jpg)
Microsoft Visual C# 2010, Fourth Edition 5
Understanding Class Concepts
• Types of classes– Classes that are only application programs with a Main() method
– Classes from which you instantiate objects• Can contain a Main() method, but it is not required
• Everything is an object– Every object is a member of a more general class
• An object is an instantiation of a class
• Instance variables– Data components of a class
![Page 6: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/6.jpg)
Microsoft Visual C# 2010, Fourth Edition 6
Understanding Class Concepts (cont'd.)
• Fields– Object attributes
• State– Set of contents of an object’s instance variables
• Instance methods– Methods associated with objects– Every instance of the class has the same methods
• Class client or class user– Program or class that instantiates objects of another
prewritten class
![Page 7: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/7.jpg)
Microsoft Visual C# 2010, Fourth Edition 7
Creating a Class from Which Objects Can Be Instantiated
• Class header or class definition parts– An optional access modifier– The keyword class– Any legal identifier for the name of your class
• Class access modifiers– public– protected– internal– private
![Page 8: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/8.jpg)
Microsoft Visual C# 2010, Fourth Edition 8
Creating a Class from Which Objects Can Be Instantiated (cont'd.)
![Page 9: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/9.jpg)
Microsoft Visual C# 2010, Fourth Edition 9
Creating Instance Variables and Methods
• When creating a class, define both its attributes and its methods
• Field access modifiers– new, public, protected, internal, private, static, readonly, and volatile
• Most class fields are nonstatic and private– Provides the highest level of security
![Page 10: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/10.jpg)
Microsoft Visual C# 2010, Fourth Edition 10
Creating Instance Variables and Methods (cont'd.)
• Using private fields within classes is an example of information hiding
• Most class methods are public• private data/public method arrangement
– Allows you to control outside access to your data
• Composition– Using an object within another object– Defines a has-a relationship
![Page 11: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/11.jpg)
Microsoft Visual C# 2010, Fourth Edition 11
Creating Instance Variables and Methods (cont'd.)
![Page 12: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/12.jpg)
Microsoft Visual C# 2010, Fourth Edition 12
Creating Objects
• Declaring a class does not create any actual objects
• Two-step process to create an object– Supply a type and an identifier– Create the object, which allocates memory for it
• Reference type– Identifiers for objects are references to their memory
addresses
• When you create an object, you call its constructor
![Page 13: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/13.jpg)
Microsoft Visual C# 2010, Fourth Edition 13
Creating Objects (cont'd.)
Invoking constructor
Invoking an object’s method
![Page 14: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/14.jpg)
Microsoft Visual C# 2010, Fourth Edition 14
Passing Objects to Methods
• You can pass objects to methods– Just as you can simple data types
![Page 15: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/15.jpg)
Passing Objects to Methods (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 15
![Page 16: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/16.jpg)
Microsoft Visual C# 2010, Fourth Edition 16
Creating Properties
• Property– A member of a class that provides access to a field of
a class– Defines how fields will be set and retrieved
• Properties have accessors– set accessors for setting an object’s fields– get accessors for retrieving the stored values
• Read-only property– Has only a get accessor
![Page 17: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/17.jpg)
Microsoft Visual C# 2010, Fourth Edition 17
Creating Properties (cont'd.)
![Page 18: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/18.jpg)
Microsoft Visual C# 2010, Fourth Edition 18
Creating Properties (cont'd.)
• Implicit parameter– One that is undeclared and that gets its value
automatically
• Contextual keywords– Identifiers that act like keywords in specific
circumstances– get, set, value, partial, where, and yield
![Page 19: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/19.jpg)
Microsoft Visual C# 2010, Fourth Edition 19
Creating Properties (cont'd.)
![Page 20: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/20.jpg)
Microsoft Visual C# 2010, Fourth Edition 20
Using Auto-Implemented Properties
• Auto-implemented property– The property’s implementation is created for you
automatically with the assumption that:• The set accessor should simply assign a value to the
appropriate field
• The get accessor should simply return the field
• When you use an auto-implemented property:– You do not need to declare the field that corresponds
to the property
![Page 21: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/21.jpg)
Microsoft Visual C# 2010, Fourth Edition 21
Using Auto-Implemented Properties (cont'd.)
![Page 22: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/22.jpg)
Microsoft Visual C# 2010, Fourth Edition 22
More About public and private Access Modifiers
• Occasionally you need to create public fields or private methods– You can create a public data field when you want all
objects of a class to contain the same value
• A named constant within a class is always static– Belongs to the entire class, not to any particular
instance
![Page 23: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/23.jpg)
Microsoft Visual C# 2010, Fourth Edition 23
More About public and private Access Modifiers (cont'd.)
![Page 24: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/24.jpg)
Microsoft Visual C# 2010, Fourth Edition 24
More About public and private Access Modifiers (cont'd.)
![Page 25: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/25.jpg)
Microsoft Visual C# 2010, Fourth Edition 25
Understanding the this Reference
• You might eventually create thousands of objects from a class– Each object does not need to store its own copy of
each property and method• this reference
– Implicitly passed reference
• When you call a method, you automatically pass the this reference to the method– Tells the method which instance of the class to use
![Page 26: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/26.jpg)
Microsoft Visual C# 2010, Fourth Edition 26
Understanding the this Reference (cont'd.)
![Page 27: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/27.jpg)
Microsoft Visual C# 2010, Fourth Edition 27
Understanding the this Reference (cont'd.)
![Page 28: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/28.jpg)
Microsoft Visual C# 2010, Fourth Edition 28
Understanding the this Reference (cont'd.)
![Page 29: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/29.jpg)
Microsoft Visual C# 2010, Fourth Edition 29
Understanding Constructors
• Constructor– Method that instantiates an object
• Default constructor– Automatically supplied constructor without parameters
• Default value of the object– The value of an object initialized with a default
constructor
![Page 30: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/30.jpg)
Microsoft Visual C# 2010, Fourth Edition 30
Passing Parameters to Constructors
• Parameterless constructor– Constructor that takes no arguments
• You can create a constructor that receives arguments
• Using the constructor Employee partTimeWorker = new Employee(12.50);
![Page 31: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/31.jpg)
Passing Parameters to Constructors (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 31
![Page 32: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/32.jpg)
Overloading Constructors
• C# automatically provides a default constructor– Until you provide your own constructor
• Constructors can be overloaded– You can write as many constructors as you want
• As long as their argument lists do not cause ambiguity
Microsoft Visual C# 2010, Fourth Edition 32
![Page 33: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/33.jpg)
Microsoft Visual C# 2010, Fourth Edition 33
Overloading Constructors (cont'd.)
![Page 34: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/34.jpg)
Microsoft Visual C# 2010, Fourth Edition 34
Overloading Constructors (cont'd.)
![Page 35: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/35.jpg)
Microsoft Visual C# 2010, Fourth Edition 35
Using Constructor Initializers
• Constructor initializer– Clause that indicates another instance of a class
constructor should be executed• Before any statements in the current constructor body
![Page 36: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/36.jpg)
Microsoft Visual C# 2010, Fourth Edition 36
Using Constructor Initializers (cont'd.)
![Page 37: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/37.jpg)
Microsoft Visual C# 2010, Fourth Edition 37
Using Object Initializers
• Object initializer– Allows you to assign values to any accessible
members or properties of a class at the time of instantiation
• Without calling a constructor with parameters
• For you to use object initializers, a class must have a default constructor
![Page 38: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/38.jpg)
Microsoft Visual C# 2010, Fourth Edition 38
Using Object Initializers (cont'd.)
![Page 39: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/39.jpg)
Microsoft Visual C# 2010, Fourth Edition 39
Using Object Initializers (cont'd.)
• Using object initializers allows you to:– Create multiple objects with different initial
assignments• Without having to provide multiple constructors to cover
every possible situation
– Create objects with different starting values for different properties of the same data type
![Page 40: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/40.jpg)
Microsoft Visual C# 2010, Fourth Edition 40
Using Object Initializers (cont'd.)
![Page 41: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/41.jpg)
Microsoft Visual C# 2010, Fourth Edition 41
Using Object Initializers (cont'd.)
![Page 42: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/42.jpg)
Microsoft Visual C# 2010, Fourth Edition 42
Overloading Operators
• Overload operators– Enable you to use arithmetic symbols with your own
objects
• Overloadable unary operators:+ - ! ~ ++ -- true false
• Overloadable binary operators:+ - * / % & | ^ == != > < >= <=
• Cannot overload the following operators:= && || ?? ?: checked unchecked new typeof as is
• Cannot overload an operator for a built-in data type
![Page 43: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/43.jpg)
Microsoft Visual C# 2010, Fourth Edition 43
Overloading Operators (cont'd.)
• When a binary operator is overloaded and has a corresponding assignment operator:– It is also overloaded
• Some operators must be overloaded in pairs:== with !=, and < with >
• Syntax to overload unary operators:type operator overloadable-operator (type identifier)
• Syntax to overload binary operators:type operator overloadable-operator (type identifier, type operand)
![Page 44: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/44.jpg)
Microsoft Visual C# 2010, Fourth Edition 44
Overloading Operators (cont'd.)
![Page 45: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/45.jpg)
Overloading Operators (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 45
![Page 46: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/46.jpg)
Microsoft Visual C# 2010, Fourth Edition 46
Declaring an Array of Objects
• You can declare arrays that hold elements of any type– Including objects
• ExampleEmployee[] empArray = new Employee[7];
for(int x = 0; x < empArray.Length; ++x)
empArray[x] = new Employee();
![Page 47: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/47.jpg)
Microsoft Visual C# 2010, Fourth Edition 47
Using the Sort() and BinarySearch() Methods with Arrays of Objects
• CompareTo() method– Provides the details of how the basic data types
compare to each other– Used by the Sort() and BinarySearch() methods
• When you create a class that contains many fields– Tell the compiler which field to use when making
comparisons• Use an interface
![Page 48: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/48.jpg)
Microsoft Visual C# 2010, Fourth Edition 48
Using the Sort() and BinarySearch() Methods with Arrays of Objects (cont'd.)• Interface
– Collection of methods that can be used by any class • As long as the class provides a definition to override the
interface’s do-nothing, or abstract, method definitions
• When a method overrides another:– It takes precedence, hiding the original version
• IComparable interface– Contains the definition for the CompareTo() method
• Compares one object to another and returns an integer
![Page 49: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/49.jpg)
Microsoft Visual C# 2010, Fourth Edition 49
Using the Sort() and BinarySearch() Methods with Arrays of Objects (cont'd.)
![Page 50: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/50.jpg)
Using the Sort() and BinarySearch() Methods with Arrays of Objects (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 50
![Page 51: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/51.jpg)
Microsoft Visual C# 2010, Fourth Edition 51
Using the Sort() and BinarySearch() Methods with Arrays of Objects (cont'd.)
![Page 52: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/52.jpg)
Microsoft Visual C# 2010, Fourth Edition 52
Using the Sort() and BinarySearch() Methods with Arrays of Objects (cont'd.)
![Page 53: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/53.jpg)
Microsoft Visual C# 2010, Fourth Edition 53
Understanding Destructors
• Destructor– Contains the actions you require when an instance of
a class is destroyed
• Most often, an instance of a class is destroyed when it goes out of scope
• Explicitly declare a destructor– Identifier consists of a tilde (~) followed by the class
name
![Page 54: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/54.jpg)
Microsoft Visual C# 2010, Fourth Edition 54
Understanding Destructors (cont'd.)
![Page 55: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/55.jpg)
Microsoft Visual C# 2010, Fourth Edition 55
Understanding Destructors (cont'd.)
![Page 56: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/56.jpg)
Understanding GUI Application Objects
• Objects you have been using in GUI applications are objects just like others– They encapsulate properties and methods
Microsoft Visual C# 2010, Fourth Edition 56
![Page 57: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/57.jpg)
You Do It
• Activities to explore– Creating a Class and Objects– Using Auto-Implemented Properties– Adding Overloaded Constructors to a Class– Creating an Array of Objects
Microsoft Visual C# 2010, Fourth Edition 57
![Page 58: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/58.jpg)
Microsoft Visual C# 2010, Fourth Edition 58
Summary
• You can create classes that are only programs with a Main() method and classes from which you instantiate objects
• When creating a class:– Must assign a name to it and determine what data
and methods will be part of the class– Usually declare instance variables to be private
and instance methods to be public
• When creating an object: – Supply a type and an identifier, and you allocate
computer memory for that object
![Page 59: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/59.jpg)
Microsoft Visual C# 2010, Fourth Edition 59
Summary (cont'd.)
• A property is a member of a class that provides access to a field of a class
• Class organization within a single file or separate files
• Each instantiation of a class accesses the same copy of its methods
• A constructor is a method that instantiates (creates an instance of) an object
• You can pass one or more arguments to a constructor
![Page 60: Csc253 chapter 09](https://reader036.vdocument.in/reader036/viewer/2022062513/557b04c4d8b42af74a8b4e0e/html5/thumbnails/60.jpg)
Microsoft Visual C# 2010, Fourth Edition 60
Summary (cont'd.)
• Constructors can be overloaded
• You can pass objects to methods just as you can simple data types
• You can overload operators to use with objects
• You can declare arrays that hold elements of any type, including objects
• A destructor contains the actions you require when an instance of a class is destroyed