Computing II Lab-1 Suppose you have a class Sale and there are two types of Sale
ID: 3917497 • Letter: C
Question
Computing II Lab-1 Suppose you have a class Sale and there are two types of Sale - OnlineSale class and StoreSale class (inherited from Sale). Suppose you have a method, bill0, which is pure virtual in Sale and a virtual method, calculate Tax0, in Sale. Suppose that tax is 10% in online sales and 20% in store sales. Suppose that bill in online sales includes address also (not required in store sales). Suppose that StoreSale is of two types WholesaleStoreSale and RetailStoreSale (inherited from StoreSale). StoreSale has a (non virtual) method calculateDiscounted Value which calculates the discount of 25% on wholesale store sale and discount of 10% on retail store sale. (You may other assume more information to make your program 1. Can you make objects of class Sale? Why or why not? 2. Can we leave a pure virtual function not implemented in output more presentable.) Write a test for it. derived class (considering one descendent)? What about the case of virtual function? 3. Is it necessary for all virtual functions that are non-pure to have some definition? 4. Which of the methods in our example provide late binding? 6. Which is overriding in the above example? Display values 7. Which is redefining in the above example? Display values 8. Print any output from online sale, wholesale store sale and 9. Suppose we do not want a virtual function to be overridden. 5. Is calculateTax0 automatically virtual in StoreSale class? from base and from override to distinguish. from base and from override to distinguish. retail store sale. Which keyword will we add? Submit your written answers in a text file (to subhajit_chakrabarty 1@uml.edu ) and show your display to the instructor.Explanation / Answer
1. If we try to create an object for the class which has a pure virtual function, it will not get created.
Pure virtual function will be of type:
virtual void bill()=0;
A class containing pure virtual function is called as an abstract class. Abstract class cann't be instantiated. Hence object can't be created for base class Sales which is an abstract class as it contains pure virtual function bill().
2.If we leave the pure virtual function without implementing in the derived class as well, then that derived class as well will become as abstract class. And if there is only one descendent of the base class, then no objects can be created for either of the base class or derived class and they becomes obsolete(useless). So inorder to create objects, the derived class should have the implementation of the pure virtual function.
In case of virtual function, eventhough virtual functions are not implemented in derived classes, it has to be implemented in base class.
3. Yes it is necessary to define some definition to all non-pure virtual functions in the base class. As for some derived class will not require that functionality, that class will not implement it. In that case if the definition is present in base class other derived class can inherit that functionality.
4. calculateTax() and bill() functions provide late binding. With the keyword virtual both the functions will be binded at run time, based on the content of the pointer, the binding is done.
Sales *s;
storeSales *ss;
s=&ss;
s->bill();
will get the bill function defined in the storeSales derived class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.