Saturday, June 10, 2017

Uglyclass and Its Wrapper

/*This code was written by A. El-Gadi of Tripoli University for educational purposes. The code is a model answer to a Spring-2017 2nd exam question. For any questions use the comment section below.*/

#include <iostream>

using namespace std;

class Uglyclass{public:
int x1, x2, x3, x4, x5, x6;
};

class Wrapper{
    private:
    Uglyclass uc;

    public:
   
    int& operator [](int index){
            if(index<=1){return uc.x1;}
            else{
                if(index>=6){return uc.x5;}
                else{
                    switch(index){
                       
                        case 2: return uc.x2;
                        case 3: return uc.x3;
                        case 4: return uc.x4;
                        case 5: return uc.x5;
                        }
                    }
            }
        }
};

int main(){
Wrapper w;

w[0]=2; w[2]=3; w[3]=4; w[4]=5; w[5]=6; w[7]=8;

for(int i=0; i<6; i++){cout<<w[i];}
return 0;
}

No comments:

Post a Comment