Question: Shape functions ( 2 0 pts ) [ Difficulty: Easy ] These functions serve to introduce the concept of polymorphism and object - oriented programming.

Shape functions (20 pts)[Difficulty: Easy] These functions serve to introduce the concept of polymorphism and object-oriented programming. You will be programming two types of objects that represent shapes. The rectangle_t type represents a rectangle shape with a width and length. The triangle_t type represents an equilateral triangle with a length. Both types are based on a base shape_t type that corresponds to the base class in an object-oriented language.
Polymorphism is the concept that a pointer to a base object can refer to objects of different sub-types. For example, a shape_t pointer can refer to a rectangle_t or a triangle_t. Object-oriented languages handle this automatically, but in a non-object-oriented language like C, we must emulate this behavior ourselves. This is done by having the shape_t struct located at the beginning of the rectangle_t and triangle_t structs (as shown in pointer.h). Thus, casting a rectangle_t pointer to a shape_t pointer is valid since the beginning of rectangle_t is a shape_t struct (note this is an actual struct, not a pointer to the struct).
To support virtual functions (i.e., shape functions that can either refer to rectangle functions or triangle functions), we use what's known as a virtual function pointer table that contains function pointers that refer to the correct functions. For example, rectangle_t will have function pointers within the shape_t portion that point to rectangle functions, and triangle_t will have function pointers within the shape_t portion that point to triangle functions. Thus, a shape can call the appropriate function by using the virtual function pointer table. For example, the provided shape_area function uses the table to call the appropriate area function for the shape.
Your role in this part of the assignment is to 1) understand how the shapes are manipulated, and 2) implement a set of functions that emulate object-oriented programming in C. The first four functions implement the area and perimeter calculations for rectangles and triangles. The next two functions illustrate the initialization of each shape. The last two functions illustrate how to compare generic shapes, which may either be rectangles, triangles, or some other shape.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!