Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Visual C# (Made in a Console App) 11.3 (Composition vs. Inheritance) Many apps written with inheritance could be written with composition instead, and vice cersa.
Visual C# (Made in a Console App)
11.3 (Composition vs. Inheritance) Many apps written with inheritance could be written with composition instead, and vice cersa. Rewrite class BasePlusCommissionEmployee (Fig. 11.11) of the CommissionEmployee-BasePlusCommissionEmployee hierarchy to use composition rather than in-heritance.
Derived Class BasePlus Commission Employee Class BasePlus Commission Employee (Fig. 11.11) has several changes to its method imple- mentations that distinguish it from the version in Fig. 11.9. Methods Earnings (Fig. 11.11, line 43) and ToString (lines 46-47) each invoke property Base Salary s get accessor to obtain the base-salary value, rather than accessing baseSalary directly. If we decide to rename instance variable baseSalary, only the body of property BaseSalary will need to change. I // Fig. 11.11: BasePlus Commission Employee.cs 2 // BasePlusCommission Employee inherits from Commission Employee and has // controlled access to Commission Employee s private data via // its public properties. using System; te lines 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class BasePlus Commission Employee Commission Employee { private decimal baseSalary; // base salary per week // six-parameter derived-class constructor // with call to base class Commission Employee constructor public BasePlus Commission Employee (string firstName, string lastName, string social SecurityNumber, decimal gross Sales, decimal commission Rate, decimal baseSalary) : base (firstName, lastName, social SecurityNumber, grossSales, commission Rate) BaseSalary baseSalary; // validates base salary { } mo)logusn} // property that gets and sets // BasePlus Commission Employee s base salary public decimal BaseSalary { get { } set { return baseSalary; sulsy couleversanotaaimmo if (value < 0) // validation { 15311 Man oring Fig. 11.11 BasePlusCommission Employee inherits from Commission Employee and has access to Commission Employee s private data via its public properties. (Part I of 2.) 35 36 37 38 39 40 31 12 43 14 -5 6 7 8 } throw new ArgumentOutOfRange Exception (nameof (value), value, ${nameof (BaseSalary)} must be >= 0 ); baseSalary = value; // calculate earnings public override decimal Earnings () => Base Salary + base. Earnings(); // return string representation of BasePlusCommission Employee public override string ToString() => $ base-salaried {base.ToString()}base salary: {BaseSalary:C} ; .11.11 | BasePlus Commission Employee inherits from Commission Employee and has ess to Commission Employee s private data via its public properties. (Part 2 of 2.)
Step by Step Solution
★★★★★
3.45 Rating (152 Votes )
There are 3 Steps involved in it
Step: 1
Answer USe this code using composition not inheritance using System public class CommissionEmployee private string firstName private string lastName private string socialSecurityNumber private decimal ...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