Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 199 Final Review.

Similar presentations


Presentation on theme: "CIS 199 Final Review."— Presentation transcript:

1 CIS 199 Final Review

2 Outline Parameter to a method
Accessor and Modifiers (get and set methods) Sequential Search Algorithm Overloading Class Concepts Overriding Inheritance Polymorphism Virtual Members

3 Parameter to a method A variable that holds data passed to a method when it is called Parameter 1 Parameter 2

4 How to call a value-returning method?
Assign the returning value! Calling a method

5 Sequential Search Algorithm
Looping through the array with for static void Main()         {             int search = 10;             bool found = false;             int[] values = { 3, 8, 13, 10, 7 };             for (int i = 0; i < values.Length; i++)             {                 if(search == values[i])                 {                     Console.WriteLine($"{search} found at index {i}");                     found = true;                     break;                 }             }             if (found) {                 Console.WriteLine("Search term found");             else {                 Console.WriteLine("Search term not found");             Console.Read();         }

6 Overloading Methods Overloading When you overload a C# method:
Involves using one term to indicate diverse meanings When you overload a C# method: You write multiple methods with a shared name The compiler understands your meaning based on the arguments you use with the method Methods are overloaded correctly when they have the same identifier but different parameter lists

7 Method takes string as a parameter

8 Same method takes int as a parameter

9

10 Class Concept State       –A 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       –A program or class that instantiates objects of another prewritten class

11 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, read-only, and volatile Most class fields are non-static and private       –Provides the highest level of security

12 Creating Properties Property
      –Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. 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

13

14 Accessors Both the get and the set accessor can be implemented as expression-bodied members. In this case, the get and set keywords must be present. The following example illustrates the use of expression body definitions for both accessors. Note that the return keyword is not used with the get accessor.

15 Auto-implemented Accessors
By using auto-implemented properties, you can simplify your code while having the C# compiler transparently provide the backing field for you. Note that the example also removes the parameterized constructor, so that SaleItem objects are now initialized with a call to the default constructor and an object initializer.

16 Inheritance Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.  Derived class “IS-A” base class Student IS-A Person “All students are a person, but not all persons are a student” Even if no base class is specified, one will be provided: Object

17 Inheritance The following illustration shows a class WorkItem that represents an item of work in some business process. Like all classes, it derives from System.Object and inherits all its methods. WorkItem adds five members of its own. These include a constructor, because constructors are not inherited.

18 Example from: https://docs. microsoft

19 Overriding toString()

20 Overriding In this example, the Square class must
provide an overridden implementation of  Area because Area is inherited from the abstract ShapesClass. Overriding Area() Method 

21 Polymorphism Polymorphism is a Greek word that means "many-shaped" and You can use polymorphism to solve this problem in two basic steps: Create a class hierarchy in which each specific shape class derives from a common base class. Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.

22 Virtual Members A derived class can override a base class member only if the base class member is declared as virtual or abstract. The derived member must use the override keyword to explicitly indicate that the method is intended to participate in virtual invocation. The following code provides an example.

23 Virtual Members When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class. The following code provides an example:

24 Questions


Download ppt "CIS 199 Final Review."

Similar presentations


Ads by Google