Solved

Implementation of overloading, where?

Posted on 2007-11-25
6
216 Views
Last Modified: 2013-12-14
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;
}
0
Comment
Question by:jschmuff
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20347206
>>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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20347211
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20347216
this code portions implements the stream output (<<) operator:

std::ostream &operator<<(std::ostream &os, const person &p)
{
    os << p.itsNameFirst << " " << p.itsNameLast;
      return os;
}

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20347232
try to remove this funcion from your class, then try to compile, you will see a difference that will help you to understand...
0
 

Author Comment

by:jschmuff
ID: 20347296
so in this code I have no implementation of overloading? That what is all that crap in there for then?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 20347311
Well, you are overloading an operator in

std::ostream &operator<<(std::ostream &os, const person &p)
{
    os << p.itsNameFirst << " " << p.itsNameLast;
      return os;
}

not a class member. As already mentioned, take a look at http://en.wikipedia.org/wiki/Operator_overloading and http://en.wikipedia.org/wiki/Method_overloading
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question