Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 22nd, 2006, 8:13 PM   #1
Mack1982
Programmer
 
Join Date: Dec 2004
Posts: 34
Rep Power: 0 Mack1982 is on a distinguished road
Pointer return type,.. ???

I am writing a class employ with the following private data members

fName. // An object of type char array used to hold the Employee's first name 
lName. // An object of type char array used to hold the Employee's last name 
address. // An object of type char array used to hold the Employee's address 
ssn. // An object of type char array used to hold the Employee's social security number 
hours. // An object of type double used to hold the Employee's hours worked 
payRate. // An object of type static  double used to hold the payRate 
empCount/ // an object of type static int that will count the number of Employee objects created

Now the problem is how do i use the following accessors to acesses the private memebers:
1.       char* getFName()   for the fName member

2.      char * getLName()

3.      char* getSsn()

4.      char* getAddress()

5.      double getHours()

6.       void display() :

The regular one i understand ,. the porblem are the ones with the pionter return type,..
The is bascially an abstract class and couple of classes would be dervied from it.
Thank u,..
Mack1982 is offline   Reply With Quote
Old May 22nd, 2006, 8:33 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
char* getFName()
{
  return fName; // assuming fName was a character array/char*
}
Jimbo is offline   Reply With Quote
Old May 22nd, 2006, 9:20 PM   #3
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
char* getFName()
{
     return &fName // or &fName[0] if that makes more sense, same thing
}

//Then, in your main function

{
 . . . .
    char* TheName = getfName();
. . . .
}

TheName then becomes a pointer to the first element of fName, and you can use it as an array as well, by going TheName[i] in all the standard ways.
Twilight is offline   Reply With Quote
Old May 22nd, 2006, 9:40 PM   #4
Mack1982
Programmer
 
Join Date: Dec 2004
Posts: 34
Rep Power: 0 Mack1982 is on a distinguished road
return &fName
means that the address is returned,.... and char* means that the address could only be stored in a pointer since the return type is pointer,..
Mack1982 is offline   Reply With Quote
Old May 22nd, 2006, 9:49 PM   #5
Mack1982
Programmer
 
Join Date: Dec 2004
Posts: 34
Rep Power: 0 Mack1982 is on a distinguished road
is it possible to send a character array via function with out telling the size of the array like this:

//Prototype
   void setfName (char First[])

//Implementation
  void setfName (char First[])
 {
    int size = 10;
   //then use it like First[size]

   strcpy(fName ,First );
}
Mack1982 is offline   Reply With Quote
Old May 22nd, 2006, 9:57 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
You can pass the size as another parameter, and refer to the parameter. Hardcoding it like you have is a bad idea. Have you considered using std::string?
Jimbo is offline   Reply With Quote
Old May 22nd, 2006, 10:22 PM   #7
Mack1982
Programmer
 
Join Date: Dec 2004
Posts: 34
Rep Power: 0 Mack1982 is on a distinguished road
The question specifialy asks for the C Strings,.. I would have perfered string too,.. lol
Mack1982 is offline   Reply With Quote
Old May 22nd, 2006, 10:22 PM   #8
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Why exactly, would you need to have the size of the array for a copy operation? That said:

int size = 0;
int i = 0;

while( CharArray[i] = '\0')
{
    size++;
    i++;
}



And
Quote:
means that the address is returned,.... and char* means that the address could only be stored in a pointer since the return type is pointer,..
What? The Function returns an address. An address is what goes inside a pointer variable, since at heart, a pointer is just a number. The fact that your variable is char* only tells the compiler what kind of variable it will be looking at with that pointer.
Twilight is offline   Reply With Quote
Old May 22nd, 2006, 11:00 PM   #9
Mack1982
Programmer
 
Join Date: Dec 2004
Posts: 34
Rep Power: 0 Mack1982 is on a distinguished road
This is the Entire Question
Begin with creating the Employee class.

In a separate header file called Employee.h, construct a class named Employee. 

This class will have the following data-members:

fName. // An object of type char array used to hold the Employee's first name 
lName. // An object of type char array used to hold the Employee's last name 
address. // An object of type char array used to hold the Employee's address 
ssn. // An object of type char array used to hold the Employee's social security number 
hours. // An object of type double used to hold the Employee's hours worked 
payRate. // An object of type static  double used to hold the payRate 
empCount/ // an object of type static int that will count the number of Employee objects created 
This class will have the following member functions:

A no parameter constructor that will set all char arrays to blanks and all instance numeric variables to 0. 
A four parameter constructor that will accept parameters in this order 
1.      A parameter for the first name

2.      A parameter for the last name

3.      A parameter for the address

4.      A parameter for the social security number 

·   The following Non I/O modifiers:

1.      void setFName(char n[]). This will modify the fName member

2.      void setLName(char n[]). This will modify the lName member

3.      void setAddress(char n[]). This will modify the address member

4.      void setSsn(char n[]). This will modify the ssn member

5.      void setHours(double n). This will modify the hours member

 ·   The following I/O modifier

1.      void read(). This will allow user input into all the instance data-members

 ·  The following accessors:

1.       char* getFName()   for the fName member

2.      char * getLName()

3.      char* getSsn()

4.      char* getAddress()

5.      double getHours()

6.       void display() :  This will display the contents of one Employee object on one line.  In the following example, the number  35 refers to his hours, 12.00 refers to the pay rate and 420 is his salary.

 

 

123-45-6789   Brown      John     31 E.5th St.     35     12.00   420.00 

·   The following implementor:

1.      double calcSalary(). This will calculate and return a salary based on hours and payRate. Hours in excess of 40 are paid at time and a half

 

·  The following static functions:

1.      static void setPayRate(double rate). This will modify the static member

2.      static double getPayRate( ). This will return the payRate

3.      static double getEmpCount( ). This will return the empCount member 


Then using this class as a base class, implement the following classes 

1. HourlyWorker class: This class would be exactly like the base class.
2. SalaryWorker class. This class would possess a new data-member called yearlySalary that would contain the yearly salary of the worker. The worker would be paid on a bi-weekly basis in an amount equal to 1/26th of the yearly salary. All Salaried workers start with a default salary of $36,000.00. 

There must be the additional functions:
    a. double getYearlySalary(); // This would return the yearly salary of the worker
    b. void setYearlySalary(double sal); // This would set the yearly salary of the worker

3. Executive class. This class in addition to having a yearly salary has a bonus in a variable called bonus. The bi-weekly salary will be calculated at 1/26th of the yearly salary + 1/26th of the bonus. All executives start with a default bonus of $10,000.00 and a yearly salary of $50,000.00.

There must be additional functions: 
    a. double getYearlySalary(); // This would return the yearly salary of the worker
    b. void setYearlySalary(double sal); // This would set the yearly salary of the worker
    c. double getBonus(); // This would return the bonus of the worker
    d. void setBonus(double bonl); // This would set the bonus of the worker.


Please note that all functions must be able to be accessed from a base class pointer. This would of course require some changes in the base class. 
Hint: The base class would become an abstract class.

Write a main function that implements a simple menu that would:
• Add an employee 
• List Employees
• Quit

When adding an employee, give the User the choice of which type of Employee to add (HourlyWorker, SalaryWorker or Executive). Create the object dynamically and attach it to a list of Employee pointers.

Question: With the stated Functions and datatypes is it possible to create a list? Or do i have to use somthing else like Structs,....??
Mack1982 is offline   Reply With Quote
Old May 22nd, 2006, 11:52 PM   #10
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Sure.
std::list<Employee*> employees;
I think, but I'm very rusty at C++.

Note: If you're supposed to write your own list class, have fun! (sucks for you!)
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC