?
Solved

overloaded + operator

Posted on 2003-03-24
4
Medium Priority
?
215 Views
Last Modified: 2010-04-01
The below code uses a VectorList class in a main function that reads three sets of quiz scores into an array of VectorList objects. Then for each set of data, prints the scores in the original order of entry, prints the scores in descending order.  

Does anybody know what an overloaded operator is and how I can use it to create another VectorList object that combines dataset1 and dataset2?


The output should look like this:

Enter quiz scores, -1 to teminate: 10 9 8 7 8 9 10 -1
Enter quiz scores, -1 to teminate: 5 9 6 9 7 -1
Enter quiz scores, -1 to teminate: 10 8 7 10 9 8 10 7 10 -1

DATASET 1
Original Data:    10    9    8    7    8    9   10
  Sorted Data:    10   10    9    9    8    8    7
Average Excluding Lowest Score: 9.00

DATASET 2
Original Data:     5    9    6    9    7
  Sorted Data:     9    9    7    6    5
Average Excluding Lowest Score: 7.75

DATASET 3
Original Data:    10    8    7   10    9    8   10    7   10
  Sorted Data:    10   10   10   10    9    8    8    7    7
Average Excluding Lowest Score: 9.00

DATASET 1 + DATASET 2
Original Data:    10   10    9    9    8    8    7    9    9    7    6    5
  Sorted Data:    10   10    9    9    9    9    8    8    7    7    6    5
Average Excluding Lowest Score: 8.36

Press any key to continue



#include <iostream.h>

#include <iomanip>

#include <string.h>

//#include "VectorList.h"

#include <vector>

using namespace std;



class VectorList



{

public:

VectorList();

void ReadList();

void PrintList();

void SortList();

double AverageMinusLowest();

VectorList Add (const VectorList &b) const;



private:

vector<int> z;



};



VectorList operator+ (const VectorList &a, const VectorList &b);



VectorList::VectorList()

{



z.clear();

}

void VectorList::ReadList()

{



int i;

cout<< "Enter quiz scores, -1 to terminate:";

cin >>i ;



while (i != -1)

{

z.push_back(i);

cin >>i ;

}



}





void VectorList::PrintList()

{

cout<<"\nOriginal Data:";


int x;

for (x = 0; x < z.size(); x++)

{



cout <<z[x]<<" ";



}


}


void VectorList::SortList()



{

int y;
int y1;
int temp;
cout<<"\n Sort List  :";


for (y=0;y < z.size();y++)
{
     for (y1=y; y1< z.size();y1++)
     {
          if(z.at(y) < z.at(y1))
          {
    temp = z.at(y);
     z.at(y)=z.at(y1);
     z.at(y1)=temp;
          }
     }
}

for (int x=0;x < z.size();x++)
{
     cout<<z.at(x)<<" ";
}
}




int main()

{



VectorList Dataset1;
VectorList Dataset2;
VectorList Dataset3;


Dataset1.ReadList();
Dataset2.ReadList();
Dataset3.ReadList();


Dataset1.PrintList();

Dataset1.SortList();

Dataset2.PrintList();
Dataset2.SortList();





Dataset3.PrintList();
Dataset3.SortList();




return 0;

}

0
Comment
Question by:ktran40
[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
  • 2
  • 2
4 Comments
 

Expert Comment

by:unknownmat
ID: 8199597
Unless I'm horribly mistaken, this is a relatively easy problem...

Your class already contains a VectorListAdd() routine that accepts a new vector list.

Your overloaded plus operator would then simply be:

VectorList operator+( const VectorList& v1, const VectorList& v2 )
{
  VectorList ReturnVec; //New object created
  ReturnVec.VectorListAdd( v1 );
  ReturnVec.VectorListAdd( v2 );
  ReturnVec.SortList();
  return ReturnVec;
}

... ?Is this mistaken?

Oh well, hope that helps.

Matt
0
 

Author Comment

by:ktran40
ID: 8202878
You are probably correct.  Except for some reason it is not recognizing "VectorListAdd".  I added what you suggested, it errored out on me.  Do you know what I'm doing wrong?
0
 

Accepted Solution

by:
unknownmat earned 300 total points
ID: 8205109
Does your code look exactly as posted?

If so, you have not written your VectorListAdd routine yet.

Matt
0
 

Author Comment

by:ktran40
ID: 8228042
Thank you, I finally got.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

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