2000FUN論壇

 

 

搜索
2000FUN論壇 綜合論壇 學生討論區 關於C++ class同constructor問題
返回列表 發新帖 回覆
查看: 2758|回覆: 4
go

[其他] 關於C++ class同constructor問題 [複製鏈接]

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

UID
23710 
帖子
768 
積分
1623 
Good
0  
註冊時間
02-11-29 
在線時間
383 小時 
1#
發表於 09-4-2 03:58 PM |只看該作者 |倒序瀏覽 |打印
我呢家學緊C++...但係上一堂我病左...
所以我無聽到書...所以有一份功課唔識做
唔知有無人可以教一教我呢份功課點做同埋我都有C++既問題想問埋
份功課既題目係咁

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呢個功能呢?
唔該晒

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

UID
23710 
帖子
768 
積分
1623 
Good
0  
註冊時間
02-11-29 
在線時間
383 小時 
2#
發表於 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));
}
}

int main(int argc, char** argv)
{
int i;
Employee ;
for(i=0; i<3;, ++i)
{
Employee.input();
employee.set(_name,_salary);
cout<<"Employee ["<<i<<"]"<<Employee.get_name()<<"Salary :"<<Employee.get_salary()<<endl;
}

Rank: 10

UID
181055 
帖子
9423 
積分
4448 
Good
241  
註冊時間
04-4-23 
在線時間
5369 小時 

十週年勳章(賀詞)

3#
發表於 10-2-26 11:35 PM |只看該作者
public 係其他class 都可以access到個data
private 係唔得~

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

UID
1008241 
帖子
1426 
積分
4329 
Good
21  
註冊時間
07-12-5 
在線時間
1000 小時 

十週年勳章(賀詞)

4#
發表於 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);

                //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隻野都會死於山埃池之內......你要自己搭橋了.........

UID
1651619 
帖子
15 
積分
-23 
Good
0  
註冊時間
10-3-3 
在線時間
0 小時 
5#
發表於 10-3-5 02:13 AM |只看該作者

太陽城開戶 太陽城代理開戶

太陽城開戶 太陽城代理開戶
太陽城總代理,太陽城網,亞洲官方網是菲律賓太陽城指定亞洲代理,太陽城娛樂開戶,太陽城亞洲官方網提供太陽城代理,太陽城娛樂代理,太陽城開戶,太陽城游戲投注,百傢樂開戶代理,龍虎,斗地主,真人網上娛樂,百傢樂開戶,百傢樂代理,太陽城開戶,太陽城代理,百傢樂真人游戲,百傢樂游戲,太陽城真人游戲,太陽城游戲,百傢樂網上娛樂.TEL:13400656855
‹ 上一主題|下一主題
你需要登錄後才可以回帖 登錄 | 免費註冊

聯絡我們|Archiver| 2000FUN論壇

SERVER: 2 GMT+8, 26-4-18 03:00 PM , Processed in 0.030892 second(s), 11 queries , Gzip On.

Sponsor:工作間 , 網頁寄存

Powered by Discuz! X1.5.1

© 2001-2010 Comsenz Inc.