1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include "orderedArrayListType.h"
#include "arrayListType.h"
using namespace std;
#ifndef PERSON
#define PERSON
/*************************************************************
declare a personType class
************************************************************/
class personType
{
friend ostream& operator<<(ostream&, personType&);
friend istream& operator>>(istream&, personType&);
public:
bool operator==(personType&); // == overloading
bool operator<(personType&); // > overloading
bool operator<=(personType&);
bool operator>(personType&); // < overloading
personType & operator=(personType&);// = overloading
//nameType getName(){return name;}
// dateType getDate(){return birthday;}
void setPerson(string, int, double); // set the full person
void getPerson(string&, int&, double&); // get the full person
personType(); // default ructor
personType(string, int, double); // a ructor with parameters
personType(personType&); // copy ructor
~personType(); //destructor
private:
string name;
int ID;
double grade;
};
//overload operator<<, which is not a member of personType, but is a friend to it.
ostream& operator<<(ostream& os, personType& aperson)
{
os << aperson.ID << " " << aperson.name << " " << aperson.grade << endl;
return os;
}
//overload operator>>, which is not a member of personType, but is a friend to it.
istream& operator>>(istream& is, personType& p)
{
//***********************************************************************
if (is == cin) //this is where the error is
//***********************************************************************
{
cout << "Please enter SSN:";
is >> p.ID;
cout << "enter name ";
getline(is, p.name);
cout << "enter grade ";
is >> p.grade;
}
else
{
is >> p.ID;
getline(is, p.name);
is >> p.grade;
}
return is;
}
//overload == operator
bool personType::operator==(personType& aperson)
{
return name == aperson.name
|| ID == aperson.ID;
}
//overload < operator
bool personType::operator<(personType& aperson)
{
return grade < aperson.grade;
}
//overload < operator
bool personType::operator<=(personType& p)
{
return *this <p || *this == p;
}
//overload > operator
bool personType::operator>(personType& aperson)
{
return grade > aperson.grade;
}
//copy ructor
personType & personType::operator=(personType & aperson)
{
if (this != &aperson)
{
name = aperson.name;
ID = aperson.ID;
grade = aperson.grade;
}
return *this;
}
//setperson
void personType::setPerson(string n, int I, double g)
{
name = n;
ID = I;
grade = g;
}
//getperson
void personType::getPerson(string &n, int &I, double &g)
{
n = name;
I = ID;
g = grade;
}
//default ructor
personType::personType() // default ructor
{
name = "cat dog";
ID = -1111;
grade = 0;
}
//another ructor
personType::personType(string n, int I, double g) // a ructor
{
name = n;
ID = I;
grade = g;
}
//copy ructor
personType::personType(personType & aperson)
{
if (this != &aperson)
{
name = aperson.name;
grade = aperson.grade;
ID = aperson.ID;
}
}
// desructor
personType::~personType()
{
cout << " Oh, I am dying ..." << endl;
}
#endif
|