Assistance with classes and functions.

Hi,

I am having a very difficult time with this. Yes, it's an assignment but I can't wrap my head around it. It compiles just fine, but the numbers are off.
Your help is greatly appreciated.

Thanks.

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
122
123
124
125
126
#include <iostream>

using namespace std;

class Number
{	
private:
	int num;
	int twiceNum;
public:
	Number(int n = 0);
	Number(const Number & n);
	void setNum(int n);
	void setTwiceNum(int tn);
	int getNum() const;
	int getTwiceNum() const;
	Number operator+ (const Number & r);
	Number operator++ ();
	Number operator++ (int);
	friend void Reset(Number &t);
};
Number::Number(int n)
{
	setNum(n);
}
Number::Number(const Number & n)
{
	setNum(num=0 & twiceNum);
}
void Number::setNum(int n)
{
	num = n;
	twiceNum = 2 * n;
}
void Number::setTwiceNum(int tn)
{
	if (tn % 2 == 0)
	{
		twiceNum = tn;
	}
	else
	{
		twiceNum = 0;
	}
	twiceNum = num / 2;
}
int Number::getNum() const
{
	return num;
}
int Number::getTwiceNum() const
{
	return twiceNum;
}
Number Number::operator+ (const Number & r)
{
	Number sum;
	sum.num = num + r.num;
	sum.twiceNum = twiceNum + r.twiceNum;
	&Number::setNum;
	return sum;
	
}
Number Number::operator++ ()
{
	++num;
	&Number::setNum;
	return *this;
}
Number Number::operator++ (int)
{
	Number old(*this);
	num++;
	&Number::setNum;
	return old;
}
void Reset(Number &t)   
{
	t.num = 0;
	t.twiceNum = 0;
}
int main()
{
	Number a, b(5), c(10);
	Number d=c;
	Number e;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout<<"c: " <<c.getNum() << ", 2 X c: "<<c.getTwiceNum()<<endl;
	cout<<"d: " <<d.getNum() << ", 2 X d: "<<d.getTwiceNum()<<endl;
	cout <<endl;
	Reset(b);
	cout <<"After reset of b"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setNum(7);
	cout <<"After b.setNum(7)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setTwiceNum(21);
	cout <<"After b.setTwiceNum(21)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setTwiceNum(30);
	cout <<"After b.setTwiceNum(30)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	a= b + c;
	cout <<"After a= b+c"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout<<"c: " <<c.getNum() << ", 2 X c: "<<c.getTwiceNum()<<endl;
	cout <<endl;
	e= ++a;
	cout <<"After e= ++a"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"e: " <<e.getNum() << ", 2 X e: "<<e.getTwiceNum()<<endl;
	cout <<endl;
	e= a++;
	cout <<"After e= a++"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"e: " <<e.getNum() << ", 2 X e: "<<e.getTwiceNum()<<endl;
	cout <<endl;
	return 0;
}



The output should be:

a: 0, 2 X a: 0
b: 5, 2 X b: 10
c: 10, 2 X c: 20
d: 10, 2 X d: 20

After reset of b
b: 0, 2 X b: 0

After b.setNum(7)
b: 7, 2 X b: 14

After b.setTwiceNum(21)
b: 0, 2 X b: 0

After b.setTwiceNum(30)
b: 15, 2 X b: 30

After a = b+c
a: 25, 2 X b: 50
b: 15, 2 X b: 30
c: 10, 2 X b: 20

After e = ++a
a: 26, 2 X a: 52
e: 26, 2 X e: 52

After e = a++
a: 27, 2 X a: 54
e: 26, 2 X e: 52

Press any key to continue . . .

Last edited on
Create a class called Number. The Number class will have 2 private member
variables.


int num stores a number
int twiceNum stores 2 X num

It will have the following public member functions:

Number(int n =0)
It will call setNum and pass in n

Number(const Number & n)
It will call setNum and pass in an argument so that
the Number object being constructed will have the
same values in its member variables as the
parameter object.


void setNum(int n)
It will set num to n It will set twiceNum to 2 X n

void setTwiceNum(int tn)
If tn is even, it will set twiceNum to tn
If tn is odd, it will set twiceNum to 0
It will set num, to be half of twiceNum


int getNum() const
It will return num

int getTwiceNum() const
It will return twiceNum

Number operator + (const Number & r)
It will add two Number objects together and
return the sum. It should call the setNum function
and not use the assignment operator.


Number operator ++ ()
It will pre-increment the Number object by
adding 1 to the num member variable. It should
call the setNum function and not use the
assignment operator.


Number operator ++ (int)
It will post-increment the Number object by
adding 1 to the num member variable.It should
call the setNum function and not use the assignment operator.


It will have the following stand-alone function as a friend:

void Reset (Number &t)
It will set the num member variable to 0
It will set twiceNum member variable to 0
It will use the assignment operator (i.e. = ) and not
call the set functions
Last edited on
1
2
3
4
Number::Number(const Number & n)
{
	setNum(num=0 & twiceNum);
}

What do you think this does?


1
2
3
4
5
6
7
8
9
10
11
12
void Number::setTwiceNum(int tn)
{
	if (tn % 2 == 0)
	{
		twiceNum = tn;
	}
	else
	{
		twiceNum = 0;
	}
	twiceNum = num / 2;
}

Where is num updated? Why is twiceNum overwritten immediately after it is assigned a value?


&Number::setNum;
What do you think that does?

The implementations of operator++ don't update twiceNum.
Last edited on
Hi Cire,

I was going down the list of what needed to be done to accomplish the output.

1) the first part is to call "setNum" and pass in an argument so that the Number object being constructed will have the same values in its member variables as the parameter object.

2) If tn is even, it will set twiceNum to tn. If tn is odd, it will set twiceNum to 0. It will set num, to be half of twiceNum.

3) That is supposed to be calling the setNum function.

I'm seeing stars and have been reading and working on this for (don't laugh), 30+ hours.

1) Where in setNum(num=0 & twiceNum) do you see the parameter object being referenced?

2) You never assign to num, so I can't see how that's the case.

3) It doesn't do that. It's effectively a no-op. You take the address of the number function setNum, but you don't call it.
My comments inline:
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
Number::Number(int n)
{
	setNum(n); // Good
}
Number::Number(const Number & n)
{
	setNum(num=0 & twiceNum); // Not good
//      setNum( n.num ); <-- Should be this
}
void Number::setNum(int n)
{
	num = n;
	twiceNum = 2 * n; // Good
}
void Number::setTwiceNum(int tn)
{
	if (tn % 2 == 0)
	{
		twiceNum = tn;
	}
	else
	{
		twiceNum = 0;
	}
	twiceNum = num / 2; // Wrong
//      num = twiceNum / 2; // twiceNum is already set, we need num
}
int Number::getNum() const
{
	return num; // Good
}
int Number::getTwiceNum() const
{
	return twiceNum; // Good
}
Number Number::operator+ (const Number & r)
{
	Number sum;
	sum.num = num + r.num; // The assignment fobids the assignment operator
	sum.twiceNum = twiceNum + r.twiceNum;
	&Number::setNum; // This is actually extracting the address of the setNum function
	return sum;

//      Number sum; // This does it as required by the assignment
//      sum.setNum( num + r.num );
//      return sum;
	
}
Number Number::operator++ ()
{
	++num;
	&Number::setNum; // Whoa.  What's going on here?
	return *this;

//      setNum( num + 1 );
//      return *this;
}
Number Number::operator++ (int)
{
	Number old(*this);
	num++;
	&Number::setNum; // Same thing... crazy
	return old;

//      Number old(*this); // Uses setNum as required in the assignment
//      setNum( num + 1 );
//      return old;
}
void Reset(Number &t)   
{
	t.num = 0;
	t.twiceNum = 0; // Good
}
Last edited on
Stewbond,

Again, thank you very much for your help! A few tweaks from cire's insight, and your vast knowledge helped tremendously. I can sleep now. lol

Thanks again, sincerely!


Topic archived. No new replies allowed.