Bài tập C++ OOP: tạo các interface và class tương ứng | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create classes as in the following class diagram:

Explanation:

The above class diagram describes a simple program used to calculate the full-time and part-time salary of employee

Employee abstract class

  • This is an abstract class that stores general information about employees.
  • name, paymentPerHour are the properties that indicates name and the amount of money the employee receives per working hour.
  • Employee(name: String, paymentPerHour: int) is a parameter constructor used to initialize values for properties.
  • setName(), getName(), setPaymentPerHour(), getPaymentPerHour() are setter and getter.
  • calculateSalary() is the abstract class which is used to calculate employees' salary.

PartTimeEmployee class

  • This class inherits from abstract class Employee.
  • workingHours is the property that indicates the number of working hours per day of an employee.
  • calculateSalary() is the overridden method from abstract class Employee, this method returns salary of part-time employee according to the formula: workingHours * paymentPerHour.

FullTimeEmployee class

  • This class inherits from abstract class Employee.
  • calculateSalary() is the overridden method from abstract class Employee, it returns salary of full-time employee accroding to the formula: 8 * paymentPerHour (full-time employee always works 8 hours a day so FullTimeEmployee class does not include workingHours property)

    A program to test the above classes:

    #include "Employee.cpp"
    #include "PartTimeEmployee.cpp"
    #include "FullTimeEmployee.cpp"
    
    int main() {
    	Employee* employee1 = new PartTimeEmployee("Trung", 45000, 7);
    	cout << "Name: " << employee1->getName() << endl;
    	cout << "Salary per day: " << employee1->calculateSalary() << endl;
    
    	Employee* employee2 = new FullTimeEmployee("Linh", 65000);
    	cout << "Name: " << employee2->getName() << endl;
    	cout << "Salary per day: " << employee2->calculateSalary() << endl;
    	return 0;
    }

    When the above code is compiled and executed, it produces the following result:

    Name: Trung
    Salary per day: 315000
    Name: Linh
    Salary per day: 520000
    

    Instruction

    Employee.cpp file

    #pragma once
    #include <iostream>
    
    using namespace std;
    
    class Employee {
    private:
    	string name;
    	int paymentPerHour;
    
    public:
    	Employee(string name, int paymentPerHour) {
    		this->name = name;
    		this->paymentPerHour = paymentPerHour;
    	}
    
    	string getName() {
    		return name;
    	}
    
    	void setName(string name) {
    		this->name = name;
    	}
    
    	int getPaymentPerHour() {
    		return paymentPerHour;
    	}
    
    	void setPaymentPerHour(int paymentPerHour) {
    		this->paymentPerHour = paymentPerHour;
    	}
    	
    	virtual int calculateSalary() = 0;
    };
    

    PartTimeEmployee.cpp file

    #include "Employee.cpp"
    
    class PartTimeEmployee : public Employee {
    private:
    	int workingHours;
    
    public:
    	PartTimeEmployee(string name, int paymentPerHour, int workingHours) :Employee(name, paymentPerHour) {
    		this->workingHours = workingHours;
    	}
    
    public:
    	int calculateSalary() {
    		return workingHours * getPaymentPerHour();
    	}
    };

    FullTimeEmployee.cpp file

    #include "Employee.cpp"
    
    class FullTimeEmployee : public Employee {
    public:
    	FullTimeEmployee(string name, int paymentPerHour) :Employee(name, paymentPerHour) {
    	}
    
    public:
    	int calculateSalary() {
    		return 8 * getPaymentPerHour();
    	}
    };