Answered step by step
Verified Expert Solution
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 ptsDifficulty: Easy These functions serve to introduce the concept of polymorphism and objectoriented programming. You will be programming two types of objects that represent shapes. The rectanglet type represents a rectangle shape with a width and length. The trianglet type represents an equilateral triangle with a length. Both types are based on a base shapet type that corresponds to the base class in an objectoriented language.
Polymorphism is the concept that a pointer to a base object can refer to objects of different subtypes. For example, a shapet pointer can refer to a rectanglet or a trianglet Objectoriented languages handle this automatically, but in a nonobjectoriented language like C we must emulate this behavior ourselves. This is done by having the shapet struct located at the beginning of the rectanglet and trianglet structs as shown in pointer.h Thus, casting a rectanglet pointer to a shapet pointer is valid since the beginning of rectanglet is a shapet struct note this is an actual struct, not a pointer to the struct
To support virtual functions ie 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, rectanglet will have function pointers within the shapet portion that point to rectangle functions, and trianglet will have function pointers within the shapet 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 shapearea function uses the table to call the appropriate area function for the shape.
Your role in this part of the assignment is to understand how the shapes are manipulated, and implement a set of functions that emulate objectoriented 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started