Write the class definition for a class named Employee. The class should include data members for an employee object's name and salary (the salary will be an integer). The class should contain two member functions: the constructor and a function that allows a program to assign values to the data members. Add two member functions to the employee class. One member function should allow any program using an employee object to view the contents of the salary data member. The other member function should allow the program to view the contents of the employee name data member. (Hint: have the member functions simply return the contents of the appropriate data member).
Add another member function to the class. This function should calculate an employee object's new salary, based on a raise percentage, provided by the program (main function). Before calculating the raise, the member function should verify that the raise percentage is greater or equal to zero. If the raise percentage is less then zero, the member function should display an error message.
Write main function that will create an array of employee objects, assign values to the objects, display the names and current salaries for all objects, ask user for the raise percentage and then calculate and display new salaries for all objects.
仲有我想順便問埋...
幾時先應該用PUBLIC 幾時應該用PRIVATE去定義個DATA MEMBER
同埋CONSTRUCTOR有咩用??應該點去用呢?
如果可以既話...可唔可以講埋解釋下return呢個功能呢?
唔該晒作者: chaaa821 時間: 09-4-7 06:55 AM
我呢家跟住2樓之前留比我D CODE....用大約既方法寫左出黎....
但係完全唔得....份功課想話係可以輸入個名同個SALARY
之後再出番黎用個個RAISE PERCENTAGE再出多次個名同個新SALARY
個CODE係下面咁...應該點改番好??
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
string _name;
int _salary;
public:
void input();
void output();
void print();
int employee();
void set(string name, int salary);
int get_salary();
string get_name();
int computerNewSalary(int raisepercentage) const;
};
void Employee::set(string name, int salary)
{
_name=name;
_salary=salary;
}
void Employee::input()
{
cout<<"Enter name and salary :"<<endl;
cin>>_name>>_salary;
}
void Employee:utput()
{
cout<<"Name"<<_name<<"Salary "<<_salary<<endl;
}
int Employee::get_salary()
{
return _salary;
}
string Employee::get_name()
{
return _name;
}
int Employee::computerNewSalary(int raisepercentage) const;
{
if (raisepercentage<0)
{
cout<<"Invalid raise percentage."<<endl;
return _salary;
}
else
{
return _salary*(1+(raisepercentage/100));
}
}
public 係其他class 都可以access到個data
private 係唔得~作者: winnieyimyim 時間: 10-2-27 06:21 AM
//Write the class definition for a class named Employee.
#include<iostream>
#include<string>
using namespace std;
class Employee
{
//The class should include data members for an employee object's name and salary
//(the salary will be an integer).
private:
string dummyEmployeeName;
int dummyEmployeeSalary;
public:
//The class should contain two member functions:
//the constructor
Employee set();//好似係咁
//and
//a function that allows a program to assign values to the data members.
void set(string name, int salary);
//雖然都係叫set, 但係好似如果一有parameter(s)就已經唔係constructor, 所以要void
int get_salary();
string get_name();
float calculateNewSalary(float raisepercentage);
//The class should contain two member functions:
//the constructor
//void Employee::set()//其實我唔係好sure constructor連void都好似唔洗...
//試下啦就
Employee::set()
{
dummyEmployeeName = "";
dummyEmployeeSalary = 0;
}
//and
//a function that allows a program to assign values to the data members.
//雖然都係叫set, 但係好似如果一有parameter(s)就已經唔係constructor, 所以要void
void Employee::set(string hahaName, int hahaSalary)
{
dummyEmployeeName = hahaName;
dummyEmployeeSalary = hahaSalary;
}
/*
void Employee:utput()
{
cout<<"Name"<<_name<<"Salary "<<_salary<<endl;
}
*/
//Add two member functions to the employee class.
//One member function should allow any program using an employee object to view the contents of the salary data member.
//The other member function should allow the program to view the contents of the employee name data member.
//(Hint: have the member functions simply return the contents of the appropriate data member).
int Employee::get_salary()
{
return dummyEmployeeSalary;
}
string Employee::get_name()
{
return dummyEmployeeName;
}
//Add another member function to the class.
//This function should calculate an employee object's new salary,
//based on a raise percentage, provided by the program (main function).
//Before calculating the raise, the member function should verify that the raise percentage is greater or equal to zero.
//If the raise percentage is less then zero, the member function should display an error message.
float Employee::calculateNewSalary(float hahaRaisePercentage) const;
{
if (hahaRaisePercentage < 0)
{
cout<<"Invalid raise percentage."<<endl;
return dummyEmployeeSalary;
}
else
{
return dummyEmployeeSalary * (1+(hahaRaisePercentage/100));
}
}
//Write main function that will create an array of employee objects,
//assign values to the objects,
//display the names and current salaries for all objects,
//ask user for the raise percentage and then calculate and display new salaries for all objects.
int main(int argc, char** argv)
{
Employee dummyEmployee;
string nameInput = "";
int salaryInput = 0;
const float raisePercentageInput = 0;
//int i;
//for(i=0; i<3;, ++i)
//我覺得應該要用array list,
//因為如果我無記錯既話array list係dynamic,你放幾多野落去都得,
//唔洗好似array咁set死放幾多野落去...
//但係我已經唔記得左array list點用...sorry
//但總之就係整左個array list之後, 係個array list度每去一格, 就放個dummyEmployee落去
{
dummyEmployee.set();
cout<<"Enter name and salary: "<<endl;
cin>>nameInput>>salaryInput;
dummyEmployee.set(nameInput,salaryInput);
//cout<<"Employee ["<<i<<"]"<<Employee.get_name()<<"Salary :"<<Employee.get_salary()<<endl;
}
//出番個array list每格個dummyEmployee料出黎...dummyEmployee.get_name...dummyEmployee.get_salary....
cout<<"Enter raise percentage: "<<endl;
cin>>raisePercentageInput;
//再行多條array list一次, 張個raise percentage放落每格個dummyEmployee佢就會出每個既new salary
cout<<dummyEmployee.calculateNewSalary(raisePercentageInput)<<endl;
}