Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Implementation of overloading, where?

Posted on 2007-11-25
6
220 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

807 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