1

I'm using C++ How to Program 8th Edition and one part of the code says,

Employee::Employee( const string &first, const string &last, const Date &dateOfBirth, const     DateOfBirth, const Date &dateOfHire)
  : firstName(first), lastName(last), birthDate(dateOfBirth), hireDate(dateOfHire)
  {
  cout <<"Employee object constructor: " << firstName << ' '<< lastName <<endl;
  }

I'm confused. Is this Employee class initializing the 2 Date class objects by copy or by reference? The book says its by a copy constructor, but I see an &ampersand before the name of each. Also, if you declare const before a function parameter name, that parameter is unchanged but the original argument passed in may not necessarily be constant right?

Also, you can use constant functions for non constant classes right? The point of declaring a function constant is that that function may not edit any variables declared in the class its in?

Here is the implementation in main()

int main() 
{
  Date birth(7, 24, 1949);
  Date hire (3, 12, 1988);
  Employee manager( "Bob", "Blue", birth, hire);
}

birth and hire are objects of class Date

Edit 2: Here is a screenshot of the function defined in the header file. Thanks again. http://tinypic.com/r/qznf6g/8 http://tinypic.com/r/2qdr3tz/8 Thankyou very much!!!! Edit 3: I think I figured it out. So it is using a reference initially to initialize the Date object but then it also uses a copy constructor to assign dateOfBirth to to birthDate in Line 14 right?

Ajay
  • 18,086
  • 12
  • 59
  • 105
Kevin Cheng
  • 39
  • 1
  • 6

4 Answers4

1
  1. By copy. (Edit:can't determinate without looking at "Date" constructor) There are two interfaces involved, "Employee" takes parameter by reference, but actual work do constructor of type "Date", it's interface does matter.

    • Also, if you declare const before a function parameter name, that parameter is unchanged but the original argument passed in may not necessarily be constant right?

Yes.

  • Also, you can use constant functions for non constant classes right? The point of declaring a function constant is that that function may not edit any variables declared in the class its in?

Yes.

Alexey Birukov
  • 1,565
  • 15
  • 22
  • 2
    Unless you have more information about the `Employee` class than is shown in the question, there is not enough information for you to definitively determine the answer to the first question. If `birthDate` and `hireDate` are references, then your answer is wrong. – Benjamin Lindley Oct 17 '14 at 07:12
  • so I'm Right? firsName(first), lastName(last), birthDate(dateOfBirth), hireDate(dateOfHire) <- is using the copy constructor but the dateOfBirth is just a reference to the a Date object created in the calling function, correct? – Kevin Cheng Oct 17 '14 at 07:43
1

Consider a function:

void foo(const Date& dt)
{
   Date local = dt;
   // Use 'local'
   // Modify 'local'
   // But cannot modify the original/source 'dt'
}

int main()
{
   Date abc;
   foo(abc);
}

The first line in foo is performing copy, there is no reference assignment, despite & present in function argument. Even if you declare abc as const in main, copy will be performed in foo

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • you can use const in the parameter even thought the original object isn't a constant right? Also, can you please confirm my edit3? thanks – Kevin Cheng Oct 17 '14 at 07:36
  • Yes. A non-const object can be assigned to, or referenced by a const-reference. But reverse is not possible. – Ajay Oct 17 '14 at 07:37
  • int &hello() { int a = 5; return a; } does the calling function receive a reference to a? Should I do int&b = hello() in the calling function? Thanks. Sorry for the questions – Kevin Cheng Oct 17 '14 at 07:38
  • This code will give warning, and would not work. Kindly understand these concepts clearly yourself, instead of asking everything here. If stuck, ask a new question. – Ajay Oct 17 '14 at 08:00
  • Aside of everything mentioned, there is a typo: firsName is not firstName. – Vincent Oct 17 '14 at 08:10
0

It is a copy constructor (the parameter is an object of the same type).

You should read with more attention the parameters part! Passing a parameter by address (reference) is faster than passing it by value. Using const & is not allowing to change the value of the parameter outside the function.

sop
  • 3,445
  • 8
  • 41
  • 84
0

Let's take this a bit at a time. Declaring the date arguments as:

const Date& dateOfBirth

means that the date is passed by reference (not copied) and that the referenced object can not be modified by the function. What happens next depends on how the Date members of the Employee class are declared.

They would be one of:

Date birthDate;        //the parameter is passed to the copy constructor of Date
const Date birthDate;  //as above, and the birthDate object cannot then be changed
const Date& birthDate; //birthDate is set to a reference to the object passed in, which then cannot be changed using this reference
Date& birthDate;       //you would get a compiler error in the Employee constructor.

In the event that the Dates are declared as in the first line - on the stack - the Date copy constructor (a constructor taking a reference, and preferably a const reference, to the same type) must be taking the argument as a const Date$ and one suspects it internally sets its attributes to those of the object passed in.

If Date's copy constructor was taking a non-const reference then again there would be a compiler error at the Employee constructor as you would be trying to pass a const to a non-const parameter.

(If it took its argument by value that would also be an error, as the copy constructor would need to be invoked to invoke the copy constructor, and so on forever!)

You are correct that a const method can be called on a non-const object, or non-const reference or pointer to an object.

Finally, I have used the same style as you in declaring the references and so on here, but you ought to read up on "right to left" order for declaring these types as it's the most consistent and preferred method. For example, we prefer

Date const$ dateOfBirth

to

const Date& dateOfBirth

even though they are effectively the same.

Grimm The Opiner
  • 1,778
  • 11
  • 29