Help with programmer defined exception classes

My question is that I am trying to create a user defined exception class that detects when the user enters the same two values for an ordered pair for example: (2,2) then it should display "The Two Values Entered Are The Same"

Here is the implementation/header file:

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
#include <iostream>
using namespace std;

namespace cs_pairs
{
    template <class T>
    class OrderedPair
    {
    private:
        T first;
        T second;
    public:
        typedef std::size_t size_type;
        typedef T value_type;        // changed int to T
        static const int DEFAULT_VALUE = 0;

        //Our Main Focus is this class
        class DuplicateMemberError {};

        OrderedPair(T newFirst = DEFAULT_VALUE, T newSecond = DEFAULT_VALUE) {
            setFirst(newFirst);
            setSecond(newSecond);
        }

        //Should I insert the if statements here
        void setFirst(T newFirst) {
           if (first == second)
            {
                throw DuplicateMemberError ();
            } 
            first = newFirst; 
        }

        //Also here
        void setSecond(T newSecond) {
            if (first == second)
            {
                throw DuplicateMemberError ();
            } 
            second = newSecond; 
        }

        T getFirst() const {
            return first;
        }

        T getSecond() const {
            return second;
        }

        OrderedPair<T> operator+(const OrderedPair<T>& right) const {
            return OrderedPair(first + right.first, second + right.second);
        }

        bool operator<(const OrderedPair<T>& right) const {
            return first + second < right.first + right.second;
        }

        void print() const {
            cout << "(" << first << ", " << second << ")";
        }
    }; 

}


Here is our main file:

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "orderedpair.h"
using namespace std;
using namespace cs_pairs;


int main() {

    int num1, num2;
    OrderedPair<int> myList[10];

    srand(static_cast<unsigned>(time(0)));
    cout << "default value: ";
    myList[0].print();
    cout << endl;

    for (int i = 0; i < 10; i++) {
        myList[i].setFirst(rand() % 50);
        myList[i].setSecond(rand() % 50 + 50);
    }

    myList[2] = myList[0] + myList[1];

    if (myList[0] < myList[1]) {
        myList[0].print();
        cout << " is less than ";
        myList[1].print();
        cout << endl;
    }

    for (int i = 0; i < 10; i++) {
        myList[i].print();
        cout << endl;
    }

    cout << "Enter two numbers to use in an OrderedPair.  Make sure they are different numbers: ";
    cin >> num1 >> num2;
    OrderedPair<int> x;

    try {
        x.setFirst(num1);
        x.setSecond(num2);
    } catch (OrderedPair<int>::DuplicateMemberError e) {
        x.setFirst(OrderedPair<int>::DEFAULT_VALUE);
        x.setSecond(OrderedPair<int>::DEFAULT_VALUE);
    }
    
    cout << "The resulting OrderedPair: ";
    x.print();
    cout << endl;
}
Last edited on
Here's a start - your default (0,0) breaks the rule. (Keep in mind there are better ways other than an exception to handle the equals rule but I guess you are stuck with it )

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <exception>

using namespace std;

namespace cs_pairs
{

class DuplicateMemberError: public exception
{
    virtual const char* what() const throw()
    {
        return "Error: first == second";
    }
}DuplicateMemberError;

template <class T>
class OrderedPair
{
private:
    T first;
    T second;
public:
    typedef std::size_t size_type;
    typedef T value_type;        // changed int to T
    static const int DEFAULT_VALUE = 0;
    
    OrderedPair(T newFirst = DEFAULT_VALUE, T newSecond = DEFAULT_VALUE) {
        
        try{
            if(newFirst == newSecond)
                throw DuplicateMemberError;
        }
        
        catch (exception& e)
        {
            cout << e.what() << '\n';
        }
        
        setFirst(newFirst);
        setSecond(newSecond);
    }
    
    void setFirst(T newFirst) {
        first = newFirst;
    }
    void setSecond(T newSecond) {
        second = newSecond;
    }
    
    T getFirst() const {
        return first;
    }
    
    T getSecond() const {
        return second;
    }
    
    OrderedPair<T> operator+(const OrderedPair<T>& right) const {
        return OrderedPair(first + right.first, second + right.second);
    }
    
    bool operator<(const OrderedPair<T>& right) const {
        return first + second < right.first + right.second;
    }
    
    void print() const {
        cout << "(" << first << ", " << second << ")";
    }
};
}


#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
using namespace cs_pairs;


int main() {
    
    int num1, num2;
    OrderedPair<int> myList[10];
    
    srand(static_cast<unsigned>(time(0)));
    cout << "default value: ";
    myList[0].print();
    cout << endl;
    
    for (int i = 0; i < 10; i++) {
        myList[i].setFirst(rand() % 50);
        myList[i].setSecond(rand() % 50 + 50);
    }
    
    myList[2] = myList[0] + myList[1];
    
    if (myList[0] < myList[1]) {
        myList[0].print();
        cout << " is less than ";
        myList[1].print();
        cout << endl;
    }
    
    for (int i = 0; i < 10; i++) {
        myList[i].print();
        cout << endl;
    }
    
    cout << "Enter two numbers to use in an OrderedPair.  Make sure they are different numbers: ";
    cin >> num1 >> num2;
    
    
    OrderedPair<int> x(num1, num2);
    
    
    cout << "The resulting OrderedPair: ";
    x.print();
    cout << endl;
}
Topic archived. No new replies allowed.