Re: constructors
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 09/01/04
- Previous message: Theo Lagendijk: "Re: constructors"
- In reply to: iwasinnihon: "constructors"
- Next in thread: David White: "Re: constructors"
- Reply: David White: "Re: constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 31 Aug 2004 22:05:11 GMT
"iwasinnihon" <iwasinnihon@hotmail.com> wrote in message
news:4134e663$0$75878$3a2ecee9@news.csolutions.net...
> I am a little new to c++ programming and I don't really understand
> constructors. I am writing a program for school and would just like some
> help. I have to write a constructor and then pass information in to
create
> a object dynamically. Here is the code that I have so far. What am
> missing? I'd appreciate any pointers you can give me.
>
> ------------------
> class
> -------------
> class Employee
> {
> private:
> int empNumber;
> char address[MAX];
> char phoneNumber[MAX];
> int deptNumber;
> char name[MAX];
> int weeklySalary;
> float commission;
> int revenue;
> public:
>
> //constructor
> Employee(int, char*, char*, int, char*, int, float);
>
> char* getEmp();
>
> int setValue(int);
>
> int calcPay();
> };
> -------------------
> Here is my implementation
> --------------------
> Employee::Employee(int empNumberIn,
> char* addressIn[],
> char* phoneNumberIn[],
> int deptNumberIn,
> char* nameIn[],
> int weeklySalaryIn,
> float commisionIn):
>
> empNumber = empNumberIn;
> strcpy(address, addressIn);
> strcpy(phoneNumber, phoneNumberIn);
> deptNumber = deptNumberIn;
> strcpy(name, nameIn);
> weeklySalary = weeklySalaryIn;
> commission = commissionIn;
> (}
> --------------------
> Here is my code for creating the object
> ---------------------
> Employee* empPtr = new Employee(
> 1,
> "12 Madison Ave",
> "123-4567",
> 100,
> "I. M. Great",
> 1000,
> 0.4);
#include <string>
using std::string;
class Employee
{
int empNumber;
string address;
string phoneNumber;
int deptNumber;
string name;
int weeklySalary;
double commission;
int revenue;
public:
Employee(int,
const string&,
const string&,
int,
const string&,
int,
double);
const string& getEmp() const;
int setValue(int);
int calcPay() const;
};
Employee::Employee(int empNumberIn,
const string& addressIn,
const string& phoneNumberIn,
int deptNumberIn,
const string& nameIn,
int weeklySalaryIn,
double commissionIn) :
empNumber(empNumberIn),
address(addressIn),
phoneNumber(phoneNumberIn),
deptNumber(deptNumberIn),
name(nameIn),
weeklySalary(weeklySalaryIn),
commission(commissionIn),
revenue(0)
{
}
int main()
{
Employee emp(1,
"12 Madison Ave",
"123-4567",
100,
"I. M. Great",
1000,
0.4);
return 0;
}
-Mike
- Previous message: Theo Lagendijk: "Re: constructors"
- In reply to: iwasinnihon: "constructors"
- Next in thread: David White: "Re: constructors"
- Reply: David White: "Re: constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|