Class help

Create a class called Number and another called Double. These classes should have separate header and .cpp file

Each of these classes should have the following functions (methods)
greater - This is a void function function that sets the value of the object. ex: double should have a double value

Double c;
c.greater(2.25);
plus
minus
times
divide

-Each of these functions should take its type as an argument and return the same.
-The Integer class should have a function called

aInt() returns a primitive integer ,it is the data section that gets returned.

-The Double class should have a function called aDouble this returns a primitive double

-the main funciton should have the code thattests the instance of your classes

Can anyone provide me an example on how I would set something similar up?
This is what I have so far and I am so lost. I started it but I think it is way off. I really need help on at least an example on how to do the integer class. I don't understand where to place the aint() function or what it does. If someone could show me where to place that in the code it would help alot.
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
//Number.h (header)
#ifndef NUMBER
#define NUMBER

class NUMBER
{
private:
int num1;
int num2;

public: 
void setGreater (int n, int p);
Number plus(Number &n);
Number minus (const Number &n);
Number times (const Number &n);
Number div ( const Number &n);

};
//cpp file
#include <iostream>
#include "Number.h"
using namespace std;

void Number::setNumber(int n, int p)
{
num1=n;
num2=p;
}
Number Number::plus(const Number&n)
{
Number tmp;
tmp.num=(num1+num2);
return tmp;
}
Number Number:: minus (const Number &n)
{
Number tmp;
tmp.num=(num1-num2);
return tmp;
}
Number Number::times(const Number &n)
{
Number tmp;
tmp.num=num1*num2;
return tmp;
}
Number Number:: div (const Number &n)
Number.tmp;
tmp.num=num1/num2;
}
//main file
#include <iostream>
#include "Number.h"
using namespace std;

int main()
{
Number n1,n2,n3;
n1.setNumber (1,2);
n2.setNumber (1,2);
n3.printNumber;
return 0;
}


This is nonsense, tell your teacher that this assignment is ludicrous because the function names to not correlate to their actions; and the assignment, while it has a very good opportunity to, does not teach the C++ concepts of operator overloading.

Here is something to chew on:
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
class Double
{
    double d;
public:
    Double() : d(0) {}
    Double(const Double &from) : d(from.d) {}
    Double(double from) : d(from) {}

    Double &operator=(const Double &from)
    {
        d = from.d;
        return *this;
    }

    operator double(){ return d; }

    bool operator<(const Double &than) const { return d < than.d; }
    bool operator>(const Double &than) const { return d > than.d; }
    bool operator<=(const Double &to) const { return d <= to.d; }
    bool operator>=(const Double &to) const { return d >= to.d; }
    bool operator==(const Double &to) const { return d == to.d; }
    bool operator!=(const Double &to) const { return d != to.d; }

    Double &operator+=(const Double &with)
    {
        d += with.d;
        return *this;
    }
    Double &operator-=(const Double &by)
    {
        d -= by.d;
        return *this;
    }
    Double &operator*=(const Double &with)
    {
        d *= with.d;
        return *this;
    }
    Double &operator/=(const Double &by)
    {
        d /= by.d;
        return *this;
    }
    //plus global operators for Double &operator@=(double &a, const Double &b)

    Double operator+(const Double &with) const { return Double(*this) += with; }
    Double operator-(const Double &by) const { return Double(*this) -= by; }
    Double operator*(const Double &with) const { return Double(*this) *= with; }
    Double operator/(const Double &by) const { return Double(*this) /= by; }
    //plus global operators for Double operator@(double a, const Double &b)
};
THIS is what the assignment SHOULD have had you write. However this isn't what the assigment asked so turning this in will get you a zero for sure.
Last edited on
Topic archived. No new replies allowed.