Section 5 - The Employee Class
6A.5.1 The Program Listing
Here's a program that defines the Employee class, then creates some Employee objects in the main() method. I have declared and used some int data along the way so you could compare the operation of declaring and using class variables with that of declaring and using int variables.
#include <iostream>
#include <string>
using namespace std;
// ------- class prototypes -------
class Employee
{
public:
// member data for the class
long socSec;
double wage;
short age;
};
int main()
{
Employee walter, birkoff;
int a, b;
// initialize walter
walter.socSec = 123456789;
walter.wage = 12.95;
walter.age = 61;
// initialize a;
a = 89;
// assign to birkoff
birkoff = walter;
// assign to b
b = a;
// show both a and b:
cout << "\n\na: " << a << "\nb: " << b << endl;
// show both walter and birkoff:
cout << "walter: ss#->" << walter.socSec
<< " wage->" << walter.wage
<< " age->" << walter.age << endl;
cout << "birkoff: ss#->" << birkoff.socSec
<< " wage->" << birkoff.wage
<< " age->" << birkoff.age ;
// modify a and walter
a++;
walter.age = (short)(walter.age - 40);
walter.socSec ++ ;
walter.wage += 8.50;
walter.age++;
// show both a and b after changing a:
cout << "\n\na: " << a << "\nb: " << b << endl;
// show both walter and birkoff after changing walter:
cout << "walter: ss#->" << walter.socSec
<< " wage->" << walter.wage
<< " age->" << walter.age << endl;
cout << "birkoff: ss#->" << birkoff.socSec
<< " wage->" << birkoff.wage
<< " age->" << birkoff.age << endl;
}
6B.4.2 The Meaning of the Assignment Operation with Objects
As before with the Pets, we see that we can set one Employee object equal to another,
birkoff = walter;
This is analogous to assigning one int to another, except that instead of the objects being ints, they are Employees -- our home grown data type. Furthermore, all of the data, both private and public, of the object walter is transferred to the object birkoff as a result of this assignment.
This is in contrast to languages such as Java or C#, in which the names birkoff and walter are treated as references. If you come from those languages, understand that things are different in C++. If you are a beginner to programming, you can ignore this observation.
