jschmuff
asked on
Implementation of overloading, where?
Ok I have my code for implementing overloading in this program. Which part is exactly the implementation of overloading in this source? I am still very confused on overloading of the output operator.
Here is my code for persontype.h:
#include <string>
class person
{
public:
std::string getNameLast();
std::string getNameFirst();
void setNameLast(std::string nameLast);
void setNameFirst(std::string nameFirst);
void introduceFirst();
void introduceLast();
friend std::ostream &operator<<(std::ostream &os, const person &p);
private:
std::string itsNameLast;
std::string itsNameFirst;
};
Here is my code for persontype.cpp:
#include <iostream>
#include <string>
#include "persontype.h"
using namespace std;
string person::getNameLast()
{
return itsNameLast;
}
void person::setNameLast(string nameLast)
{
itsNameLast = nameLast;
}
void person::introduceLast()
{
cout << itsNameLast;
}
string person::getNameFirst()
{
return itsNameFirst;
}
void person::setNameFirst(strin g nameFirst)
{
itsNameFirst = nameFirst;
}
void person::introduceFirst()
{
cout << itsNameFirst;
}
std::ostream &operator<<(std::ostream &os, const person &p)
{
os << p.itsNameFirst << " " << p.itsNameLast;
return os;
}
Here is my main.cpp code:
#include <iostream>
#include <string>
#include "persontype.h"
using namespace std;
int main()
{
person PersonFull;
PersonFull.setNameLast("An n");
PersonFull.setNameFirst("H eidi");
cout << "The persons first and last name is: ";
cout << PersonFull << endl;
return 0;
}
Here is my code for persontype.h:
#include <string>
class person
{
public:
std::string getNameLast();
std::string getNameFirst();
void setNameLast(std::string nameLast);
void setNameFirst(std::string nameFirst);
void introduceFirst();
void introduceLast();
friend std::ostream &operator<<(std::ostream &os, const person &p);
private:
std::string itsNameLast;
std::string itsNameFirst;
};
Here is my code for persontype.cpp:
#include <iostream>
#include <string>
#include "persontype.h"
using namespace std;
string person::getNameLast()
{
return itsNameLast;
}
void person::setNameLast(string
{
itsNameLast = nameLast;
}
void person::introduceLast()
{
cout << itsNameLast;
}
string person::getNameFirst()
{
return itsNameFirst;
}
void person::setNameFirst(strin
{
itsNameFirst = nameFirst;
}
void person::introduceFirst()
{
cout << itsNameFirst;
}
std::ostream &operator<<(std::ostream &os, const person &p)
{
os << p.itsNameFirst << " " << p.itsNameLast;
return os;
}
Here is my main.cpp code:
#include <iostream>
#include <string>
#include "persontype.h"
using namespace std;
int main()
{
person PersonFull;
PersonFull.setNameLast("An
PersonFull.setNameFirst("H
cout << "The persons first and last name is: ";
cout << PersonFull << endl;
return 0;
}
this code portions implements the stream output (<<) operator:
std::ostream &operator<<(std::ostream &os, const person &p)
{
os << p.itsNameFirst << " " << p.itsNameLast;
return os;
}
std::ostream &operator<<(std::ostream &os, const person &p)
{
os << p.itsNameFirst << " " << p.itsNameLast;
return os;
}
try to remove this funcion from your class, then try to compile, you will see a difference that will help you to understand...
ASKER
so in this code I have no implementation of overloading? That what is all that crap in there for then?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is none, that is: not in your class.
std::ostream &operator<<(std::ostream &os, const person &p)
{
os << p.itsNameFirst << " " << p.itsNameLast;
return os;
}
overloads the global 'operator<<()' to provide an implementation that can be used for 'person', that's the overloading part.