base classes and inheritance chapter 4. engineer class

42
BASE CLASSES AND INHERITANCE CHAPTER 4

Upload: brendan-howard

Post on 27-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

BASE CLASSES AND INHERITANCECHAPTER 4

Page 2: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

Engineer Class

Page 3: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

CivilEngineer inherits from Engineer

Result is Engineer Charge = 31Civil Engineer Charge = 40

Page 4: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

ARRAY OF ENGINEERS

• Creation of an array of engineers

Engineer[] earray = new Engineer[2];

earray[0] = new Engineer("George", 15.50F);

earray[1] = new CivilEngineer("Sir John", 40F);

Page 5: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 6: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 7: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

• First code yields values for the object engineer.

• Engineer Charge = 31Engineer Charge = 30

• Second code yields both engineer and civilengineer with the introduction of enum.

• Engineer Charge = 31Civil Engineer Charge = 40

Page 8: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 9: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 10: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

VIRTUAL FUNCTIONS

• In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP). (Wikipedia)

• Virtual means that when a call to a member function is made, the compiler should look at the real type of the object (not just the type of the reference) and call the appropriate function based on that type.

Page 11: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 12: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

• When the compiler encounters a call to TypeName()or CalculateCharge(), it goes to the definition of the function and notes that it is a virtual function. Instead of generating code to call the function directly, it writes a bit of dispatch code that at runtime will look at the real type of the object and call the function associated with the real type, rather than just the type of the reference. This allows the correct function to be called even if the class wasn’t implemented when the caller was compiled.

• For example, if there was payroll processing code that stored an array of Engineer, a new class derived from Engineer could be added to the system without having to modify or recompile the payroll code.

Page 13: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

ABSTRACT CLASSES

• An abstract member is a function member that is designed to be overridden. An abstract member has the following characteristics:

• it is marked with the abstract modifier.

• It doesn’t have an implementation code block. The code blocks of abstract members are represented by semicolons.

Page 14: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 15: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 16: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 17: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

SEALED CLASSES AND METHODS

• A sealed class can be instantiated only as a stand-alone class object—it cannot be used as a base class as opposed to abstract class.

• A sealed class is labeled with the sealed modifier.

Page 18: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

STATIC CLASSES

• Static classes are used to group data and functions that are not affected by instance data.

• A common use of a static class might be to create a math library containing sets of mathematical methods and values.The class itself must be marked static.

• All the members of the class must be static.

• The class can have a static constructor, but it cannot have an instance constructor, since you cannot create an instance of the class.

• Static classes are implicitly sealed. That is, you cannot inherit from a static class.

Page 19: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 20: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

EXCEPTION HANDLING

Page 21: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

WHAT ARE EXCEPTIONS

• An exceptionis a run-time error in a program that violates a system or application constraint, or a condition that is not expected to occur during normal operation.

• Examples are when a program tries to divide a number by zero or tries to write to a read-only file. When these occur, the system catches the error and raises an exception.

Page 22: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 23: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THE TRY STATEMENT

• The try block contains the code that is being guarded for exceptions.

• The catch clauses section contains one or more catch clauses. These are blocks of code to handle the exceptions. They are also known as exception handlers.

• The finally block contains code to be executed under all circumstances, whether or not an exception is raised

Page 24: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 25: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THE EXCEPTION CLASSES

Page 26: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 27: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THE CATCH CLAUSE

Page 28: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 29: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THE FINALLY BLOCK

• If no exception occurs inside the try block, then at the end of the try block, control skips over any catch clauses and goes to the finally block.

• If an exception occurs inside the try block, then the appropriate catch clause in the catch clauses

• section is executed, followed by execution of the finallyblock.

Page 30: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 31: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THROWING EXCEPTION

• You can make your code explicitly raise an exception by using the throw statement. The syntax for the throw statement is the following:

throw ExceptionObject;

Page 32: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 33: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

THROWING WITHOUT AN EXCEPTION OBJECT

• The throw statement can also be used without an exception object, inside a catch block.

• This form rethrows the current exception, and the system continues its search for additional handlers for it.

• This form can be used only inside a catch statement.

Page 34: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 35: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class
Page 36: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

FIELD INITIALIZERS ARE NOT ALLOWED

Page 37: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

STRUCTS ARE SEALED

• The modifiers that cannot be used with structs are the following:

• Protected

• Internal

• abstract

• virtual

Page 38: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

BOXING AND UNBOXING

• If you want to use a struct instance as a reference type object, you must

• make a boxed copy

Page 39: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

STRUCTS AS RETURN VALUES AND PARAMETERS

• Structs can be used as return values and parameters. • Return value: When a struct is a return value, a copy is created and

returned from the function member.

• Value parameter: When a struct is used as a value parameter, a copy of the actual parameter struct is created. The copy is used in the execution of the method.

• ref and out parameters: If you use a struct as a ref or out parameter, a reference to the struct is passed into the method so that the data members can be changed.

Page 40: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

STRUCTS

Page 41: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class

WHAT ARE STRUCTS?

• Structs are programmer-defined data types, very similar to classes. They have data members and function members.

• Although similar to classes, there are a number of important differences. The most important ones are the following:

• Classes are reference types, and structs are value types.

• Structs are implicitly sealed, which means they cannot be derived from.

Page 42: BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class