Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions