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...
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.
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...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT