- 閱讀權限
- 60
- 最後登錄
- 14-1-28
- 精華
- 0
- UID
- 1008241
- 帖子
- 1426
- 積分
- 4329
- 註冊時間
- 07-12-5
- 在線時間
- 1000 小時
     
- UID
- 1008241
- 帖子
- 1426
- 積分
- 4329
- Good
- 21
- 註冊時間
- 07-12-5
- 在線時間
- 1000 小時
|
//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);
//void input();
//void output();
//void print();
//int employee();
};
//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;
}
應該仲有野錯...山埃多...
CONSTRUCTOR有咩用..
例如你寫個Class BankAccount, 寫完之後, 如果有隻熊貓, 有條中華鱘, 同有個得番一眼既特首, 黎開戶口, 你係int main()度
BankAccount 熊貓;
BankAccount 中華鱘;
BankAccount 得番一眼既特首;
你就已經由個BankAccount模度整左3個一式一樣既object出黎......而家就好似係銀行度依3舊野正準備開戶口, 但未開, 根本講唔上戶口有無存款之類.....
而你個Class BankAccount入面有
BankAccount::set()
{
accountName = "";
accountAmount = 0;
}
咁你係int main()度一行左 熊貓.set(); 咁熊貓就正式開左戶口......我d code極有可能錯, 但係constructor既義意就係如此...由個object空洞無物變做有野果一刻.
void BankAccount::set(string hahaName, int hahaAmount)
{
accountName = hahaName;
accountAmount = hahaAmount;
}
依個係熊貓一開戶口就入錢了......
anyway...3隻野都會死於山埃池之內......你要自己搭橋了......... |
|