Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

overloaded + operator

Posted on 2003-03-24
4
Medium Priority
?
217 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
  • 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

564 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