Microsoft Visual C# 2010        Fourth Edition          Chapter 9  Using Classes and Objects
Objectives• Learn about class concepts• Create classes from which objects can be  instantiated• Create objects• Create pro...
Objectives (contd.)• Learn about using public fields and private  methods• Learn about the this reference• Write construct...
Objectives (contd.)• Declare an array of objects and use the Sort() and  BinarySearch() methods with them• Write destructo...
Understanding Class Concepts• Types of classes    – Classes that are only application programs with a      Main() method  ...
Understanding Class Concepts                   (contd.)• Fields    – Object attributes• State    – Set of contents of an o...
Creating a Class from Which Objects          Can Be Instantiated• Class header or class definition parts    – An optional ...
Creating a Class from Which Objects     Can Be Instantiated (contd.)Microsoft Visual C# 2010, Fourth Edition   8
Creating Instance Variables and                    Methods• When creating a class, define both its attributes and  its met...
Creating Instance Variables and               Methods (contd.)• Using private fields within classes is an example  of info...
Creating Instance Variables and               Methods (contd.)Microsoft Visual C# 2010, Fourth Edition   11
Creating Objects• Declaring a class does not create any actual objects• Two-step process to create an object    – Supply a...
Creating Objects (contd.)                                                   Invoking constructor                          ...
Passing Objects to Methods• You can pass objects to methods    – Just as you can simple data typesMicrosoft Visual C# 2010...
Passing Objects to Methods (contd.)Microsoft Visual C# 2010, Fourth Edition   15
Creating Properties• Property    – A member of a class that provides access to a field of      a class    – Defines how fi...
Creating Properties (contd.)Microsoft Visual C# 2010, Fourth Edition     17
Creating Properties (contd.)• Implicit parameter    – One that is undeclared and that gets its value      automatically• C...
Creating Properties (contd.)Microsoft Visual C# 2010, Fourth Edition     19
Using Auto-Implemented Properties• Auto-implemented property    – The property’s implementation is created for you      au...
Using Auto-Implemented Properties                 (contd.)Microsoft Visual C# 2010, Fourth Edition   21
More About public and private            Access Modifiers• Occasionally you need to create public fields or  private metho...
More About public and private         Access Modifiers (contd.)Microsoft Visual C# 2010, Fourth Edition   23
More About public and private         Access Modifiers (contd.)Microsoft Visual C# 2010, Fourth Edition   24
Understanding the this Reference• You might eventually create thousands of objects  from a class    – Each object does not...
Understanding the this Reference                (contd.)Microsoft Visual C# 2010, Fourth Edition   26
Understanding the this Reference                (contd.)Microsoft Visual C# 2010, Fourth Edition   27
Understanding the this Reference                (contd.)Microsoft Visual C# 2010, Fourth Edition   28
Understanding Constructors• Constructor    – Method that instantiates an object• Default constructor    – Automatically su...
Passing Parameters to Constructors• Parameterless constructor    – Constructor that takes no arguments• You can create a c...
Passing Parameters to Constructors               (contd.)Microsoft Visual C# 2010, Fourth Edition   31
Overloading Constructors• C# automatically provides a default constructor    – Until you provide your own constructor• Con...
Overloading Constructors (contd.)Microsoft Visual C# 2010, Fourth Edition   33
Overloading Constructors (contd.)Microsoft Visual C# 2010, Fourth Edition   34
Using Constructor Initializers• Constructor initializer    – Clause that indicates another instance of a class      constr...
Using Constructor Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition   36
Using Object Initializers• Object initializer    – Allows you to assign values to any accessible      members or propertie...
Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition      38
Using Object Initializers (contd.)• Using object initializers allows you to:    – Create multiple objects with different i...
Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition      40
Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition      41
Overloading Operators• Overload operators    – Enable you to use arithmetic symbols with your own      objects• Overloadab...
Overloading Operators (contd.)• When a binary operator is overloaded and has a  corresponding assignment operator:    – It...
Overloading Operators (contd.)Microsoft Visual C# 2010, Fourth Edition   44
Overloading Operators (contd.)Microsoft Visual C# 2010, Fourth Edition   45
Declaring an Array of Objects• You can declare arrays that hold elements of any  type    – Including objects• Example    E...
Using the Sort() and BinarySearch()     Methods with Arrays of Objects• CompareTo() method    – Provides the details of ho...
Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) • Interface     – Collection of methods that c...
Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition   49
Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition   50
Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition   51
Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition   52
Understanding Destructors• Destructor    – Contains the actions you require when an instance of      a class is destroyed•...
Understanding Destructors (contd.)Microsoft Visual C# 2010, Fourth Edition   54
Understanding Destructors (contd.)Microsoft Visual C# 2010, Fourth Edition   55
Understanding GUI Application                    Objects• Objects you have been using in GUI applications  are objects jus...
You Do It• Activities to explore    –   Creating a Class and Objects    –   Using Auto-Implemented Properties    –   Addin...
Summary• You can create classes that are only programs with  a Main() method and classes from which you  instantiate objec...
Summary (contd.)• A property is a member of a class that provides  access to a field of a class• Class organization within...
Summary (contd.)• Constructors can be overloaded• You can pass objects to methods just as you can  simple data types• You ...
Upcoming SlideShare
Loading in...5
×

Csc253 chapter 09

295

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
295
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Csc253 chapter 09

  1. 1. Microsoft Visual C# 2010 Fourth Edition Chapter 9 Using Classes and Objects
  2. 2. Objectives• Learn about class concepts• Create classes from which objects can be instantiated• Create objects• Create properties, including auto-implemented propertiesMicrosoft Visual C# 2010, Fourth Edition 2
  3. 3. Objectives (contd.)• Learn about using public fields and private methods• Learn about the this reference• Write constructors and use them• Use object initializers• Overload operatorsMicrosoft Visual C# 2010, Fourth Edition 3
  4. 4. Objectives (contd.)• Declare an array of objects and use the Sort() and BinarySearch() methods with them• Write destructors• Understand GUI application objectsMicrosoft Visual C# 2010, Fourth Edition 4
  5. 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 classMicrosoft Visual C# 2010, Fourth Edition 5
  6. 6. Understanding Class Concepts (contd.)• 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 classMicrosoft Visual C# 2010, Fourth Edition 6
  7. 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 – privateMicrosoft Visual C# 2010, Fourth Edition 7
  8. 8. Creating a Class from Which Objects Can Be Instantiated (contd.)Microsoft Visual C# 2010, Fourth Edition 8
  9. 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 securityMicrosoft Visual C# 2010, Fourth Edition 9
  10. 10. Creating Instance Variables and Methods (contd.)• 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 relationshipMicrosoft Visual C# 2010, Fourth Edition 10
  11. 11. Creating Instance Variables and Methods (contd.)Microsoft Visual C# 2010, Fourth Edition 11
  12. 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 constructorMicrosoft Visual C# 2010, Fourth Edition 12
  13. 13. Creating Objects (contd.) Invoking constructor Invoking an object’s methodMicrosoft Visual C# 2010, Fourth Edition 13
  14. 14. Passing Objects to Methods• You can pass objects to methods – Just as you can simple data typesMicrosoft Visual C# 2010, Fourth Edition 14
  15. 15. Passing Objects to Methods (contd.)Microsoft Visual C# 2010, Fourth Edition 15
  16. 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 accessorMicrosoft Visual C# 2010, Fourth Edition 16
  17. 17. Creating Properties (contd.)Microsoft Visual C# 2010, Fourth Edition 17
  18. 18. Creating Properties (contd.)• 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 yieldMicrosoft Visual C# 2010, Fourth Edition 18
  19. 19. Creating Properties (contd.)Microsoft Visual C# 2010, Fourth Edition 19
  20. 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 propertyMicrosoft Visual C# 2010, Fourth Edition 20
  21. 21. Using Auto-Implemented Properties (contd.)Microsoft Visual C# 2010, Fourth Edition 21
  22. 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 instanceMicrosoft Visual C# 2010, Fourth Edition 22
  23. 23. More About public and private Access Modifiers (contd.)Microsoft Visual C# 2010, Fourth Edition 23
  24. 24. More About public and private Access Modifiers (contd.)Microsoft Visual C# 2010, Fourth Edition 24
  25. 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 useMicrosoft Visual C# 2010, Fourth Edition 25
  26. 26. Understanding the this Reference (contd.)Microsoft Visual C# 2010, Fourth Edition 26
  27. 27. Understanding the this Reference (contd.)Microsoft Visual C# 2010, Fourth Edition 27
  28. 28. Understanding the this Reference (contd.)Microsoft Visual C# 2010, Fourth Edition 28
  29. 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 constructorMicrosoft Visual C# 2010, Fourth Edition 29
  30. 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);Microsoft Visual C# 2010, Fourth Edition 30
  31. 31. Passing Parameters to Constructors (contd.)Microsoft Visual C# 2010, Fourth Edition 31
  32. 32. 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 ambiguityMicrosoft Visual C# 2010, Fourth Edition 32
  33. 33. Overloading Constructors (contd.)Microsoft Visual C# 2010, Fourth Edition 33
  34. 34. Overloading Constructors (contd.)Microsoft Visual C# 2010, Fourth Edition 34
  35. 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 bodyMicrosoft Visual C# 2010, Fourth Edition 35
  36. 36. Using Constructor Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition 36
  37. 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 constructorMicrosoft Visual C# 2010, Fourth Edition 37
  38. 38. Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition 38
  39. 39. Using Object Initializers (contd.)• 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 typeMicrosoft Visual C# 2010, Fourth Edition 39
  40. 40. Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition 40
  41. 41. Using Object Initializers (contd.)Microsoft Visual C# 2010, Fourth Edition 41
  42. 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 typeMicrosoft Visual C# 2010, Fourth Edition 42
  43. 43. Overloading Operators (contd.)• 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)Microsoft Visual C# 2010, Fourth Edition 43
  44. 44. Overloading Operators (contd.)Microsoft Visual C# 2010, Fourth Edition 44
  45. 45. Overloading Operators (contd.)Microsoft Visual C# 2010, Fourth Edition 45
  46. 46. Declaring an Array of Objects• You can declare arrays that hold elements of any type – Including objects• Example Employee[] empArray = new Employee[7]; for(int x = 0; x < empArray.Length; ++x) empArray[x] = new Employee();Microsoft Visual C# 2010, Fourth Edition 46
  47. 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 Microsoft Visual C# 2010, Fourth Edition 47
  48. 48. Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) • 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 Microsoft Visual C# 2010, Fourth Edition 48
  49. 49. Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition 49
  50. 50. Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition 50
  51. 51. Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition 51
  52. 52. Using the Sort() and BinarySearch() Methods with Arrays of Objects (contd.) Microsoft Visual C# 2010, Fourth Edition 52
  53. 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 nameMicrosoft Visual C# 2010, Fourth Edition 53
  54. 54. Understanding Destructors (contd.)Microsoft Visual C# 2010, Fourth Edition 54
  55. 55. Understanding Destructors (contd.)Microsoft Visual C# 2010, Fourth Edition 55
  56. 56. Understanding GUI Application Objects• Objects you have been using in GUI applications are objects just like others – They encapsulate properties and methodsMicrosoft Visual C# 2010, Fourth Edition 56
  57. 57. 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 ObjectsMicrosoft Visual C# 2010, Fourth Edition 57
  58. 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 objectMicrosoft Visual C# 2010, Fourth Edition 58
  59. 59. Summary (contd.)• 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 constructorMicrosoft Visual C# 2010, Fourth Edition 59
  60. 60. Summary (contd.)• 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 destroyedMicrosoft Visual C# 2010, Fourth Edition 60
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×