Inhering overloaded functions

Hi guys,

Is there a way to inherit the overloaded functions from Sterling to Sterfrac? Please note I have not done pointers or virtual functions.

If there's a way, could you please show me the syntax? Also, how to introduce the "eights" integer into the inherited overloaded function (that's a mouthful).

Thanks,

Mike

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
127
128
129

class Sterling
{
public:
	Sterling(){}
	Sterling(double p, int s, int pe):pounds(p),shilling(s),pence(pe){}
	operator double();
	Sterling (double decpound);
	void input()
	{
		cout<<"Enter the pounds: ";cin>>pounds;
		while(pounds<0){
			cout<<"Pounds can't be less than zero, try again: ";
			cin>>pounds;
		}
		cout<<"Enter the shillings: ";cin>>shilling;
		while(shilling<0){
			cout<<"Shillings can't be less than zero, enter again: ";
			cin>>shilling;
		}
		cout<<"Enter the pence: ";cin>>pence;
		while(pence<0){
			cout<<"Pence can't be less than zero, enter again: ";
			cin>>pence;
		}
	}
	void print()
	{
		cout<<"\x9c "<<pounds<<"."<<shilling<<"."<<pence;
	}


	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));
	}
	Sterling operator*(double d){
		return Sterling(double(Sterling(pounds,shilling,pence))*d);
	}
	Sterling operator/(Sterling& s){
		return Sterling(double(Sterling(pounds,shilling,pence))/double(s));
	}
	Sterling operator/(double d){
		return Sterling(double(Sterling(pounds,shilling,pence))/d);
	}
	

protected:
	int shilling, pence;
	double pounds;
};  
class Sterfrac:public Sterling{

public:
	Sterfrac():Sterling(){}
	Sterfrac(double p, int s, int pe):Sterling(p,s,pe){}
	Sterfrac(double p, int s, int pe,int e):eights(e),Sterling(p,s,pe){
	
	}
	void input(){
		Sterling::input();
		cout<<"Enter the eights: ";
		cin>>eights;
		while(eights<1 || eights>8){
			cout<<"Out of bounds, value has to be [1,8]: ";
			cin>>eights;
		}
	
	}
	//Is it possible to inherit overloaded here?
	void print(){
		Sterling::print();
		//reduce fraction here!!
		cout<<" - "<<eights<<"/"<<8<<endl;
	}
	
private:
	int eights;
	
};


int main()
{
	char choice;
	
	Sterfrac test(3,1,4,2);
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	do
	{
		
		test.print();
		cout<<"\n\nAgain (y/n): ";
		cin>>choice;
	}while(choice=='y');


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

	pounds=(decpound);
	decfrac=decpound-pounds;
	decshill=20*decfrac;

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

	pence=static_cast<double>(decpence);

}
Sterling::operator double()
{
	return(static_cast<double>(pounds)+((static_cast<double>(shilling)/20)+(static_cast<double>(pence)/240)));
}
Since every method in Sterling is public there is no need to worry about inheriting them. You can just use them at will.

Inheritance works like this:

class A
{
public:
int a;
protected:
int b;
private:
int c;
};

class B : public A
{
B ( )
{
a is accessible
b is accessible
c is not accessible
}

int main()
{
A a;
B b;

a.a; // ok
a.b // not ok
a.c // not ok

b.a // not ok
b.b // not ok
b.c // not ok

}

}


Last edited on
Since every method in Sterling is public there is no need to worry about inheriting them. You can just use them at will.


That's exactly right in this case, but it's not good practice to have everything public. It's the same as having global variables, and allows anything to change the value or variables.

Generally it's good to make everything private or protected, then provide public set and get functions that retrieve the info.

I have a real life example of this in another post, if you are interested. I have to go out now, I can post example when I get back.
I have not understood what overloaded functions you are talking about.
But if you mean the base class constructors then C++ 2011 allows to inherite of base class constructors in a derived class.

So instead of

1
2
3
4
5
	Sterfrac():Sterling(){}
	Sterfrac(double p, int s, int pe):Sterling(p,s,pe){}
	Sterfrac(double p, int s, int pe,int e):eights(e),Sterling(p,s,pe){
	
	}


you could simply write

using Sterling::Sterling;

However the problem is that I do not know which compiler supports inheriting constructors.:)
But in any case instead of Sterfrac():Sterling(){} you could write simply
Sterfrac() {}
Last edited on
Generally it's good to make everything private or protected, then provide public set and get functions
Great, you just made everything public.
Generally it's good to make everything private or protected, then provide public set and get functions that retrieve the info.


Sorry I wasn't quite clear on that. I meant provide public set and get functions that retrieve info that should be available publicly .



In C++, It is best to make all your member variables private or protected, then provide public functions to set and get the member variables.

Here's a somewhat complex example:

Say you want to have the ability to create an arc object by sending it 3 2d points. The 3 points are the beginning of the arc, a point on the arc, and an end point; we might name them A, B, C. Quick geometry lesson: Draw lines AB and BC. The perpendicular Bisectors of these intersect at the center of the arc.

To do these calcs you might need local variable like ABDist, ABBisectorAngle etc.

Now if I was to give you an arc object, you would probably be only interested in: CenterPoint, Radius, StartPoint, EndPoint, say (there are lots of other variations).

So you make everything private or protected, then provide public functions that return only the things that you want. i.e getCenterPoint, getRadius, getStartPoint, getEndPoint.

With set functions, it's the same, and in this case, if you change one thing, you have to recalc all the others.

If everything was public, then changing the CenterPoint would invalidate the others.

Private means that only that class has access to the variables via the code in the member functions, and not via the dot operator. They are inherited, but there won't be access to them in derived classes

Protected means that derived classes also have access to the base's private members via the code in the member functions, and not via the dot operator.




So you have public functions that expose only the variables that should be available publicly, everything else is private or protected.

I hope this is much clearer

Edit:

An Arc is best stored as CenterPoint, Radius, StartAngle, EndAngle. That way Move and Rotate functions are much easier to do.
Last edited on
Topic archived. No new replies allowed.