Presentation is loading. Please wait.

Presentation is loading. Please wait.

BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.

Similar presentations


Presentation on theme: "BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class."— Presentation transcript:

1 BASE CLASSES AND INHERITANCE CHAPTER 4

2 Engineer Class

3 CivilEngineer inherits from Engineer Result is Engineer Charge = 31 Civil Engineer Charge = 40

4 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);

5

6

7 First code yields values for the object engineer. Engineer Charge = 31 Engineer Charge = 30 Second code yields both engineer and civilengineer with the introduction of enum. Engineer Charge = 31 Civil Engineer Charge = 40

8

9

10 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.

11

12 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.

13 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.

14

15

16

17 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.

18 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.

19

20 EXCEPTION HANDLING

21 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.

22

23 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

24

25 THE EXCEPTION CLASSES

26

27 THE CATCH CLAUSE

28

29 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.

30

31 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;

32

33 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.

34

35

36 FIELD INITIALIZERS ARE NOT ALLOWED

37 STRUCTS ARE SEALED The modifiers that cannot be used with structs are the following: Protected Internal abstract virtual

38 BOXING AND UNBOXING If you want to use a struct instance as a reference type object, you must make a boxed copy

39 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.

40 STRUCTS

41 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.

42


Download ppt "BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class."

Similar presentations


Ads by Google