Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating and Using Classes

Similar presentations


Presentation on theme: "Creating and Using Classes"— Presentation transcript:

1 Creating and Using Classes
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data

2 Objectives Describe these members of a class: constructor, method, field, and property Explain how instantiation works Describe the concept of overloading a method Explain what a static member is 9/18/2019 6:23 AM

3 Common Class Elements (repeated)
Classes contain a number of elements: Functions (methods) – operations that can be performed by an object. Constructors – special type of method that’s executed when an object is instantiated Data stores (including properties and fields) Forms – objects of the form data type Numerous objects of types defined by other classes Variables (of primitive types) Events (and Event handlers -- not always tied to forms) – an event is a signal that notifies other objects that something noteworthy has occurred 9/18/2019 6:23 AM

4 Common Class Elements (continued)
User defined data types – structures, enumerations, complex structured types (arrays of structures, lists or arrays of objects, etc) Delegate - A special type of object that’s used to wire an event to a method Operator - A special type of method that’s performed for a Visual Basic operator such as + or =. 9/18/2019 6:23 AM

5 How Instantiation Works
The process of creating an object from a class is called instantiation You can create many instances of a single class A class defines a reference type The variable that is created for the object of a class thus contains a reference to the memory location where the object is stored and not the object itself When an object is instantiated the appropriate constructor is executed 9/18/2019 6:23 AM

6 Fields aka Attributes Fields (Class attributes) are class variables/objects that are global to (accessible by) all of the executable components of a class Usually declared as private and are not accessible outside the class May also be declared as public or protected 9/18/2019 6:23 AM

7 Fields (continued) The desirable goal in using classes is to encapsulate and protect class data stores from external reference except through methods of the class Declaring fields as public defeats the purpose of using classes If accessing some class data stores from outside the class is considered cumbersome, we can access them through the use of properties, which simplifies the access mechanism we have to use while still protecting the data Properties, as implemented in C# do not provide much of an advantage. I would not use them 9/18/2019 6:23 AM

8 9/18/2019 6:23 AM

9 9/18/2019 6:23 AM

10 9/18/2019 6:23 AM

11 Static versus Instance Data
Class data can be instance data or static data Static data is declared with the static keyword One copy exists no matter the number of class instances One copy of instance data exists for each class instance How are static data useful?  9/18/2019 6:23 AM

12 A Note on Designing with Static Data
NOTE: A static function operates within the context of static data – it can access any static data in a class, but it cannot access instance members How is the static counter NextHuman used below? 9/18/2019 6:23 AM

13 Using Properties to Access Private Data
If we wish to provide more convenient access to the private or protected data of a class, we can do so through the use of public properties These are described, with examples, in the text I don’t advocate using public properties in C# The cause more confusion and provide little advantage 9/18/2019 6:23 AM

14 Methods Classes contain functions which we refer to as the called methods Methods can be public (often the case) or private (lower level functions usually used by higher level private functions) Whatever the case, we can write public or private functions in a class Why might we want a private function in a class? Methods perform operations on the data stores (attributes) of a class 9/18/2019 6:23 AM

15 Method Signatures and Overloading
A method’s signature is defined by its name and its unique combination of parameters Note that the return value is not part of a method’s signature We can write methods having the same name but different sets of parameters  This is known as overloading Overloading provides multiple ways of invoking a given method 9/18/2019 6:23 AM

16 Methods and Overloading
9/18/2019 6:23 AM

17 Constructors By default, C# will provide you with a default constructor for each class you write When you use new to create a new object (an instance of a class), the C# default constructor assigns default values to all instance variables of the new object If this is not what you want, you can write your own constructor(s) by taking advantage of overloading (See slide 9) How does the compiler know which constructor is being referenced when you use new applied to a type (class) with multiple constructors? 9/18/2019 6:23 AM

18 More on Constructors 9/18/2019 6:23 AM

19 9/18/2019 6:23 AM

20 9/18/2019 6:23 AM

21 9/18/2019 6:23 AM


Download ppt "Creating and Using Classes"

Similar presentations


Ads by Google