Nested Classes, Operator Overloading and Sorting

Was randomly practicing some techniques I felt like putting together. Nothing to brag, but it was fun :). P.S It compiles just fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <algorithm>
#include <vector>

class X{
public:
    class Y{
    public:
        class Z{
            public:
                struct XYZ{
                    XYZ();
                    XYZ(int);
                    XYZ operator+(XYZ);
                    XYZ operator-(XYZ);
                    XYZ operator/(XYZ);
                    XYZ operator*(XYZ);
                    XYZ operator%(XYZ);
                    int GetObj();
                private:
                    int object;
                };
        };
    };
};

X::Y::Z::XYZ::XYZ(){/*Nada aqui...*/}

X::Y::Z::XYZ::XYZ(int object){
    this->object = object;
}

X::Y::Z::XYZ X::Y::Z::XYZ::operator+(XYZ object){
    XYZ newObj = object;
    newObj = this->object + object.object;
    return(newObj);
}

X::Y::Z::XYZ X::Y::Z::XYZ::operator-(XYZ object){
    XYZ newObj = object;
    newObj = this->object - object.object;
    return(newObj);
}

X::Y::Z::XYZ X::Y::Z::XYZ::operator/(XYZ object){
    XYZ newObj = object;
    newObj = this->object / object.object;
    return(newObj);
}

X::Y::Z::XYZ X::Y::Z::XYZ::operator*(XYZ object){
    XYZ newObj = object;
    newObj = this->object * object.object;
    return(newObj);
}

X::Y::Z::XYZ X::Y::Z::XYZ::operator%(XYZ object){
    XYZ newObj = object;
    newObj = this->object % object.object;
    return(newObj);
}

int X::Y::Z::XYZ::GetObj(){
    return(object);
}

//Setting up global vars
X::Y::Z::XYZ x(11), y(5), z = x + y; 

int main(int argc, char* argv[]){

    X::Y::Z::XYZ x(10), y(4), z = x + y; //Setting up local vars

    int XYZ[]={
        ::z.GetObj(), z.GetObj()
    };

    std::vector<int> vect(XYZ, XYZ + 2);
    
    //Not sorted
    std::cout<<"Not Sorted"<<std::endl;
    for(int notSorted(0); notSorted<2; ++notSorted){
        std::cout<<vect[notSorted]<<std::endl;
    }

    //Anything beyond is sorted :)
    std::sort(vect.begin(), vect.end());

    std::cout<<"Sorted"<<std::endl;
    for(int sorted(0); sorted<2; ++sorted){
        std::cout<<vect[sorted]<<std::endl;
    }

    //IM using Dev BLood Shed thats y 
    std::cin.sync(); // <----^
    std::cin.ignore(); // <----^
    std::cin.clear(); // <----^
    return 0;
}
Last edited on
Cool
Topic archived. No new replies allowed.