Question
WRITE IN C++ USE GET LINE IF NEEDED. The lab this week is to implement the example that I have shown in Lecture5_4_Virtual_Fucntions, demonstrating derived
WRITE IN C++ USE GET LINE IF NEEDED.
The lab this week is to implement the example that I have shown in Lecture5_4_Virtual_Fucntions, demonstrating derived classes and virtual functions. Because a child can be passed where a parent is expected we can use a parent pointer to hold a child object. In doing this no information is lost due to slicing. Now, at first it seems that this pointer would only be able to call functions that were part of the parent, but because a virtual function allows a parent function to call a function implemented in the child we are able to call functions from the parent functions, and from the parent pointer, provided that they are labeled as virtual in the parent. It will not compile until you have written the required classes.
You should write a Shape class with a default constructor, an input function (at first without the word virtual), a cost function, and private variables for thickness and cost_per_cubic_unit (you can shorten the name if you like). From this class you are to derive the classes Circle, Rectangle, and Trapezoid. Each of these will have a default constructor, an input function and a function that returns the area of the shape. The private variables and area formulas are:
Circle: radius formula = M_PI*radius*radius (M_PI is defined in the library cmath.)
Rectangle: length & width formula = length*width
Trapezoid: base1, base2, & height- formula = height*(base1+base2)/2.0
In the Shape class the cost function will return
thickness*cost_per*area()
In order for this to work you will need to declare a virtual function for the area in the Shape class, even though it is implemented in the child
When you first run this program you find that all the classes are calling your Shape input function, asking for thickness and cost_per instead of the appropriate measurement. You can fix this by putting the word virtual in front of the input function in the parent. Then you see the child function being called. (The child function can call the parent function using scope resolution, or you can set default values for the thickness and cost_per.)
You can implement all four classes with one .h file and one .cc file.
MAIN.CC----------------------------------
#include "shapes.h" #includeusing namespace std; // Displays the menu to the user and returns the user's choice int menu(); int main(){ Shape * box[15]; Shape *tmp; int used = 0; double total_cost = 0; // Note that this is a totally different use of // =0 than what happens in purely virtual //functions int choice; choice = menu(); while(choice != 4){ if(choice == 1){ tmp = new Circle; } else if(choice == 2){ tmp = new Rectangle; } else if(choice == 3){ tmp = new Trapezoid; } else if (choice == 4){ break; } tmp -> input(); box[used] = tmp; used++; choice = menu(); // read-at-the-bottom, sentinel loop }// end while loop for(int i = 0; i < used; ++i) total_cost += box[i] -> cost(); cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); cout<<"The total cost of this box of blocks is $" < >ans; return ans; }
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