Sei sulla pagina 1di 3

HaNoi University of Technology

Week 4: Inheritance Mastering C# 2008

Week 4: Object-Oriented Programming: Inheritance PART I: SELF-REVIEW EXERCISES


Exercise 1
Fill in the blanks in each of the following statements: a) Client code can access a classs members via the operator in conjunction with a reference to an object of the class. b) Members of a class declared .. are accessible only to methods of the class in which those members are defined. c) A. initializes the instance variables of a class. d) A property. accessor is used to assign values to private instance variables of a class. e) Methods of a class normally are declared , and instance variables of a class normally are declared . f) A. accessor of a property is used to retrieve values of private data of a class. g) The keyword.. introduces a class definition. h) Members of a class declared. are accessible anywhere that an object of the class is in scope. i) The operator allocates memory dynamically for an object of a specified type and returns a to that type. j) A variable represents class-wide information. k) The keyword .specifies that an object or variable is not modifiable after it is initialized at execution time.

Exercise 2
State whether each of the following is true of false. If false explain why. a) All objects are passed by reference. b) Constructors can have return values. c) Properties must define get and set accessors. d) The this reference of an object is a reference to that object itself. e) A static member can be referenced when no object of that type exists. f) A static member of a class can be referenced through an instance of the class. g) Variables declared const must be initialized either in a declaration or in the class constructor. h) Different namespaces cannot have classes/methods with the same names. i) Assembly files are not required to define an entry point (Main method). j) Indexers can return any type in C#.

Exercise 3
Fill in the blanks in each of the following statements: a). 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.

1/3

HaNoi University of Technology

Week 4: Inheritance Mastering C# 2008

b) A base classs. members can be accessed only in the base-class definition or in derived-class definitions. c) In a(n) relationship, an object of a derived class also can be treated as an objectof its base class. d) In a(n).. .relationship, a class object has one or more references to objects ofother classes as members. e) A class exists in a(n). relationship with its derived classes. f) A base classs members are accessible anywhere that the program has a reference to that base class or to one of its derived classes. g) A base classs protected access members have a level of protection between those of public and .access. h) A base classs.. members can be accessed only in the same assembly. i) 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. j) Derived-class constructors can call base-class constructors via the reference.

Exercise 3
State whether each of the following is true or false. If false, explain why. a) It is possible to treat base-class objects and derived-class objects similarly. b) Base-class constructors are not inherited by derived classes. c) A has-a relationship is implemented via inheritance. d) All methods, by default, can be overridden. e) Method ToString of class System.Object is declared as virtual. f) When a derived class redefines a base-class method using the same signature, the derived class is said to overload that base-class method. g) A Car class has an is a relationship with its SteeringWheel and Brakes. h) Inheritance encourages the reuse of proven high-quality software.

2/3

HaNoi University of Technology

Week 4: Inheritance Mastering C# 2008

PART II :Exercise
Exercise 1
Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite classes Point3, Circle4 and Cylinder to use composition, rather than inheritance. After you do this, assess the relative merits of the two approaches for both the Point3,Circle4, Cylinder problem, as well as for object-oriented programs in general. Which approach is more natural, why?

Exercise 2
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.

Exercise 3
Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the base class of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. The private data of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of each of the classes in your hierarchy and polymorphically outputs each objects dimensions and area.

3/3

Potrebbero piacerti anche