1 / 11

Classes & Objects: A Deeper Look Chapters 10, 11, & 12

Explore chapters 10, 11, & 12 of Dr. John P. Abraham's book on classes and objects, covering concepts such as encapsulation, inheritance, and polymorphism. Example code and explanations provided.

brittd
Download Presentation

Classes & Objects: A Deeper Look Chapters 10, 11, & 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes & Objects: A Deeper LookChapters 10, 11, & 12 Dr. John P. Abraham Professor UTPA

  2. Example used: Time Class • Contains 3 int variables declared as private. • Also, 3 public methods: setTime, toUniversalString and toString. • Does not have a constructor. Instead time is passed in using public setTime, where the three integers are validated to be within range, if not 0 is assigned. When constructor is not defined, the compiler adds a default one, assigning 0’s to all three int variables.

  3. Time class example continued • Method ToUniversalString returns a string in unversal-time format. • Return string.Format(“{0:D2}:{1:D2}:{2:D2}”,hour, minute, second); //like 13:27:06 • Method ToString() overrides string • Return string.Format (“{0:D2}:{1:D2}:{2:D2}”,((hour==0||hour==12)?12:hour%12), etc.);// if hour is 0 or 12 it appears as 12 AM, or PM, else it appears from 1 to 11, if hour is <12 it is AM.

  4. Indexers • The indices can be either integer (as conventional arrays) or non-integer such as the data element name. • Indexers are defined like properties in a class • I will be showing a program using indexers next week to explain this concept.

  5. class IntIndexer { private string[] myData; public IntIndexer(int size) { myData = new string[size]; for (int i = 0; i < size; i++) { myData[i] = "empty"; } } public string this[int pos] { get { return myData[pos]; } set { myData[pos] = value; } }

  6. static void Main(string[] args) { int size = 10; IntIndexer myInd = new IntIndexer(size); myInd[0] = "Artem Chebotko"; myInd[1] = "Robert Scheweller"; myInd[2] = "Pearl Brazier"; myInd[3] = "Laura Grabowski"; myInd[4] = "John Abraham"; myInd[5] = "Emmet Tomai"; myInd[6] = "AYang Liu"; myInd[7] = "Bin Fu"; Console.WriteLine("\nIndexer Output\n"); for (int i = 0; i < size; i++) { Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]); } Console.ReadKey(); }

  7. Encapsulation • Lets the program hide some of the data and operation of a class while exposing others. • Implementation of a method is hidden from the user, so it appears like a black box. • Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).

  8. Inheritance • A new class is created by absorbing an existing class’s members and enhancing them with new or modified capabilities. • Base class : the existing class from which a new class inherits members. • Derived class : the new class that inherited from the base class. Each new class can become the base class for a future derived class. Is-a relationship and has-a relationship. Is-a represents inheritance. Has-a represents a composition. • Inheritance allows you to reuse code

  9. Inheritance contd. public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); Console.ReadKey(); } }

  10. Polymorphism • Enables you to “program in general” than “program in specific”. • Enables us to write applications that process objects that share the same base class in a class hierarchy as if they were all objects of the base class. • It allows you to invoke derived class methods through a base class reference during run-time. • overrides means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.

More Related