Help with overloading

HI guys,

Working a problem from a rather old C++ book. Everything in the program works, except the overloaded functions (which were provided as part of the program).

The idea is to take two old ENglish sterling amounts, convert it a double, add the doubles and return the new Sterling object.

However, when I run the overloaded functions, the program returns a bunch of gibberish numbers.

I would like get some advice, before I move to the next step, that is define the overloaded functions more explicitly, ie. not using the doubles.

Any help would be greatly appriciated.

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
class Sterling
{
public:
	Sterling(){}
	Sterling(double p, int s, int pe):pounds(p),shilling(s),pence(pe){}
	friend istream& operator>>(istream& in, Sterling& s);
	friend ostream& operator<<(ostream& out, Sterling& s);
	operator double();
	Sterling (double decpound);
	Sterling operator+(Sterling& s){
		return Sterling(double(Sterling(pounds,shilling,pence))+double(s));//problem here
	}
	Sterling operator-(Sterling& s){
		return Sterling(double(Sterling(pounds,shilling,pence))-double(s));
	}
	Sterling operator*(Sterling& s){
		return Sterling(double(Sterling(pounds,shilling,pence))*double(s));
	}
	Sterling operator/(Sterling& s){
		return Sterling(double(Sterling(pounds,shilling,pence))/double(s));
	}
private:
	int shilling, pence;
	double pounds;
};  

int main()
{
	char choice;
	Sterling one(4,10,12), two(12,4,8);

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	do
	{
		
		cout<<"First sterling value: "<<one<<endl<<endl;
		cout<<"Second sterling value: "<<two<<endl;
		Sterling add=one+two;//problem here
		cout<<add<<endl;
		
		cout<<"\n\nAgain (y/n): ";
		cin>>choice;
	}while(choice=='y');


	_getch();
	return 0;
}
Sterling::Sterling (double decpound)
{
	double decfrac, decshill,decpence;
	Sterling temp;

	temp.pounds=static_cast<double>(decpound);
	decfrac=decpound-temp.pounds;
	decshill=20*decfrac;

	temp.shilling=static_cast<double>(decshill);
	decfrac=decshill-temp.shilling;
	decpence=12*decfrac;

	temp.pence=static_cast<double>(decpence);

	decshill=static_cast<double>(decshill);
	

}
Sterling::operator double()
{
	return(static_cast<double>(pounds)+((static_cast<double>(shilling)/20)+(static_cast<double>(pence)/240)));
}
ostream& operator<<(ostream& out, Sterling& s)
{
	out<<"\x9c "<<s.pounds<<"."<<s.shilling<<"."<<s.pence;
	return out;
}

istream&  operator>>(istream& in, Sterling& s)
{
	cout<<"Enter the pounds: ";in>>s.pounds;
	while(s.pounds<0){
		cout<<"Pounds can't be less than zero, try again: ";
		in>>s.pounds;
	}
	cout<<"Enter the shillings: ";in>>s.shilling;
	while(s.shilling<0){
		cout<<"Shillings can't be less than zero, enter again: ";
		in>>s.shilling;
	}
	cout<<"Enter the pence: ";in>>s.pence;
	while(s.pence<0){
		cout<<"Pence can't be less than zero, enter again: ";
		in>>s.pence;
	}
	return in;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sterling::Sterling (double decpound)
{
	double decfrac, decshill,decpence;
	Sterling temp;

	temp.pounds=static_cast<double>(decpound);
	decfrac=decpound-temp.pounds;
	decshill=20*decfrac;

	temp.shilling=static_cast<double>(decshill);
	decfrac=decshill-temp.shilling;
	decpence=12*decfrac;

	temp.pence=static_cast<double>(decpence);

	decshill=static_cast<double>(decshill);
}
¿when do you assign anything to the object being constructed?

¿Why so many casts?
I may have overdone the casts, now that you mention it.

But the Sterling::Sterling (double decpound) is working well, it does what it's supposed to. I don't think any assignment is needed, the constructor is simply used for conversion.
it does what it's supposed to
Nope, it does nothing.
That's why you are seeing garbage.

You never set to anything this->pounds
In the book I am reading that constructor is supposed to convert from double to class object, and it does that admirably. Can you tell me then what needs to be done. I have not covered "this->" although I think it has something to do with pointers.

Thanks
Your constructor Sterling(double) should be setting the classes private variables
1
2
	int shilling, pence;
	double pounds;
but it doesn't.
Last edited on
Thanks a lot naraku! You rock, damn program was a thorn in my side for the past day and a half. So inside a constructor, you can't (or should not) create a separate object ? You should only deal with private variables?

Thanks,

Mike
You can create as many temporaries as you want, it just doesn't make much sense. Also, the casts make no sense either, you are literally casting a double to a double.
Topic archived. No new replies allowed.