MCC - CIS162AD
CS9 Inheritance - 15 points![]()
This project is based on CS8 Classes. In this assignment students should create a 3rd class, clsOrderPreferred, that inherits clsOrder. The new class will be used to process orders for preferred customers, and will include a 5% discount in the calculation of extended price.
How to Complete Assignment:
- Students should open project CS8; a new project is not required.
- Edit clsOrder and make calcExtendedPrice virtual so it can be overridden.
- Add a new class, clsOrderPreferred, which will inherit clsOrder.
On the menu click on Project and then scroll down and select Add Class (see page 362).
Copy and paste the partial code provided below for clsOrderPreferred.- Define the default and overloaded constructors for clsOrderPreferred. See the UML diagram below for additional information.
- Apply the changes provided below to CS8Form.cs to test the new class (clsOrderPreferred). In order for the source code to work as provided, you should use the variable and method names provided in the UML Class Diagram.
- Test the program with the test data provided below.
Partial Code for clsOrderPreferred:
//Your Name namespace CS8 { class clsOrderPreferred : clsOrder { private const decimal cdecDISCOUNT_RATE = 0.05M; //declare default constructor - call base default constructor //declare overloaded constructor - call base overloaded constructor //override definition of virtual definition in base public override void calcExtendedPrice() { cdecExtendedPrice = cintQuantity * (cdecPrice - cdecPrice * cdecDISCOUNT_RATE); } }//end of class }//end of classUML - Class Diagrams:
Symbol definitions:
- private
+ public
# protected
* shared/static
% read-only
clsOrder < Inherited By clsOrderPreferred # string cstrDescription
# int cintQuantity
# decimal cdecPrice
# decimal cdecExtendedPrice
(Shared Variables)
# * decimal cdecTotalPrice
# * int cintTotalCount
(Const Variables)
# decimal cdecDISCOUNT_RATE = 0.05M
+ clsOrder( )
default constructor
+ clsOrder(string strDescription,
int intQuantity, decimal decPrice)
+ set & get string Description
+ set & get int Quantity
+ set & get decimal Price
(read-only property)
+ % get decimal ExtendedPrice
(Shared read-only property)
+ * % decimal get TotalPrice
+ * % int get TotalCount
(Supporting Methods)
+ virtual void calcExtendedPrice( )
+ void accumulateTotals( )
+ * void resetTotals( )
+ clsOrderPreferred( )
: base( )
call default constructor in base
+ clsOrderPreferred(string strDescription, int intQuantity, decimal decPrice)
: base(strDescription, intQuantity, decPrice)
Instead of assigning values here pass the arguments
to the overloaded constructor in the base which
already has the code to assign the values.
(Supporting Methods)
override definition of virtual definition in base
+ override void calcExtendedPrice( )
Partial Code for CS8Form:
//Replace the existing btnCalculate method with the following:private void btnCalculate_Click(object sender, EventArgs e) { string strMailingLabel; try { if (chkPreferredDiscount.Checked == true) { //Create an instance of clsOrderPreferred clsOrderPreferred cobjOrder = new clsOrderPreferred (txtDescription.Text, int.Parse(txtQuantity.Text), decimal.Parse(txtPrice.Text)); //Calculate Extended Price cobjOrder.calcExtendedPrice(); //Accumulate Totals cobjOrder.accumulateTotals(); //Display Extended Price lblExtension.Text = cobjOrder.ExtendedPrice.ToString("C"); } else { //Create an instance of clsOrder clsOrder cobjOrder = new clsOrder (txtDescription.Text, int.Parse(txtQuantity.Text), decimal.Parse(txtPrice.Text)); //Calculate Extended Price cobjOrder.calcExtendedPrice(); //Accumulate Totals cobjOrder.accumulateTotals(); //Display Extended Price lblExtension.Text = cobjOrder.ExtendedPrice.ToString("C"); }//end if-else //Create an instance of clsCustomer using the overloaded constructor clsCustomer cobjCustomer = new clsCustomer(txtName.Text, txtStreet.Text, txtCity.Text, txtState.Text, txtZip.Text); //Build mailing label using the Get methods for Customer. strMailingLabel = cobjCustomer.Name + "\n" + cobjCustomer.Street + "\n" + cobjCustomer.City + ", " + cobjCustomer.State + " " + cobjCustomer.Zip; //Display mailing address lblMailingLabel.Text = strMailingLabel; //Shared properties can be accessed using class name //Object names are declared locally in if statement //Test the Get Property methods of ReadOnly Shared properties lblTotalCount.Text = clsOrder.TotalCount.ToString("N0"); lblTotalPrice.Text = clsOrder.TotalPrice.ToString("C"); }//end of try catch (Exception ex) { MessageBox.Show("Error :" + ex.Message + "\n" + ex.StackTrace, "Try/Catch - Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }//end of catch }//end of btnCalculateTest Data:
Enter any name and address to see if the data displays as a mailing label.
Regular Order without discount:
Description Quantity Price Extension Flower Pot 2 19.95 39.90 Mailbox 1 24.95 24.95 Planter 3 21.95 65.85
Regular Order: 3 130.70 Preferred Discount applied to test data:
Description Quantity Price Extension Flower Pot 2 19.95 37.91 Mailbox 1 24.95 23.70 Planter 3 21.95 62.56
Preferred Discount: 3 124.17 Debug Option:
Consider walking through all of the class methods using the debugger. Set a break on the if statement inside of btnCalculate, and then step through (F11) the program to see the constructors and methods of the base and inherited classes.Submit the modified CS8Form.cs, CS8Form.Designer.cs, clsOrder.cs and the new clsOrderPreferred.cs
Adopted from Programming in C#.Net, by Bradley and Millspaugh, 2004
Revised: 03/01/2012 - www.mesacc.edu/~marquez/cis162ad/cs9_inheritance.html