Question

In: Computer Science

21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate);...

21. Consider the code below: [13]

Employee class:

class Employee {
public:
  Employee(string theName, float thePayRate);
 
protected:
  string getName() const;
  float getPayRate() const;
  float pay(float hoursWorked) const;
 
private:
  string name;
  float payRate;
};
Definitions for some of the methods follow: 
Employee::Employee(string theName, float thePayRate)
{
  name = theName;
     payRate = thePayRate;
}
 
float Employee::pay(float hoursWorked) const
{
 return hoursWorked * payRate;
}
 
Manager Class:
#include "employee.h"
 
class Manager : public Employee {
public:
  Manager(string theName,
          float thePayRate,
          bool isSalaried);
protected:
  bool getSalaried() const;
  float pay(float hoursWorked) const;
 
private:
  bool salaried;
};
 
Some of the function definitions follow:
float Manager::pay(float hoursWorked) const
{
  if (getSalaried())
    return Employee::getPayRate();
  /* else */
  return Employee::pay(hoursWorked);
}
 
Answer the following questions based on the code above:
  1. Class Manager is a ____________ class of base class _________.[2]
  2. Enumerate the private members of class Manager.[3]
  3. What would be the output for the following: [2+2]
 
Employee emp("John Burke", 25.0);
Manager mgr("Jan Kovacs", 1200.0, true);
 
Employee *emp1P = &emp;
cout << "Pay: " << emplP->pay(40.0);
*emp1P = &mgr;
    cout << "Pay: " << emplP->pay(40.0);  
 
 
  1. If the pay() method is declared virtual in both the classes, then what would be the output for the following:[2+2]
 
Employee emp("John Burke", 25.0);
Manager mgr("Jan Kovacs", 1200.0, true);
 
Employee *emp1P = &emp;
cout << "Pay: " << emplP->pay(40.0);  
   *emp1P = &mgr;
 cout << "Pay: " << emplP->pay(40.0);
  1. If the pay() method is NOT declared virtual in class Employee, and the following function is introduced in class Employee only
void Employee:: PrintPay(float hoursWorked)
{
 cout << "Pay: " << pay(hoursWorked) << endl;
}
 
Which version of “pay” would be called in the following call to “PrintPay”? [2]
    Manager mgr;
    mgr.PrintPay(40.0);

Solutions

Expert Solution

a.Class Manager is a child class of base class Employee

(Manager class is derived publicly from the base class Employee)

b.Enumerate the private members of class Manager.

Manager class has only one private member variable, that is a boolean variable salaried. As the Manager is derived publicly, all the public members of Employee become public members of Manager, and all the protected members of Employee become protected members of Manager, private members are not inherited. And if the Manager was derived using protected mode, then all the protected members of the Employee class would have been the private members of Manager class.

c.What would be the output for the following code:

Employee emp(“John Burke”,25.0)

Manager mgr(“Jan Kovacs”,1200.0,true)

Employee *emp1P=&emp;

cout<<”Pay: ”<< emp1P->pay(40.0);

cout<<”Pay: ”<< emp1P->pay(40.0);

The above code will not compile due to many reasons. First of all, pay() is the protected member method of Employee class as well as the Manager class. A protected member can be ONLY accessed by member functions or by a friend class. Secondly, on the line 5, it will show an error that ‘no known conversion from Manager* to const Employee&’, it will work only if we remove the ‘*’ before emp1P on line 5. If we solve all these problems and then execute the code, both calls will invoke the base class method pay() only. If we want to invoke the child class method like this, we have to declare pay() as the virtual function in the super class.

A) Output:

if the pay() method is declared as virtual

Pay1000Pay1200
Pay1000 is for Employee John Burke (25.0*40) and Pay1200 is for Manager 'Jan Kovacs' as salaried = true, so it will return payRate.

A) Output:

if the pay() method is not declared virtual

Pay1000Pay48000
For Manager ,48000(40*1200) will be displayed

C)

Employee::Pay()

printPay() is declared in Employee class . So Pay() function of Employee class will be called from printPay()

Employee::Pay()

printPay() is declared in Employee class . So Pay() function of Employee class will be called from printPay()

Hope these answers helpful

Please kindly give your valuable upvotes please it helps me alot


Related Solutions

Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT