C++ code is below. #include #include using namespace std; class Product ( int ID; string saledate;
Posted: Thu May 05, 2022 1:21 pm
C++ code is below. #include <iostream> #include <iomanip> using namespace std; class Product ( int ID; string saledate; double price: int daysTaxDelayed; protected: string typ: double vat: public: Product (int i, string dt, double p, int delay) : ID (1), saledate (dt), price (p), daysTaxDelayed (delay) () double caleTax () { double vatax - vat; for (int d daysTaxDelayed: d> 0; d-28) vazax 1.02; return (price vaTax / 100): } int getID () { return ID: ) string getDate () { return saledate; } double getPrice () { return price;) double getVat () (return vat; ) int getDelay () { return daysTaxDelayed; } void setDate (string dt) (saledate= dt; } void setPrice (double p) ( price = p) virtual void setVat () = 0; void setDelay (int d) (dayaTaxDelayed d; } void print () ( cout << fixed << setprecision (2): cout << "ID: << ID << Type: << typ << Sale Date: << saledate << Price: << price: cout << Delay: <<daysTaxDelayed; cout << VAT 8: << vat << Tax: << calefax () << endl; } }; class Printer public Product ( public: Printer (int i, string dt, double p, int delay) Product (i, dt, p, delay) { setVat (): typ "Printer": } void setVat () { vat = 23; } }; class Repair public Product ( public: Repair (int i, string dt, double p, int delay): Product (1, dt, p, delay) { setVat (); typ 'Repair"; } void setVat () { vat = 13; } Product parray (5): parray [0] new Printer (1, "05-02-2022", 145.95, 0); 04-01-2022", 36, 29); "03-20-2022, 163.25, 30); parray [1] = new Repair (2, parray (21 new Printer (3, parray [3] new Repair (4, parray [41= new Printer (5, 01-01-2022, 45.5, 85); "02-25-2022, 155, 28); double totaltax = 0; for (int i = 0; i < 5; i++) { parray [1]->print (); totaltax * parray [1]->calefax ()1 } cout << Total tax bill << totaltax << endl; return 0; }; int main() (
Exercise 3: Now take your solution from exercise 2 and change it again. This time delete all lines where you created the original objects in exercise 1. Instead you are going to create these objects in the same line that you created the pointers as part of exercise 2. To do this write the line parent_class_type *pointer_name = new child_class_type You might have noticed by now that when using the parent class point- ers and arrow notation, you are not able to access functions that were defined in the child classes and were not available to the parent class. To solve this problem add a little bit of code to your parent class definition to convert it to an abstract base class; see notes. Hint: You will need to use the word virtual. Now demonstrate the full functionality of your classes using the arrow notation.
Exercise 2: Now add some code to your solution from exercise 1 to demon- strate polymorphism. Create a number of pointers to the parent class, and set these equal to the addresses of some of the objects you created as part of the first exercise. Once again demonstrate the full functionality of your classes (as much as you can) this time using the parent class pointers and the arrow notation.
Exercise 3: Now take your solution from exercise 2 and change it again. This time delete all lines where you created the original objects in exercise 1. Instead you are going to create these objects in the same line that you created the pointers as part of exercise 2. To do this write the line parent_class_type *pointer_name = new child_class_type You might have noticed by now that when using the parent class point- ers and arrow notation, you are not able to access functions that were defined in the child classes and were not available to the parent class. To solve this problem add a little bit of code to your parent class definition to convert it to an abstract base class; see notes. Hint: You will need to use the word virtual. Now demonstrate the full functionality of your classes using the arrow notation.
Exercise 2: Now add some code to your solution from exercise 1 to demon- strate polymorphism. Create a number of pointers to the parent class, and set these equal to the addresses of some of the objects you created as part of the first exercise. Once again demonstrate the full functionality of your classes (as much as you can) this time using the parent class pointers and the arrow notation.