Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#ifndef __PROGTEST__
- //#include "key_value.cpp"
- //#endif /* __PROGTEST__ */
- class CValue{
- public:
- CValue(int x){
- value = x;
- }
- CValue(){
- }
- CValue(const CValue &i){
- value = i.value;
- }
- ~CValue(){
- // value = 0;
- }
- CValue& operator = ( const CValue &i ){
- if(this == &i){
- return *this;
- }
- value= i.value;
- return *this;
- }
- bool operator == ( const CValue &i ) const{
- if(value == i.value){
- return true;
- }
- return false;
- }
- bool operator != ( const CValue &i ) const{
- if(*this == i){
- return false;
- }
- return true;
- }
- bool operator < ( const CValue &i ) const{
- if(value < i.value){
- return true;
- }
- return false;
- }
- bool operator > ( const CValue &i ) const{
- if(value > i.value){
- return true;
- }
- return false;
- }
- bool operator >= ( const CValue &i ) const{
- if(value >= i.value){
- return true;
- }
- return false;
- }
- bool operator <= ( const CValue &i ) const{
- if(value <= i.value){
- return true;
- }
- return false;
- }
- int value;
- };
- class CKey{
- public:
- CKey(int x){
- value= x;
- }
- CKey(){
- }
- CKey(const CKey &i){
- value = i.value;
- }
- ~CKey(){
- }
- CKey& operator = ( const CKey &i ){
- if(this == &i){
- return *this;
- }
- value= i.value;
- return *this;
- }
- bool operator == ( const CKey &i ) const{
- if(value == i.value){
- return true;
- }
- return false;
- }
- bool operator != ( const CKey &i ) const{
- if(value == i.value){
- return false;
- }
- return true;
- }
- bool operator < ( const CKey &i ) const{
- if(value < i.value){
- return true;
- }
- return false;
- }
- bool operator > ( const CKey &i ) const{
- if(value > i.value){
- return true;
- }
- return false;
- }
- bool operator >= ( const CKey &i ) const{
- if(value >= i.value){
- return true;
- }
- return false;
- }
- bool operator <= ( const CKey &i ) const{
- if(value <= i.value){
- return true;
- }
- return false;
- }
- int value;
- };
- #include <iostream>
- #include <exception>
- using namespace std;
- class CEntry {
- public:
- CKey key;
- CValue val;
- CEntry(const CKey& key, const CValue& val) {
- this->key = key;
- this->val = val;
- }
- // CEntry* operator=( CEntry *orig) {
- // *this = orig;
- // return this;
- // };
- };
- class ArrayOutOfBounds: public exception {
- virtual const char* what() const throw()
- {
- return "Array out of bounds!";
- }
- };
- class EntriesArray {
- public:
- CEntry **entries;
- int length;
- EntriesArray( int length ) : length(length), entries(new CEntry*[length]()) {
- }
- EntriesArray( const EntriesArray& ); // need custom copy-constructor
- ~EntriesArray(); // need custom destructor
- EntriesArray& operator=(const EntriesArray&); // need custom assignment-operator
- CEntry& operator[] (const int index) {
- if (index < 1 || index > length) {
- throw ArrayOutOfBounds();
- }
- return *entries[index - 1];
- };
- };
- int main (int argc, const char * argv[])
- {
- try {
- EntriesArray a(5);
- // a.entries[0] = new CEntry(CKey(1), CValue(1));
- a[1] = new CEntry(CKey(1), CValue(1));
- cout << a[1].val.value << endl;
- } catch (exception& e) {
- cout << e.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement