Question

C++  Polymorphism Please read the instruction!! In this exercise, you are given an abstract base class and...

C++  Polymorphism

Please read the instruction!!

In this exercise, you are given an abstract base class and you are asked to provide concrete (non-abstract) derived classes. Then write the test code to test those classes. The test code scans the data entered on the keyboard using the following format.

For the information of a part-time employee, the line starts with the letter P followed by other information separated by commas.

P,empname,hourly rate,hours worked

For the information of a full-time employee, the line starts with the letter U followed by other information separated by commas.

U,empname,salary

You are required to write the function getEmployeeInfo that scans the data from keyboard and creates an object of the appropriate type and return a unique_ptr that 'points' to the object.

In the main function, you can call the getEmployeeInfo function and then call a member function to print the employee info.

For keyboard input,

P,Ivy Oaks,21,20

The output must be

Employee Name: Ivy Oaks
Pay Amount: 420
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

#include<iostream>
#include<string>
#include <sstream>
using namespace std;
class partTimeEmployee{
   public:
       string emp_name;
       int hourly_rate;
       int hours_worked;  
};

class fullTimeEmployee{
   public:
       string emp_name;
       int salary;
};

void getEmployeeInfo(){
   string info;
     
   cout<<"----------------- Enter Employee Details -----------------\n";
   getline(cin,info);
   if(info[0]=='P')
   {
       string word="";
       partTimeEmployee p;
       int j=0;
       for(int i=2;i<info.length();i++)
       {
           if(info[i]==','){
               if(j==0){
                   p.emp_name=word;
               }else if(j==1){
                   stringstream change(word);
                   change>>p.hourly_rate;
               }
               word="";
               j++;
           }  
           else{
               word=word+info[i];
           }
       }
       stringstream change(word);
       change>>p.hours_worked;
       cout<<"----------------------------------------------------------";
       cout<<"\nEmployee Name : "<<p.emp_name;
       cout<<"\nPay Amount : "<<p.hourly_rate*p.hours_worked;
      
   }
   else if(info[0]=='U')
   {
       string word="";
       fullTimeEmployee f;
       int j=0;
       for(int i=2;i<info.length();i++)
       {
           if(info[i]==','){
               if(j==0){
                   f.emp_name=word;
           }
               word="";
               j++;
           }  
           else{
               word=word+info[i];
           }
       }
       stringstream change(word);
       change>>f.salary;
       cout<<"----------------------------------------------------------";
       cout<<"\nEmployee Name : "<<f.emp_name;
       cout<<"\nPay Amount : "<<f.salary;
      
   }
}

int main(){
   getEmployeeInfo();
}

CODE SCREENSHOT :

employeeClass.cpp 1 #include<iostream> 2 #include<string> 3 #include <sstream> 4 using namespace std; 5 class partTimeEmploye1 1 } ; E { [*] employeeClass.cpp 36 } 37 word=; 38 39 } 40 else { 41 word=word+info[i]; 42 } 43 44 stringstream change (w

OUTPUT :

For Part-Time Employee :

E:\HomeworkLib\employeeClass.exe Enter Employee Details P, Ivy Oaks, 21, 20 Employee Name : Ivy Oaks Pay Amount : 420 Process exite

For Full-Time Employee :

E:\HomeworkLib\employeeClass.exe Enter Employee Details U, Ivy Oaks, 2120 Employee Name : Ivy Oaks Pay Amount : 2120 Process exited

Add a comment
Know the answer?
Add Answer to:
C++  Polymorphism Please read the instruction!! In this exercise, you are given an abstract base class and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is In C++ only Thank you 2. Employee is a base class and Hourly Worker...

    This is In C++ only Thank you 2. Employee is a base class and Hourly Worker is a derived class. Which statement about the destructors of both classes is true? al They should be declared virtual u Declaring them virtual or not does not make any difference c) They should be implemented as friend functions d) The destructor of HourlyWorker must be virtual whereas the destructor of Employee should 3. If a base class has a non-virtual member function called...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee {...

    //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee { protected final String name; protected final int id; public Employee(String empName, int empId) { name = empName; id = empId; } public Employee() { name = generatedNames[lastGeneratedId]; id = lastGeneratedId++; } public String getName() { return name; } public int getId() { return id; } //returns true if work was successful. otherwise returns false (crisis) public abstract boolean work(); public String toString() { return...

  • please help code in c++, assignment stats below dashed line ________________________________ In this lab we will...

    please help code in c++, assignment stats below dashed line ________________________________ In this lab we will practice polymorphism using several different classes. Implement your classes in B.h and test them in main.cpp: B.h Define a class B1 with: virtual function vf() non-virtual function f() pure virtual function pvf() Define these functions within class B1 Implement each function to output its name (e.g., B1::vf()). Make the functions public. Derive a class D1 from B1 Define f() override vf() override pvf() Derive...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • I need this in C++. This is all one question. Introduction Your eighth assignment will consist...

    I need this in C++. This is all one question. Introduction Your eighth assignment will consist of two programs, which will involve the use of simple classes. The source code for these problems should be submitted using the naming conventions we specified in class. Please note that your computer programs should comply with the commenting and formatting rules as described in class. For example, there should be a header for the whole program that gives the author's name, class name,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT