Advertisement
Guest User

Untitled

a guest
Jan 30th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. //#ifndef __PROGTEST__
  2. //#include "key_value.cpp"
  3. //#endif /* __PROGTEST__ */
  4.  
  5. class CValue{
  6. public:    
  7.     CValue(int x){
  8.         value = x;
  9.     }  
  10.     CValue(){
  11.        
  12.     }
  13.     CValue(const CValue &i){
  14.         value = i.value;
  15.     }
  16.     ~CValue(){
  17.         // value = 0;
  18.     }
  19.     CValue& operator = ( const CValue &i ){
  20.         if(this == &i){
  21.             return *this;
  22.         }
  23.         value= i.value;
  24.         return *this;
  25.     }          
  26.     bool operator == ( const CValue &i ) const{
  27.         if(value == i.value){
  28.             return true;
  29.         }
  30.         return false;
  31.     }
  32.     bool operator != ( const CValue &i ) const{
  33.         if(*this == i){
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     bool operator < ( const CValue &i ) const{
  39.         if(value < i.value){
  40.             return true;
  41.         }
  42.         return false;
  43.     }
  44.     bool operator > ( const CValue &i ) const{
  45.         if(value > i.value){
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.     bool operator >= ( const CValue &i ) const{
  51.         if(value >= i.value){
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56.     bool operator <= ( const CValue &i ) const{
  57.         if(value <= i.value){
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.     int value;
  63. };
  64.  
  65. class CKey{
  66. public:
  67.     CKey(int x){
  68.         value= x;
  69.     }
  70.     CKey(){
  71.        
  72.     }
  73.     CKey(const CKey &i){
  74.         value = i.value;
  75.     }
  76.     ~CKey(){
  77.        
  78.     }
  79.     CKey& operator = ( const CKey &i ){
  80.         if(this == &i){
  81.             return *this;
  82.         }
  83.         value= i.value;
  84.         return *this;
  85.     }                  
  86.     bool operator == ( const CKey &i ) const{
  87.         if(value == i.value){
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.     bool operator != ( const CKey &i ) const{
  93.         if(value == i.value){
  94.             return false;
  95.         }
  96.         return true;
  97.     }
  98.     bool operator < ( const CKey &i ) const{
  99.         if(value < i.value){
  100.             return true;
  101.         }
  102.         return false;
  103.     }
  104.     bool operator > ( const CKey &i ) const{
  105.         if(value > i.value){
  106.             return true;
  107.         }
  108.         return false;
  109.     }
  110.     bool operator >= ( const CKey &i ) const{
  111.         if(value >= i.value){
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116.     bool operator <= ( const CKey &i ) const{
  117.         if(value <= i.value){
  118.             return true;
  119.         }
  120.         return false;
  121.     }
  122.     int value;
  123. };
  124.  
  125. #include <iostream>
  126. #include <exception>
  127. using namespace std;
  128.  
  129. class CEntry {
  130. public:
  131.     CKey key;
  132.     CValue val;
  133.    
  134.     CEntry(const CKey& key, const CValue& val) {
  135.         this->key = key;
  136.         this->val = val;
  137.     }
  138.    
  139. //    CEntry* operator=( CEntry *orig) {
  140. //        *this = orig;
  141. //        return this;
  142. //    };
  143. };
  144.  
  145. class ArrayOutOfBounds: public exception {
  146.     virtual const char* what() const throw()
  147.     {
  148.         return "Array out of bounds!";
  149.     }
  150. };
  151.  
  152. class EntriesArray {    
  153. public:
  154.     CEntry **entries;
  155.     int length;
  156.    
  157.     EntriesArray( int length ) : length(length), entries(new CEntry*[length]()) {
  158.     }
  159.    
  160.     EntriesArray( const EntriesArray& );          // need custom copy-constructor
  161.     ~EntriesArray();                              // need custom destructor
  162.     EntriesArray& operator=(const EntriesArray&); // need custom assignment-operator
  163.  
  164.     CEntry& operator[] (const int index) {
  165.         if (index < 1 || index > length) {
  166.             throw ArrayOutOfBounds();
  167.         }
  168.         return *entries[index - 1];
  169.     };
  170.  
  171. };
  172.  
  173. int main (int argc, const char * argv[])
  174. {
  175.     try {
  176.         EntriesArray a(5);
  177.         // a.entries[0] = new CEntry(CKey(1), CValue(1));
  178.         a[1] = new CEntry(CKey(1), CValue(1));
  179.         cout << a[1].val.value << endl;
  180.        
  181.        
  182.     } catch (exception& e) {
  183.         cout << e.what() << endl;
  184.     }
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement