Link to home
Start Free TrialLog in
Avatar of jschmuff
jschmuffFlag for United States of America

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(string 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("Ann");
      PersonFull.setNameFirst("Heidi");

      cout << "The persons first and last name is: ";
      cout << PersonFull << endl;

      return 0;
}
Avatar of jkr
jkr
Flag of Germany image

>>Which part is exactly the implementation of overloading in this source?

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.
this code portions implements the stream output (<<) operator:

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...
Avatar of jschmuff

ASKER

so in this code I have no implementation of overloading? That what is all that crap in there for then?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial