Understanding constructors to define a class with 3 data members

Mar 9, 2009 at 11:09pm
Hi.

I'm in the middle of writing the code for an assignment that is due this week for my Beginner C++ class. I've been trying all weekend without success to understand it all from the lecture, examples, and textbook. Now I am asking for help from individuals who have conquered this beast before. Any help at all, clarification is mainly what I'm after, would be sincerely appreciated.

The assignment is this. Create a class called Employee. Give it 3 data members: firstName, lastName, and monthlySalary. The class should have a constructor to initializes the 3 data members.
Here's where I encounter my first problem: How should I write my constructor in order to initialize these 3 data members with 2 different types (string and int, respectively)?
The examples given in the text only have one type and so the constructor is obviously going to return the same type. (I don't know if I'm saying that correctly)


class Employee
{
private:
string firstName;
string lastName;
int monthlySalary;
public:
Employee (???)
}


I'm going to just write out this as I progress. I want to stress that I've been working on this code all weekend. I'm not looking for someone to do this for me but provide some much needed clarity as I feel as though I've tapped out all my other resources.

Thank you so much for even just reading this.

Kathleen
Mar 9, 2009 at 11:49pm
Constructors do not return values.

I'll give you an example:

1
2
3
4
5
6
7
class Fruit {
    string type;
    int      calories;
  public:
    Fruit( const string& name, int cals ) :
        type( name ), calories( cals ) {}
};


Looking at the above example, you should be able to write the constructor you need.
Mar 10, 2009 at 12:38am
Thank you so much for your reply.

I wish your example did not look so foreign. I've (so far) not learned "const", "&", and I'm having a hard time understanding the way the information is presented.

Here's what I've gathered so far from your example.

In the class Fruit, we have 2 data members: type (string) and calories (int).
I imagine const is short for constructor.
Fruit is also the constructor.

Fruit( const string& name, int cals ) : type( name), calories( cals)

In my example this line would be written.

Employee( const string&

I'm sorry I just have no idea. I've never seen it written like this before. I'm assuming the word 'name' is just a placeholder that will be used to store what 'type' is define to be later on, by a user perhaps, but really don't know.

While I was waiting (and hoping, and praying...) for a reply, I did more research about constructors and here's more of what I came up with:

class Employee
{
private:
// data members of class Employee
string firstName;
string lastName;
int monthlySalary;
public:
Employee ( string, string, int ); // contstructor that initializes data members to their default
void setEmployeeName( string ); // sets the employee first name
string getEmployeeName(); // retrieves the employee first name
void setMonthlySalary( int ); // requires 1 parameter, an integer, no return
int getMonthlySalary(); // requires no parameter, returns an integer
void determineAnnualSalary(); // determines the annual salary for employee
void give10PercentRaise(); // gives 10% raise to employee
};


Am I on the right path? Can I even write the constructor like that?
Mar 10, 2009 at 12:43am
You are on the right track.

You can simply forget the "const" and the "&"; the code works the same with or without.

Mar 10, 2009 at 1:13am
jsmith,

I can't thank you enough for your assistance.

I just received an email from my professor. I can only assume that he must have received requests for clarification/assistance from other members of my class.

Evidently he did fail to give us enough information to complete the exercise.

Truth be told, I feel like we've been going over the same thing for the last few weeks in this class. The book was going into much more detail than he was during lecture. I kept up with everything and was feeling quite good about completing this assignment, which is why I was so surprised at how difficult this was for me.

Thankfully, now because of your help and some more research, I feel a bit ahead of the class. :)

Have a good one. :)

Kathleen
Mar 10, 2009 at 1:23am
yw. it looks from your class declaration that you are on the right track.
Topic archived. No new replies allowed.