4.2 Inheritance: the Shape class
- If needed, start from the project up to chapter 3. This contains the classes as you left them at the end of chapter 3.
-
Create a new class
Shapeaccording to the following class diagram:+means public
-means private (not used here)
#means protected (available to the class and all derived classes)
<<constant>>means constant field (PascalCase per C# conventions)
Underlined means static
In UML, virtual operations are often shown in italic; in C#,CalculateArea()andCalculatePerimeter()are declaredvirtualso derived classes can override them.Use the following extra information:
- The default value for color is "white" and since the default value is used in more than one place, make it a constant;
- Use constructor chaining;
- A shape is a collection of points;
- The perimeter is unknown, so return 0;
- The area is unknown, so return 0;
- TheDescribeSize()method is identical to that of theCircleandRectangleclasses;
- Add the getter and setter for color; -
Note: The
Definition()method is static. It therefore cannot be overridden in the derived classes. You can redefine a static method within the derived classes.- The three attributes are protected and therefore available to the derived classes;
- The methods
CalculatePerimeter()andCalculateArea()are public. However, they will be overridden in the derived classes; - The methods
Shape(string color),Shape()andDefinition()are public, because they say something specific about the Shape class; - The method
DescribeSize()is public. This method will disappear from the derived classes.