Solved

Implementation of overloading, where?

Posted on 2007-11-25
6
205 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now