/*************************************************/
// R. A. Hillyard
// stuRecord03.cpp
// November 2001
//
// class with constructors
// private member function and friend function
/*************************************************/
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
class StuRecord
{
public:
//notice that the "equal" member function is overloaded and
//both functions accomplish the same thing. The friend function
//is not a member function but has access to private data members
//best to use a friend function when more than one object is involved
friend bool equal(StuRecord n1, StuRecord n2);
bool equal(StuRecord n1);
StuRecord(); //default constructor
StuRecord(int id, string fname, string lname); //3 arg constructor
void input();
void output();
int getidNum();
string getFirst();
string getLast();
private:
int idNum;
string first;
string last;
//private member function to Normalize a string
string normal(string s1);
};
/*************************************************/
//friend function equal - compares two StuRecord to see if
//the first and last name are the same. Note the direct access
//to the member data using the dot notation
/*************************************************/
bool equal (StuRecord n1, StuRecord n2)
{ return (n1.first == n2.first && n1.last == n2.last); }
/*************************************************/
bool StuRecord::equal (StuRecord n1)
{ return (first == n1.getFirst() && last == n1.getLast()); }
/*************************************************/
StuRecord::StuRecord()
{
cout << "In default Constructor\n";
idNum = -6666;
first = "";
last = "";
}
/*************************************************/
StuRecord::StuRecord(int id, string fname, string lname)
{
cout << "In 3 arg Constructor\n";
idNum = id;
first = normal(fname);
last = normal(lname);
}
/*************************************************/
void StuRecord::input()
{
cout << "enter id number, first name and last name: ";
cin >> idNum >> first >> last;
first = normal(first);
last = normal(last);
}
/*************************************************/
void StuRecord::output()
{ cout << idNum << " " + first + " " + last << endl; }
/*************************************************/
int StuRecord::getidNum()
{ return idNum; }
/*************************************************/
string StuRecord::getFirst()
{ return first; }
/*************************************************/
string StuRecord::getLast()
{ return last; }
/*************************************************/
string StuRecord::normal(string s1)
{
string temp = s1;
temp[0] = toupper(temp[0]);
for(int i = 1; i < temp.length(); i++)
temp[i] = tolower(temp[i]);
return temp;
}
/*************************************************/
/*************************************************/
int main()
{
StuRecord name1;
StuRecord name2(1234, "Roger", "craft");
StuRecord name3(3456, "merry", "Christmas");
StuRecord name4;
name1.input();
name4 = StuRecord(9345, "Happy", "Birthday");
//calling equal as a member function
if(name1.equal(name4))
cout << "you guessed the secret word\n";
else
cout << "you did not guess the secret word\n";
//calling equal as a friend function
if(equal(name1,name4))
cout << "Repeat you guessed the secret word\n";
else
cout << "Repeat you did not guess the secret word\n";
name1.output();
name2.output();
name3.output();
name4.output();
return 0;
}//end main
/***************Program Output*******************
//first run
In default Constructor
In 3 arg Constructor
In 3 arg Constructor
In default Constructor
enter id number, first name and last name: 1234 haPPY birthDAy
In 3 arg Constructor
you guessed the secret word
Repeat you guessed the secret word
1234 Happy Birthday
1234 Roger Craft
3456 Merry Christmas
9345 Happy Birthday
//second run
In default Constructor
In 3 arg Constructor
In 3 arg Constructor
In default Constructor
enter id number, first name and last name: 5555 happy trails
In 3 arg Constructor
you did not guess the secret word
Repeat you did not guess the secret word
5555 Happy Trails
1234 Roger Craft
3456 Merry Christmas
9345 Happy Birthday
*/