To implement the BasePlusCommissionEmp class using composition instead of inheritance and invoke different functions in the test program subsequently.
Explanation of Solution
Program:
// BasePlusCommissionEmp class definition . #ifndef BP_COMMISSION_H #define BP_COMMISSION_H #include<string>// C++ standard string class usingnamespacestd; classBasePlusCommissionEmp { public: BasePlusCommissionEmp(conststring&, conststring&, conststring&, double = 0.0,double = 0.0, double = 0.0 ); //For generic attributes of Employee voidsetFirstName( conststring& ); // set first name stringgetFirstName() const; // return first name voidsetLastName( conststring& ); // set last name stringgetLastName() const; // return last name voidsetSocialSecurityNumber( conststring& ); // set SSN stringgetSocialSecurityNumber() const; // return SSN // additional functions for attributes of CommisionEmployee voidsetGrossSales( double ); // set gross sales amount doublegetGrossSales() const; // return gross sales amount voidsetCommissionRate( double ); // set commission rate doublegetCommissionRate() const; // return commission rate //additional functions for baseSalary voidsetBaseSalary( double ); // set base salary doublegetBaseSalary() const; // return base salary // Generic functions of Employee doubleearnings() const; voidprint() const; private: //Generic attributes of Employee stringfirstName; // composition: member object stringlastName; // composition: member object stringsocialSecurityNumber; //composition: member object //attributes of CommisionEmployee doublegrossSales; // gross weekly sales doublecommissionRate; // commission percentage //attribute for BaseSalary doublebaseSalary; // base salary }; // end class BasePlusCommissionEmp #endif //BasePlusCommisionEmp.cpp /* BasePlusCommissionEmp.cpp using composition Created on: 31-Jul-2018 :rajesh@acroknacks.com */ #include<string>// C++ standard string class #include"BasePlusCommissionEmp.h" #include<iostream> usingnamespacestd; BasePlusCommissionEmp::BasePlusCommissionEmp(conststring&fname, conststring&lname, conststring&ssn1, doublebaseSalary, doublegrossSales , doublecomRate ) :firstName (fname), lastName ( lname),socialSecurityNumber (ssn1 ) { setBaseSalary(baseSalary ); // validate and store base salary setGrossSales(grossSales);//validate and store gross sales setCommissionRate(comRate);//validate and store commision rate }// end constructor /&Functions Below are specific to This class */ // set gross sales amount voidBasePlusCommissionEmp::setGrossSales( double sales ) { if ( sales **gt;= 0.0 ) grossSales = sales; else throwinvalid_argument( "Gross sales must be >= 0.0" ); } // end function setGrossSales // return gross sales amount doubleBasePlusCommissionEmp::getGrossSales() const { returngrossSales; } // end function getGrossSales // set commission rate voidBasePlusCommissionEmp::setCommissionRate( double rate ) { if ( rate > 0.0 && rate < 1.0 ) commissionRate = rate; else throwinvalid_argument( "Commission rate must be > 0.0 and < 1.0" ); } // end function setCommissionRate doubleBasePlusCommissionEmp::getCommissionRate() const { returncommissionRate; } // end function getCommissionRate voidBasePlusCommissionEmp::setBaseSalary( double salary ) { if ( salary >= 0.0 ) baseSalary = salary; else throwinvalid_argument( "Salary must be >= 0.0" ); } // end function setBaseSalary // return base salary doubleBasePlusCommissionEmp::getBaseSalary() const { returnbaseSalary; } // end function getBaseSalary //compute earnings doubleBasePlusCommissionEmp::earnings() const { return ( (getCommissionRate() * getGrossSales()) + getBaseSalary()) ; } // end function earnings // print CommissionEmployee object voidBasePlusCommissionEmp::print() const { cout<<"\nBasePlusCommission employee: "; cout<<lastName<<", "<<firstName<<endl; cout<<"SSN : "<<socialSecurityNumber<<endl; cout<<"\n gross sales: $ "<<getGrossSales() <<"\n Base Salary: $ "<<getBaseSalary() <<"\n commission rate: "<<getCommissionRate() ; } // end function print /&Generic Employee functions **/ // set first name voidBasePlusCommissionEmp::setFirstName( conststring**first ) { firstName = first; // should validate } // end function setFirstName // return first name stringBasePlusCommissionEmp::getFirstName() const { returnfirstName; } // end function getFirstName // set last name voidBasePlusCommissionEmp::setLastName( conststring&last ) { lastName = last; // should validate } // end function setLastName // return last name stringBasePlusCommissionEmp::getLastName() const { returnlastName; } // end function getLastName // set social security number voidBasePlusCommissionEmp::setSocialSecurityNumber( conststring&ssn ) { socialSecurityNumber = ssn; // should validate } // end function setSocialSecurityNumber // return social security number stringBasePlusCommissionEmp::getSocialSecurityNumber() const { returnsocialSecurityNumber; } // end function getSocialSecurityNumber
Test Program
// Testing class BasePlusCommissionEmp. #include<iostream> #include<iomanip> #include"BasePlusCommissionEmp.h"// BasePlusCommissionEmp class definition usingnamespacestd; intmain() { // instantiate a BasePlusCommissionEmp object BasePlusCommissionEmpemployee("Sue", "Jones", "222-22-2222",1500,10000,0.16 ); // get commission employee data cout<<"Employee information obtained by get functions: \n" <<"\nFirst name is "<<employee.getFirstName() <<"\nLast name is "<<employee.getLastName() <<"\nSocial security number is " <<employee.getSocialSecurityNumber() <<"\nBase Salary is $"<<employee.getBaseSalary() <<"\nGross sales is $"<<employee.getGrossSales() <<"\nCommission rate is $"<<employee.getCommissionRate() <<endl; cout<<"Earnings based on current Data : $"<<employee.earnings(); //Modify Sales data employee.setGrossSales( 8000 ); // set gross sales employee.setCommissionRate( .1 ); // set commission rate cout<<"\nUpdated employee information output by print function: \n" <<endl; employee.print(); // display the new employee information // display the employee's earnings cout<<"\n\n Updated Employee's earnings: $"<<employee.earnings() <<endl; } // end main
Explanation:
Theabove program demonstrates composition as an alternate way of implementing functionality in Object oriented
Composition increases duplicity of code as seen in BasePlusCommissionEmp class where a large part of attributes and functions of the Employee class have to be repeated in the BasePlusCommissionEmp class.
Also, the test code or the actual application using these objects becomes more complicated because the use of Data structures like
If there are a large number of objects with similar functionality and a little variances, the redundant code soon becomes prone to defect and maintenance nightmares. On the other hand, composition provides more control at the compile time by limiting common access modifier errors and method overriding errors during development time.
The has-a relationship is suited mostly where there is limited or no commonality in attributes and functionality of the objects being modelled.
It’s always better to create an is-a class hierarchy whenthe objects being modelled are having a lot of common attributes and method, resulting in a generic common subset (the base class) and other derived class specializing form it. Inheritance makes the code cleaner to write, read and maintain.
Sample Output:
Employee information obtained by get functions: First name is Sue Last name is Jones Social security number is 222-22-2222 Base Salary is $1500 Gross sales is $10000 Commission rate is $0.16 Earnings based on current Data : $3100 Updated employee information output by print function: BasePlusCommission employee: Jones, Sue SSN : 222-22-2222 gross sales: $ 8000 Base Salary: $ 1500 commission rate: 0.1 Updated Employee's earnings: $2300
Want to see more full solutions like this?
Chapter 19 Solutions
C How to Program (8th Edition)
- What are the similarities and differences between massively parallel processing systems and grid computing. with referencesarrow_forwardModular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forwardBased on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward
- Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forwardQuestion: Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward23:12 Chegg content://org.teleg + 5G 5G 80% New question A feed of 60 mol% methanol in water at 1 atm is to be separated by dislation into a liquid distilate containing 98 mol% methanol and a bottom containing 96 mol% water. Enthalpy and equilibrium data for the mixture at 1 atm are given in Table Q2 below. Ask an expert (a) Devise a procedure, using the enthalpy-concentration diagram, to determine the minimum number of equilibrium trays for the condition of total reflux and the required separation. Show individual equilibrium trays using the the lines. Comment on why the value is Independent of the food condition. Recent My stuff Mol% MeOH, Saturated vapour Table Q2 Methanol-water vapour liquid equilibrium and enthalpy data for 1 atm Enthalpy above C˚C Equilibrium dala Mol% MeOH in Saturated liquid TC kJ mol T. "Chk kot) Liquid T, "C 0.0 100.0 48.195 100.0 7.536 0.0 0.0 100.0 5.0 90.9 47,730 928 7,141 2.0 13.4 96.4 Perks 10.0 97.7 47,311 87.7 8,862 4.0 23.0 93.5 16.0 96.2 46,892 84.4…arrow_forward
- You are working with a database table that contains customer data. The table includes columns about customer location such as city, state, and country. You want to retrieve the first 3 letters of each country name. You decide to use the SUBSTR function to retrieve the first 3 letters of each country name, and use the AS command to store the result in a new column called new_country. You write the SQL query below. Add a statement to your SQL query that will retrieve the first 3 letters of each country name and store the result in a new column as new_country.arrow_forwardWe are considering the RSA encryption scheme. The involved numbers are small, so the communication is insecure. Alice's public key (n,public_key) is (247,7). A code breaker manages to factories 247 = 13 x 19 Determine Alice's secret key. To solve the problem, you need not use the extended Euclid algorithm, but you may assume that her private key is one of the following numbers 31,35,55,59,77,89.arrow_forwardConsider the following Turing Machine (TM). Does the TM halt if it begins on the empty tape? If it halts, after how many steps? Does the TM halt if it begins on a tape that contains a single letter A followed by blanks? Justify your answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning