Best web site - 390 Object-Oriented Programming: Polymorphism Chapter 10 3. Referring

January 18th, 2008

390 Object-Oriented Programming: Polymorphism Chapter 10 3. Referring to a derived-class object with a base-class reference is safe, because the derived-class object is an object of its base class. However, this reference can refer only to base-class members. If this code refers to derived-class-only members through the base-class reference, the compiler reports an error. 4. Referring to a base-class object with a derived-class reference generates a compiler error. To avoid this error, the derived-class reference first must be cast to a base- class reference explicitly. In this cast, the derived-class reference must reference a derived-class object, or C# generates an InvalidCastException. Common Programming Error 10.3 After assigning a derived-class object to a base-class reference, attempting to reference derived- class-only members with the base-class reference is a compilation error. Common Programming Error 10.4 Treating a base-class object as a derived-class object can cause errors. Though it is convenient to treat derived-class objects as base-class objects by manipulating derived-class objects with base-class references, doing so can cause significant problems. For example, a payroll system, must be able to traverse an array of employees and calculate the weekly pay for each person. Intuition suggests that using base-class references would enable the program to call only the base-class payroll calculation routine (if there is such a routine in the base class). Using only base-class references, we can invoke the proper payroll calculation routine for each object, whether the object is a base-class object or a derived-class object. We learn how to create classes that exhibit this behavior as we introduce polymorphism throughout this chapter. 10.3 Type Fields and switch Statements One way to determine the type of an object that is incorporated in a larger program is to use a switch structure. This allows us to distinguish among object types, then invoke an appropriate action for a particular object. For example, in a hierarchy of shapes in which each shape object has a ShapeType property, a switch structure could employ the object s ShapeType to determine which Print method to call. However, using switch logic exposes programs to a variety of potential problems. For example, the programmer might forget to include a type test when one is warranted, or the programmer might forget to test all possible cases in a switch structure. When modifying a switch-based system by adding new types, the programmer might forget to insert the new cases in all relevant switch statements. Every addition or deletion of a class requires the modification of every switch statement in the system; tracking these statements down can be time consuming and error prone. Software Engineering Observation 10.3 Polymorphic programming can eliminate the need for unnecessary switch logic. By using C# s polymorphism mechanism to perform the equivalent logic, programmers can avoid the kinds of errors typically associated with switch logic.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Web hosting service - Chapter 10 Object-Oriented Programming: Polymorphism 389 Line 28

January 17th, 2008

Chapter 10 Object-Oriented Programming: Polymorphism 389 Line 28 casts point2, which currently refers to a Circle object (circle1), to a Circleand assigns the result to circle2. As we discuss momentarily, this cast would be dangerous if point2 were referencing a Point. Lines 30 31 invoke method ToStringof the Circle object to which circle2 now refers (note that the fourth line of the output demonstrates that Circle s ToString method is called). Lines 33 34 calculate and output circle2 s Area. Line 39 explicitly casts reference point1 to a Circle. This is a dangerous operation, because point refers to a Point object, and a Point is not a Circle. Objects can be cast only to their own type or to their base-class types. If this statement were to execute, C# would determine that point1 references a Point object, recognize the cast to Circle as dangerous and indicate an improper cast with an InvalidCastException message. However, we prevent this statement from executing by including an if/else structure (lines 37 45). The condition at line 37 uses keyword is to determine whether the object to which point1refers is a Circle. Keyword is discovers the type of the object to which the left operand refers and compares this type to the right operand (in this case, Circle). In our example, point1 does not refer to a Circle, so the condition fails, and line 44 appends to output a string that indicates the result. Note that the is comparison will be true if the left operand is a reference to an instance of the right operand or a derived class. Common Programming Error 10.2 Attempting to cast a base-class reference to a derived-class type causes an Invalid- CastException if the reference refers to a base-class object rather than an appropriate derived-class object. Software Engineering Observation 10.2 The is keyword enables a program to determine whether a cast operation would be successful by ensuring that the reference type and target type are compatible. If we remove the if test and execute the program, C# displays a MessageBox that contains the message: An unhandled exception of type ‘System.InvalidCastException’ occurred in followed by the name and path of the executing program. We discuss how to deal with such situations in Chapter 11. Despite the fact that a derived-class object also is a base-class object, the derived- class and base-class objects are different. As we have discussed previously, derived-class objects can be treated as if they were base-class objects. This is a logical relationship, because the derived class contains members that correspond to all members in the base class, but the derived class can have additional members. For this reason, assigning a base- class object to a derived-class reference is not allowed without an explicit cast. Such an assignment would leave the additional derived-class members undefined. There are four ways to mix base-class references and derived-class references with base-class objects and derived-class objects: 1. Referring to a base-class object with a base-class reference is straightforward. 2. Referring to a derived-class object with a derived-class reference is straightforward.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Simple web server - 388 Object-Oriented Programming: Polymorphism Chapter 10 49 50

January 17th, 2008

388 Object-Oriented Programming: Polymorphism Chapter 10 49 50 } // end method Main 51 52 } // end class PointCircleTest Fig. 10.3 Fig. 10.FiFig. 10.3g. 10.3Assigning derived-class references to base-class references. (Part 2 of 2.) Fig. 10.3 Line 21 assigns circle1 (a reference to a derived-class object) to point2 (a base- class reference). In C#, it is acceptable to assign a derived-class object to a base-class reference, because of the inheritance is a relationship. Class Circle inherits from class Point, because a Circleis a Point(in a structural sense, at least). However, assigning a base-class reference to a derived-class reference is potentially dangerous, as we will discuss. Lines 23 24 invoke point2.ToString and append the result to output. When C# encounters a virtual method invocation (such as method ToString), C# determines which version of the method to call from the type of the object on which the method is called, not the type of the reference that refers to the object. In this case, point2refers to a Circleobject, so C# calls Circlemethod ToString, rather than Pointmethod ToString(as one might expect from the point2reference, which was declared as type Point). The decision about which method to call is an example of polymorphism, a concept that we discuss in detail throughout this chapter. Note that if point2 referenced a Point object rather than a Circle object, C# would invoke Point s ToString method. Previous chapters used methods such as Int32.Parse and Double.Parse to convert between various built-in C# types. Now, we convert between object references of programmer-defined types. We use explicit casts to perform these conversions. If the cast is valid, our program can treat a base-class reference as a derived-class reference. If the cast is invalid, C# throws an InvalidCastException, which indicates that the cast operation is not allowed. Exceptions are discussed in detail in Chapter 11, Exception Handling. Common Programming Error 10.1 Assigning a base-class object (or a base-class reference) to a derived-class reference (without an explicit cast) is a syntax error. Software Engineering Observation 10.1 If a derived-class object has been assigned to a reference of one of its direct or indirect base classes, it is acceptable to cast that base-class reference back to a reference of the derived- class type. In fact, this must be done to send that object messages that do not appear in the base class. [Note: We sometimes use the term messages to represent the invocation of methods and the use of object properties.]
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Chapter 10 Object-Oriented Programming: Polymorphism 387 method ToString (Web site directory)

January 16th, 2008

Chapter 10 Object-Oriented Programming: Polymorphism 387 method ToString of point1 prints the object as a Point. Similarly, because circle1 is reference to a Circle object, method ToString of circle1 prints the object as a Circle. 1 // Fig. 10.3: PointCircleTest.cs 2 // Demonstrating inheritance and polymorphism. 3 4 using System; 5 using System.Windows.Forms; 6 7 // PointCircleTest class definition 8 class PointCircleTest 9 { 10 // main entry point for application. 11 static void Main( string[] args ) 12 { 13 Point point1 = new Point( 30, 50 ); 14 Circle circle1 = new Circle( 120, 89, 2.7 ); 15 16 string output = “Point point1: ” + point1.ToString() + 17 “nCircle circle1: ” + circle1.ToString(); 18 19 // use ‘is a’ relationship to assign 20 // Circle circle1 to Point reference 21 Point point2 = circle1; 22 23 output += “nnCCircle circle1 (via point2): ” + 24 point2.ToString(); 25 26 // downcast (cast base-class reference to derived-class 27 // data type) point2 to Circle circle2 28 Circle circle2 = ( Circle ) point2; 29 30 output += “nnCircle circle1 (via circle2): ” + 31 circle2.ToString(); 32 33 output += “nArea of circle1 (via circle2): ” + 34 circle2.Area().ToString( “F” ); 35 36 // attempt to assign point1 object to Circle reference 37 if ( point1 is Circle ) 38 { 39 circle2 = ( Circle ) point1; 40 output += “nncast successful”; 41 } 42 else 43 { 44 output += “nnpoint1 does not refer to a Circle”; 45 } 46 47 MessageBox.Show( output, 48 “Demonstrating the ‘is a’ relationship” ); Fig. 10.3 Fig. 10.FiFig. 10.3g. 10.3Assigning derived-class references to base-class references. (Part 1 of 2.) Fig. 10.3
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

1 on 1 web hosting - 386 Object-Oriented Programming: Polymorphism Chapter 10 21 Radius

January 15th, 2008

386 Object-Oriented Programming: Polymorphism Chapter 10 21 Radius = radiusValue; 22 } 23 24 // property Radius 25 public double Radius 26 { 27 get 28 { 29 return radius; 30 } 31 32 set 33 { 34 if ( value >= 0 ) // validate radius 35 radius = value; 36 } 37 38 } // end property Radius 39 40 // calculate Circle diameter 41 public double Diameter() 42 { 43 return Radius * 2; 44 } 45 46 // calculate Circle circumference 47 public double Circumference() 48 { 49 return Math.PI * Diameter(); 50 } 51 52 // calculate Circle area 53 public virtual double Area() 54 { 55 return Math.PI * Math.Pow( Radius, 2 ); 56 } 57 58 // return string representation of Circle 59 public override string ToString() 60 { 61 return “Center = ” + base.ToString() + 62 “; Radius = ” + Radius; 63 } 64 65 } // end class Circle Fig. 10.2 Fig. 10.2Fig. 10FiFi.2g. 10.2g. 10.2Circleclass that inherits from class Point. (Part 2 of 2.) Class PointCircleTest (Fig. 10.3) demonstrates assigning derived-class references to base-class references and casting base-class references to derived-class references. Lines 13 14 declare a Pointreference (point1) and a Circlereference (circle1). Lines 16 17 append Stringrepresentations of each object to Stringoutputto show the values used to initialize these objects. Because point1is reference to a Pointobject,
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Chapter 10 (Web hosting mysql) Object-Oriented Programming: Polymorphism 385 34 set

January 14th, 2008

Chapter 10 Object-Oriented Programming: Polymorphism 385 34 set 35 { 36 x = value; // no need for validation 37 } 38 39 } // end property X 40 41 // property Y 42 public int Y 43 { 44 get 45 { 46 return y; 47 } 48 49 set 50 { 51 y = value; // no need for validation 52 } 53 54 } // end property Y 55 56 // return string representation of Point 57 public override string ToString() 58 { 59 return “[” + X + “, ” + Y + “]”; 60 } 61 62 } // end class Point Fig. 10.1 Fig. 10.1Fig. 10FiFi.1g. 10.1g. 10.1Pointclass represents an x-y coordinate pair. (Part 2 of 2.) 1 // Fig. 10.2: Circle.cs 2 // Circle class that inherits from class Point. 3 4 using System; 5 6 // Circle class definition inherits from Point 7 public class Circle : Point 8 { 9 private double radius; // circle’s radius 10 11 // default constructor 12 public Circle() 13 { 14 // implicit call to Point constructor occurs here 15 } 16 17 // constructor 18 public Circle( int xValue, int yValue, double radiusValue ) 19 : base( xValue, yValue ) 20 { Fig. 10.2 Fig. 10.2Fig. 10FiFi.2g. 10.2g. 10.2Circleclass that inherits from class Point. (Part 1 of 2.)
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

Web page design - 384 Object-Oriented Programming: Polymorphism Chapter 10 references that

January 12th, 2008

384 Object-Oriented Programming: Polymorphism Chapter 10 references that refer to objects of many derived-class types. This is allowed despite the fact that the derived-class objects are of different data types. However, the reverse is not true a base-class object is not an object of any of its derived classes. For example, a Point is not a Circle in the hierarchy defined in Chapter 9. If a base-class reference refers to a derived-class object, it is possible to convert the base-class reference to the object s actual data type and manipulate the object as that type. The example in Fig. 10.1 Fig. 10.3 demonstrates assigning derived-class objects to base-class references and casting base-class references to derived-class references. Class Point (Fig. 10.1), which we discussed in Chapter 9, represents an x-y coordinate pair. Class Circle (Fig. 10.2), which we also discussed in Chapter 9, represents a circle and inherits from class Point. Each Circleobject is a Pointand also has a radius (represented via property Radius). We declare method Areaas virtual, so that a derived class (such as class Cylinder) can override method Areato calculate the derived-class object s area. Class PointCircleTest (Fig. 10.3) demonstrates the assignment and cast operations. 1 // Fig. 10.1: Point.cs 2 // Point class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point class definition implicitly inherits from Object 7 public class Point 8 { 9 // point coordinate 10 private int x, y; 11 12 // default constructor 13 public Point() 14 { 15 // implicit call to Object constructor occurs here 16 } 17 18 // constructor 19 public Point( int xValue, int yValue ) 20 { 21 // implicit call to Object constructor occurs here 22 X = xValue; 23 Y = yValue; 24 } 25 26 // property X 27 public int X 28 { 29 get 30 { 31 return x; 32 } 33 Fig. 10.1 Fig. 10.1Fig. 10FiFi.1g. 10.1g. 10.1Pointclass represents an x-y coordinate pair. (Part 1 of 2.)
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Chapter 10 Object-Oriented Programming: Polymorphism 383 Outline 10.1 (Bulletproof web design)

January 11th, 2008

Chapter 10 Object-Oriented Programming: Polymorphism 383 Outline 10.1 Introduction 10.2 Derived-Class-Object to Base-Class-Object Conversion 10.3 Type Fields and switch Statements 10.4 Polymorphism Examples 10.5 Abstract Classes and Methods 10.6 Case Study: Inheriting Interface and Implementation 10.7 sealed Classes and Methods 10.8 Case Study: Payroll System Using Polymorphism 10.9 Case Study: Creating and Using Interfaces 10.10 Delegates 10.11 Operator Overloading Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 10.1 Introduction The previous chapter s object-oriented programming (OOP) discussion focussed on one of OOP s key component technologies, inheritance. In this chapter, we continue our study of OOP polymorphism. Both inheritance and polymorphism are crucial technologies in the development of complex software. Polymorphism enables us to write programs that handle a wide variety of related classes in a generic manner and facilitates adding new classes and capabilities to a system. With polymorphism, it is possible to design and implement systems that are easily extensible. Programs can process objects of all classes in a class hierarchy generically as objects of a common base class. Furthermore, new classes can be added with little or no modification to the generic portions of the program, as long as those classes are part of the inheritance hierarchy that the program processes generically. The only parts of a program that must be altered to accommodate new classes are those program components that require direct knowledge of the new classes that the programmer adds to the hierarchy. In this chapter, we demonstrate two substantial class hierarchies and manipulate objects from those hierarchies polymorphically. 10.2 Derived-Class-Object to Base-Class-Object Conversion Section 9.4 created a point-circle class hierarchy, in which class Circle inherited from class Point. The programs that manipulated objects of these classes always used Point references to refer to Point objects and Circle references to refer to Circle objects. In this section, we discuss the relationships between classes in a hierarchy that enable programs to assign derived-class objects to base-class references a fundamental part of programs that process objects polymorphically. This section also discusses explicit casting between types in a class hierarchy. An object of a derived class can be treated as an object of its base class. This enables various interesting manipulations. For example, a program can create an array of base-class
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

10 Object-Oriented Programming: Polymorphism Objectives To understand (Java web server)

January 10th, 2008

10 Object-Oriented Programming: Polymorphism Objectives To understand the concept of polymorphism. To understand how polymorphism makes systems extensible and maintainable. To understand the distinction between abstract classes and concrete classes. To learn how to create abstract classes, interfaces and delegates. One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them. John Ronald Reuel Tolkien, The Fellowship of the Ring General propositions do not decide concrete cases. Oliver Wendell Holmes A philosopher of imposing stature doesn t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or is not known in the time when he lives. Alfred North Whitehead
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Chapter 9 Object-Oriented (Make web site) Programming: Inheritance 381 ANSWERS TO

January 9th, 2008

Chapter 9 Object-Oriented Programming: Inheritance 381 ANSWERS TO SELF-REVIEW EXERCISES 9.1 a) Inheritance. b) protected. c) is a or inheritance. d) has a or composition or aggregation. e) hierarchical. f) public. g) private. h) internal. i) constructor. j) base. 9.2 a) True. b) True. c) False. A has-a relationship is implemented via composition. An isa relationship is implemented via inheritance. d) False. Overridable methods must be declared explicitly as virtual. e) True. f) False. When a derived class redefines a base-class method using the same signature, the derived class overrides that base-class method. g) False. This is an example of a has a relationship. Class Carhas an is a relationship with class Vehicle. h) True. EXERCISES 9.3 Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite classes Point3, Circle4and 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? 9.4 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. 9.5 Rewrite the case study in Section 9.5 as a Point, Square, Cube program. Do this two ways once via inheritance and once via composition. 9.6 Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateralas 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 object s dimensions and area. 9.7 Modify classes Point3, Circle4 and Cylinder to contain destructors. Then, modify the program of Fig. 9.19 to demonstrate the order in which constructors and destructors are invoked in this hierarchy. 9.8 Write down all the shapes you can think of both two-dimensional and three-dimensional and form those shapes into a shape hierarchy. Your hierarchy should have base class Shape from which class TwoDimensionalShape and class ThreeDimensionalShape are derived. Once you have developed the hierarchy, define each of the classes in the hierarchy. We will use this hierarchy in the exercises of Chapter 10 to process all shapes as objects of base-class Shape. (This is a technique called polymorphism.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.