Classes using const and & ,newbie help..

I am not sure what is the purpose of adding const and & on the constructor's parameter list.Also, for the two private date members Date, why are they in const?

In simple program, how I understand it, const is used so the value cannot be modified while the & is used as as pass by reference. I could not see how these work or benefit in this program.


I just put some parts of the code that I think can help.

header file
1
2
3
4
5
6
7
8
9
public:
 Employee( const string &, const string &,
 const Date &, const Date & );
 void print() const;
private:
string firstName; // composition: member object
 string lastName; // composition: member object
const Date birthDate; // composition: member object
const Date hireDate; // composition: member object 


Implementation
1
2
3
4
5
6
7
8
9
10
11
Employee::Employee( const string &first, const string &last,
 const Date &dateOfBirth, const Date &dateOfHire )
: firstName( first ), // initialize firstName
lastName( last ), // initialize lastName
birthDate( dateOfBirth ), // initialize birthDate
hireDate( dateOfHire ) // initialize hireDate
 {
 // output Employee object to show when constructor is called
 cout << "Employee object constructor: "
 << firstName << ' ' << lastName << endl;
} // end Employee constructor 


Driver

1
2
3
4
5
6
7
8
9
10
#include "Employee.h"
int main()
 {
 Date birth( 7, 24, 1949 );
 Date hire( 3, 12, 1988 );
 Employee manager( "Bob", "Blue", birth, hire );

 cout << endl;
 manager.print();
 } // end main 


output:
Date object constructor for date 7/24/1949
Date object constructor for date 3/12/1988
Employee object constructor: Bob Blue
Blue, Bob Hired: 3/12/1988 Birthday: 7/24/1949
Last edited on
For your program it does not matter. But latter it might prove useful. Suppose you have a large array (say 1GB) and you want to use it somehow in your constructor (say find the sum of the elements). If you don't use the address, you have the array in your main function, and then you make a copy for your constructor. At some point, you might run out of memory. Same thing with const. You don't need it here, but you might need it for a future rogram
Hi,

Having the parameters marked const is a good idea - it doesn't make sense for them to be able to be modified inside the function.

The Birth and Hire dates are const because we don't want to change them once initialised.

The const after the print function means that no member variables can be altered.

const is a very useful feature in C++ - am not sure that there are many non C++ like languages that have this feature. It provides an extra level of protection, above the normal access specifier of private: for the data . One shouldn't have public: or protected: member variables - I guess you already knew that.

Pass by reference is probably not necessary for POD (Plain Old Data - int double char etc) but it is a good idea for more complex types as a reference is a similar idea to a pointer. Of course you can pass by reference if you want that variable to be altered in the scope the function is called from.

:+)
Suppose you have a large array (say 1GB) and you want to use it somehow in your constructor (say find the sum of the elements). If you don't use the address, you have the array in your main function, and then you make a copy for your constructor.
I am assuming you mean an object with a very large array inside of it, otherwise it would be very hard to pass the array by a copy.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct LargeClass
{
    char Byte;
    char KB[1024];
    char MB[1024*1024];
    char GB[1024*1024*1024];
}; 

void someFunction(LargeClass lc) //problems could happen here

void someFunction(LargeClass &lc)
//or
void someFunction(LargeClass const &lc)//would be better 

Last edited on
Topic archived. No new replies allowed.