diff_months: 10

COMP 5421-BB Inheritance And Runtime Polymorphism Assignment

Download Solution Now
Added on: 2023-07-27 04:45:05
Order Code: clt317844
Question Task Id: 0
  • Subject Code :

    COMP-5421-BB

  • Country :

    Australia

1 Purpose

  • Implement an inheritance hierarchy of classes in C++
  • Learn about virtual functions, overriding, and polymorphism in C++
  • Use two-dimensional arrays using vector, one of the simplest container class templates in the C++ Standard Template Library (STL)
  • Use modern C++ smart pointers, which automate the process of resource deallocation

2 Overview

Using simple two-dimensional geometric shapes, this assignment will give you practice with the fundamental principles of object-oriented programming (OOP).

The assignment starts by abstracting the essential attributes and operations common to four geometric shapes of interest in this assignment, namely, rhombus, rectangle, and two kinds of triangle shapes.

You will then be tasked to implement the shape abstractions using the C++ features that support encapsulation, information hiding, inheritance and polymorphism.

In addition to implementing the shape classes, you will be tasked to implement a Canvas class whose objects can be used by the shape objects to draw on.

The four geometric shapes of interest in this assignment can be textually rendered into visually identifiable images on the computer screen; for example:

shapes1-1690432149.jpg

3 Modeling 2D Geometric Shapes

3.1 Common Attributes: Data

height

the length of the vertical attribute of the shape, a positive integer

width

the length of the horizontal attribute of the shape, a positive integer

name

a string object; for example, “Book” for a rectangular shape

pen

a character to draw the shape with ID number a unique positive integer, distinct from that of all the other shape objects

3.2 Common Operations: Interface

  1. A constructor that accepts as parameters the initial values of a shape’s height, width, name, and pen data members
  2. Five accessor (getter) member-functions, one for each attribute
  3. Four mutator (setter) member-functions for setting the name, height, width and pen data members
  4. A toString() member-function that returns a std::string representation of the Shape object invoking it
  5. An overloaded polymorphic output operator <<
  6. A member-function areaGeo() that computes and returns the shape’s geometric area
  7. A member-function preimeterGeo() that computes and returns the shape’s geometric perimeter
  8. A member-function areaScr() that computes and returns the shape’s screen area, the number of characters forming the textual image of the shape
  9. A member-function preimeterScr() that computes and returns the shape’s screen perimeter, the number of characters on the borders of the textual image of the shape
  10. A member-function that draws a textual image of the shape on a Canvas object using the shape’s pen character

4 Modeling Specialized 2D Geometric Shapes

There are several ways to classify 2D shapes, but we use the following, which is specifically designed for you to gain experience with implementing inheritance and polymorphism in C++:

shapes2-1690432152.jpg

Encapsulating the attributes and operations common to all shapes, the Shape class must necessarily be abstract because the shapes it models are so general that it simply would not know how to implement several of the operations specified in section 3.2.

As a base class, Shape serves as a common interface to all classes in the class hierarchy.

As an abstract class, Shape enables polymorphism, allowing variables of types Shape* and Shape& to make polymorphic calls. Similarly, the class Triangle must be abstract, since it has no knowledge about the shapedependent data and operations of the shapes it generalizes.

Classes Rectangle, Rhombus, RightTriangle and AcuteTriangle are concrete because they each fully implement their respective interface.

5 Concrete Shapes

The specific characteristic properties of our concrete shapes are listed in the following table.

shapes3-1690432157.jpg

6 Task 1 of 2

Implement the Shape inheritance class hierarchy described above. It is completely up to you to decide which operations should be virtual, pure virtual, or non-virtual, provided that it satisfies a few simple requirements.

The amount of coding required for this task is not a lot as your shape classes will be small. Be sure that common behavior (shared operations) and common attributes (shared data) are pushed toward the top of your class hierarchy.

You may add private member functions to facilitate your operations, but you may not add data members other than those given in the attribute row of Table on page 4.

6.1 Requirements

  • The unit of length is a single character; thus, all shape attributes such as height, width, base, and diagonal are measured in characters.
  • At construction, a Rectangle shape requires the values of both its height and width, whereas the other three concrete shapes each require a single value for the length of their respective horizontal attribute.
  • The constructor of Rhombus must select the next integer if the supplied value for its diagonal is not odd.
  • The constructor of AcuteTriangle must select the next integer if the supplied value for its base length is not odd.

7 Some Examples

shapes4-1690432160.jpg

The call rect.toString() on line 2 of the source code generates the entire output shown.

However, note that line 4 would produce the same output as the overloaded output operator itself internally would call toString().

Line 3 of the output shows that rect’s ID number is 1. The ID number of the next shape will be 2, the one after 3, and so on. These unique ID numbers are generated and assigned when shape objects are first constructed.

Lines 4-5 of the output show object rect’s name and pen character, and lines 6-7 show rect’s height and width, respectively. Now let’s see how rect’s static and dynamic types are produced on lines 12-13 of the output.

To get the name of the static type of a pointer p at runtime you use typeid(p).name(), and to get its dynamic type you use typeid(*p).name(). That’s exactly what toString() does using this instead of p. You need to include the header for this.

Lines 12-13 show that rect’s static type name is PK5Shape and it’s dynamic type name is 9Rectangle. The actual names returned by these calls are implementation defined. For example, the output above was generated under g++ (GCC) 10.2.0, where PK in PK5Shape means “pointer to konst const”, and 5 in 5Shape means that the name “Shape” that follows it is 5 character long.

Your C++ compiler may generate different text to indicate the static and dynamic types of a pointer. Microsoft VC++ 2022 produces a more readable output as shown below.

shapes5-1690432165.jpg

Notice that in line 6, the supplied height 16 is invalid because it is even; to correct it, Rhombus’s constructor uses the next odd integer, 17, as the diagonal of object ace.

Again, lines 7 and 9 would produce the same output; the difference is that the call to toString() is implicit in line 9.

Here are examples of AcuteTriangle and RightTriangle shape objects.

shapes6-1690432169.jpg

7.1 Polymorphic Magic

Note that on line 22 in the source code above, rt is a regular object variable, as opposed to a pointer (or reference) variable pointing to (or referencing) an object; as such, rt cannot make polymorphic calls. That’s because in C++ the calls made by a regular object, such as rect, ace, at, and rt, to any function (virtual or not) are bound at compile time (early binding).

Polymorphic magic happens through the second argument in the calls to the output operator<< at>

ostream& operator<< (ostream& out, const Shape& shp);

Specifically, rt in line 23 is bound to the parameter shp, which is a reference, and as such, shp can call virtual functions of Shape polymorphically; in other words, the decision as to which virtual function to run depends on the type of the object referenced by shp at run time (late binding). For example, if shp references a Rhombus object, then the call shp.areaGeo() binds to Rhombus::areaGeo(), if shp references a Rectangle object, then shp.areaGeo() binds to Rectangle::areaGeo(), and so on.

Now, consider the call rt.toString() on line 25. Since, Shape::toString() is non-virtual, the call rt.toString() is bound at compile time (early binding). However, the object rt in the call rt.toString() is represented inside the function Shape::toString() through this, a pointer of type Shape*, which can in fact call virtual functions of Shape polymorphically.

7.2 Shape’s Draw Function

virtual Canvas draw() const = 0; // concrete derived classes must implement it

Introduced in Shape as a pure virtual function, the draw() function forces concrete derived classes to implement it.

Defining a local Canvas object like so

Canvas can { getHeight(), getWidth() };

the draw function draws on can using its put members function, something like this:

can.put(r, c, penChar); // write penChar in the cell at row r and column c

A Canvas object models a two-dimensional grid as abstracted in the Figure at right. The rows of the grid are parallel to the x-axis, with row numbers increasing down. The columns of the grid are parallel to the y-axis, with column numbers increasing to the right. The origin of the grid is located at the top-left grid cell (0, 0) at row 0 and column 0.

7.3 Examples Continued

shapes7-1690432174.jpgshapes8-1690432178.jpgshapes9-1690432225.jpg shapes10-1690432235.jpg

A Canvas object can be flipped both vertically and horizontally:

7.5 Using Smart Pointers to Shape objects

Now, let’s create a vector of smart pointers pointing to concrete shape objects and draw them polymorphically:

shapes11-1690432239.jpgshapes12-1690432242.jpg

8 Task 2 of 2

Implement a Canvas class using the following declaration. Feel free to introduce other private member functions, but no data members, of your choice to facilitate the operations of the other members of the class.

shapes13-1690432247.jpg

Deliverables

Header files:

Shape.h, Triangle.h, Rectangle.h, Rhombus.h, AcuteTriangle.h, RightTriangle.h, Canvas.h,

Implementation files:

Shape.cpp, Triangle.cpp, Rectangle.cpp, Rhombus.cpp, Acute Triangle.cpp, RightTriangle.cpp, Canvas.cpp, and ShapeTest Driver.cpp

README.txt

A text file (see the course outline).

9 Specific Grading scheme

Task 1: 60% The Shape classes

Task 2: 40% The Canvas class

10 General Grading scheme

shapes14-1690432251.jpg

11 Sample Test Driver

11.1 ShapeTestDriver.cpp

shapes15-1690432255.jpg

11.2 Drawing Front View of a House

shapes15-1690432259.jpgshapes16-1690432263.jpgshapes17-1690432267.jpgshapes18-1690432271.jpgshapes19-1690432275.jpgshapes19-1690432285.jpgshapes20-1690432286.jpg

11.3 Output

For the sake of brevity, the string representation of the shape objects printed on line 135 are not shown.

shapes21-1690432288.jpg

Are you struggling to keep up with the demands of your academic journey? Don't worry, we've got your back! Exam Question Bank is your trusted partner in achieving academic excellence for all kind of technical and non-technical subjects.

Our comprehensive range of academic services is designed to cater to students at every level. Whether you're a high school student, a college undergraduate, or pursuing advanced studies, we have the expertise and resources to support you.

To connect with expert and ask your query click here Exam Question Bank

  • Uploaded By : Katthy Wills
  • Posted on : July 27th, 2023
  • Downloads : 0
  • Views : 88

Download Solution Now

Can't find what you're looking for?

Whatsapp Tap to ChatGet instant assistance

Choose a Plan

Premium

80 USD
  • All in Gold, plus:
  • 30-minute live one-to-one session with an expert
    • Understanding Marking Rubric
    • Understanding task requirements
    • Structuring & Formatting
    • Referencing & Citing
Most
Popular

Gold

30 50 USD
  • Get the Full Used Solution
    (Solution is already submitted and 100% plagiarised.
    Can only be used for reference purposes)
Save 33%

Silver

20 USD
  • Journals
  • Peer-Reviewed Articles
  • Books
  • Various other Data Sources – ProQuest, Informit, Scopus, Academic Search Complete, EBSCO, Exerpta Medica Database, and more