1 / 27

Day 4.

Day 4. 1. Admin. 2. Review 3. Inheritance in depth. 4. Worked example. 5. Assignment 2. 1. Admin. Take in program 1. Program 2 will not be a full program, but an exercise in extending code using Inheritance, covered today. 2. Review.

braith
Download Presentation

Day 4.

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. Day 4. 1. Admin. 2. Review 3. Inheritance in depth. 4. Worked example. 5. Assignment 2.

  2. 1. Admin. • Take in program 1. • Program 2 will not be a full program, but an exercise in extending code using Inheritance, covered today.

  3. 2. Review. • Composition, Inheritance and Template classes are all forms of code reuse. • Last time we focused on Composition. • This is called a “__HAS A__” relation. • Why?

  4. Review (cont). • The idea of composition is to develop some general, reusable components that can appear as data members in a wide variety of other classes. • By contrast, inheritance is an “___IS A___” relation. • What does this mean?

  5. 3. Inheritance in depth. • With inheritance instead of an object of one class being included in another class, a more specific class is derived from a more abstract class. • E.g. A poodle IS A dog (unfortunately). • A Dell IS A computer (allegedly). • Scientist IS A vocation (really). • A Lawyer IS A….

  6. Inheritance in depth (cont). • The more abstract class is called the BASE or PARENT class. The more specific class is called the DERIVED or DESCENDANT class. Just as descendants can inherit things from their parents, so a DESCENDANT class automatically inherits all the data members and member methods in the PARENT class.

  7. Inheritance in depth (cont). • For example, suppose we have an EMPLOYEE base class. This contains all the data/methods that apply to any employee (it is generic) e.g. • Data members: name, address, SSN/ID, pay. • Member methods: Set and Get/Print.

  8. Inheritance in depth (cont). • Now other, more specific classes, can be defined by inheritance from EMPLOYEE, E.g. HOURLY, SALARIED, CONTRACT, INTERN, NEWBIE, WHIPPING BOY. These classes receive all that is in EMPLOYEE, but can also customize it in certain ways.

  9. Inheritance in depth (cont). • The derived classes can: • 1) Add additional data members e.g. HoursWorked for HOURLY, ManagementLevel for SALARIED. • 2) Add additional member methods e.g. CalculatePay (). • 3) Redefine member methods in the base class e.g. SetValues ( ).

  10. Inheritance in depth (cont). • There are 2 things that derived classes cannot do. • (1) They cannot inherit the base class constructor. • Why? • What implication does this have for coding?

  11. Inheritance in depth (cont). • In addition, • (2) Although derived classes can add new data members, and can redefine base class methods, they cannot redefine the data type of base class data members (e.g. if Salary is float in the base class, it cannot be redefined as double in the derived class).

  12. Inheritance in depth (cont). • In that sense, the base class data is the common denominator of all the classes in the hierarchy. • Analogy 1: like a common family resemblance (e.g. everyone in the Hapsburg family had the “Hapsburg” lip). • Analogy 2: all Christians in the body of Christ have Christ Himself in common.

  13. Inheritance in depth (cont). • Other examples of inheritance: • E.g. 1:Shape as the BASE class, Rectangle, Triangle and Circle as derived. • Here we can have second-level inheritance, defining Cube, Pyramid and Sphere by inheritance from Rectangle, Triangle and Circle.

  14. Inheritance in depth (cont). • E.g. 2:Sale as the BASE class, Cash, Check, Credit Card, Monty Python DVDs as the derived instances. • E.g. 3:Chart as the BASE class, Pie-Chart, Line-Chart and Histogram as derived instances.

  15. Inheritance in depth (cont). • Important notes: • 1) Inheritance is not the same as accessibility. Although the descendant class inherits everything in the base class, it cannot *directly* access what is private in the base class but must use the base class’s member methods.

  16. Inheritance in depth (cont). • 2) Inheritance can be single or multiple. Single inheritance is from one base class. Multiple inheritance is from 2 or more base classes. E.g: Date, Time and Customers might form Flight.

  17. Inheritance in depth (cont). • 3) Classes can be concrete or abstract. • A concrete class has / can have instances e.g. HourlyWorker, Zebra. • An abstract class cannot have meaningful instances e.g. Employee, Animal. • Like “The Average Person / Worker.” • (Can use the keyword abstract to make it impossible to instantiate a class.)

  18. Inheritance in depth (cont). • A difficulty for inheritance: • We noted that if data in the base class is private, it cannot be directly accessed by descendant classes. However, some times, we want to allow this. But, if we make the base class data public…… • What is the problem?

  19. Inheritance in depth (cont). • There is a 3rd way out of this difficulty. The base class data can be marked as protected: • “Protected” means accessible to the base class and its descendants only. The data is therefore inaccessible to non-descendant classes and cannot directly be accessed by client programs.

  20. Inheritance in depth (cont). • There are also 3 different types of inheritance: public, protected and private. • 1. Public inheritance keeps the security status of data the same in the descendant: • Base Class Descendant Public Public Protected Protected Private Private

  21. Inheritance in depth (cont). • 2. Protected inheritance makes everything at least protected. • Base Class Descendant Public Protected Protected Protected Private Private

  22. Inheritance in depth (cont). • 3. Private inheritance makes everything at least private (rarely used!). • Base Class Descendant Public Private Protected Private Private Private

  23. Inheritance in depth (cont). • Neither protected nor private inheritance are very common, because they hide not only the data but also the member methods from client programs!

  24. Inheritance in depth (cont). • Syntax of inheritance. • 1. Define a base class, e.g. • public class Employ { } 2. Define one or more descendants, e.g.

  25. Inheritance in depth (cont). • public class Hourly : public Employ { } // note use of public inheritance • public class Salaried : public Employ { }

  26. 4. Worked example. • See the sample code: • Employee_Class.cs • Employee_Driver.cs

  27. 5. Assignment 2. • Extend the code handed out (copies of text file versions available from the CSC 300 web site) by: • (1) adding 2 new descendants to the Employee base class to Employee_Class.cs; • (2) instantiating 2 objects of each class and testing them within Employee_Driver.cs.

More Related