Escolar Documentos
Profissional Documentos
Cultura Documentos
Week 5: Inheritance
Topics
Polymorphism Interfaces Delegates Operator Overloading
1/13
C# supports ____ types of Inheritance mechanisms. a) 1 b) 2 c) 3 d)4 They are: 1) _______________________________ 2) _______________________________
Exercise 2
Fill in the blanks in each of the following statements: 1) __________________ is a form of software reusability in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. 2) A base classs __________________ members can be accessed only in the base-class definition or in derived-class definitions. 3) In a(n) __________________ relationship, an object of a derived class also can be treated as an object of its base class. 4) In a(n) _________________ relationship, a class object has one or more references to objects of other classes as members. 5) A class exists in a(n) __________________ relationship with its derived classes. 6) A base classs __________________ members are accessible anywhere that the program has a reference to that base class or to one of its derived classes. 7) A base classs protected access members have a level of protection between those of public and __________________ access. 8) A base classs __________________ members can be accessed only in the same assembly. 9) When an object of a derived class is instantiated, the base classs __________________ is called implicitly or explicitly to do any necessary initialization of the base-class instance variables in the derived-class object. 10) Derived-class constructors can call base-class constructors via the __________________ reference.
2/13
Exercise 3
State whether each of the following is true or false. If false, explain why. 1) It is possible to treat base-class objects and derived-class objects similarly.
3/13
6) When a derived class redefines a base-class method using the same signature, the derived class is said to overload that base-class method.
4/13
Exercise 4
1) Some programmers prefer not to use protected access because it breaks the encapsulation of the base class. Discuss the relative merits of using protected access vs. insisting on using private access in base classes.
2) Many programs written with inheritance could be written with composition instead, and vice versa. Which approach is more natural, why?
5/13
6/13
7/13
Exercise 7
8/13
Initial classes
1. Modify the person class to contain an enumeration of the marital status called MaritalStatus containing values for (Single, Married, Divorced or Widowed). 2. Modify the CanMarry() method and the constructor.
9/13
Exercise 10
In this exercise, you are going to write an IStudent interface and a PartTimeStudent class that implements it. These may be used in a scenario where employees are allowed to register as part time students; as such one can be an employee and a student at the same time. 1. Write an interface, IStudent 2. Add two simple properties as described Property Type Description LibCardNumber string Library card number Year int Year of study
4. Write a class PartTimeStudent that inherits from Employee and implements the IStudent interface
10/13
11/13
LAB 2: HOMEWORK
1 1. Write a program demonstrating delegate to changing lower characters into upper characters.
2. Following codes contain error, correct them and write a class Circle to implement this interfaces.
public interface IDimensions { long width; long height; double Area(); double Circumference(); int Side(); }
12/13
13/13