Mega Code Archive

 
Categories / C++ / Data Structure
 

Storing Class Objects with overloaded operators in a Vector

#include <iostream> #include <vector> #include <cstdlib> using namespace std; class IntValueClass {   int temp; public:   IntValueClass() {       temp = 0;    }   IntValueClass(int x) {       temp = x;    }   IntValueClass &operator=(int x) {     temp = x; return *this;   }   double get_temp() {      return temp;    } }; bool operator<(IntValueClass a, IntValueClass b) {   return a.get_temp() < b.get_temp(); } bool operator==(IntValueClass a, IntValueClass b) {   return a.get_temp() == b.get_temp(); } int main() {   vector<IntValueClass> v;   unsigned int i;   for(i=0; i<7; i++)     v.push_back(IntValueClass(60 + rand()%30));   cout << "Fahrenheit temperatures:\n";   for(i=0; i<v.size(); i++)     cout << v[i].get_temp() << " ";   cout << endl;   for(i=0; i<v.size(); i++)     v[i] = (int)(v[i].get_temp()-32) * 5/9 ;   cout << "Centigrade temperatures:\n";   for(i=0; i<v.size(); i++)     cout << v[i].get_temp() << " ";   return 0; }