Bài tập C++ OOP: tạo các setter, getter tương ứng cho lớp Employee | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create an Employee class in Employee.cpp file with the following information:

Explanation:

  • id, firstName, lastName, salary attributes describe the ID, first name, last name and salary of the employee object.
  • Employee(id: int, firstName: String, lastName: String, salary: int) is parameterized constructor used to initialize values to attributes.
  • getFullName() method returns the firstName + lastName.
  • The other methods are setter and getter.

A program to test the above Employee class:

#include<iostream>
#include "Employee.cpp"

using namespace std;

int main() {
	Employee e(1, "Kien", "Hoang", 1000);
	cout << "Id: " << e.getId() << endl;
	cout << "Name: " << e.getFullName() << endl;
	cout << "Salary: " << e.getSalary() << endl;
	return 0;
}

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

Id: 1
Name: Kien Hoang
Salary: 1000

Instruction

You need to create an Employee class as below:

#include<iostream>
using namespace std;
class Employee {
private:
	int id;
	string firstName;
	string lastName;
	int salary;

public:
	Employee(int id, string firstName, string lastName, int salary) {
		this->id = id;
		this->firstName = firstName;
		this->lastName = lastName;
		this->salary = salary;
	}

	int getId() {
		return id;
	}

	void setId(int id) {
		this->id = id;
	}

	string getFirstName() {
		return firstName;
	}

	void setFirstName(string firstName) {
		this->firstName = firstName;
	}

	string getLastName() {
		return lastName;
	}

	void setLastName(string lastName) {
		this->lastName = lastName;
	}

	string getFullName() {
		return firstName + " " + lastName;
	}

	int getSalary() {
		return salary;
	}

	void setSalary(int salary) {
		this->salary = salary;
	}
};