Solved

Implementation of overloading, where?

Posted on 2007-11-25
6
232 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 clear a vector as well as how to detect empty vectors in C++.
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.

717 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