Skip to content

4.2 Inheritance: the Shape class

  1. If needed, start from the project up to chapter 3. This contains the classes as you left them at the end of chapter 3.
  2. Create a new class Shape according to the following class diagram:

    Mermaid diagram Mermaid 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() and CalculatePerimeter() are declared virtual so 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;
    - The DescribeSize() method is identical to that of the Circle and Rectangle classes;
    - Add the getter and setter for color;

  3. 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() and CalculateArea() are public. However, they will be overridden in the derived classes;
    • The methods Shape(string color), Shape() and Definition() are public, because they say something specific about the Shape class;
    • The method DescribeSize() is public. This method will disappear from the derived classes.