473,793 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritence and overloading operators

Hi Guys, not sure if anyone can help me on this one but......

I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.

although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.

Now I know that I can use mybase to access the base object of an
inherited object but as the overloaded function takes in two arguments
of type (derived class) how can I access the base class to do the
comparison.

Thanks in advance,

Dean.

Mar 15 '07 #1
3 1366
Noodle wrote:
I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.
I think you mean /Overridden/, not Overloaded.
You override methods in base classes where the arguments are the same,
but you want them to do something different
You Overload methods (in the same class or its Superclass) when you want
to do the same /sort/ of thing, but supplying different arguments.
although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.
So ask the base class if /it/ think things are the same, based on /its/
view of the world and, if it says that every thing's OK, then "second
guess" it, based on the subclasses view of things. (I haven't done
overloaded operators yet, but here's the same idea with functions):

Class Super
Public Property X() as ...
Public Property Y() as ...

Public Overridable Function Equals( _
other as Super _
) as Boolean
If Me.X <other.X Then
Return False
End If
If Me.Y <other.Y Then
Return False
End If
Return True
End Function
End Class

Class Subb
Public Property Z() as ...

Public Overrides Function Equals( _
other as Subb _
) as Boolean
If Not MyBase.Equals( other ) Then
Return False
End If

If Me.Z <other.Z Then
Return False
End If

Return True
End Function
End Class

HTH,
Phill W.
Mar 15 '07 #2
On 15 Mar, 12:22, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
Noodle wrote:
I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.

I think you mean /Overridden/, not Overloaded.
You override methods in base classes where the arguments are the same,
but you want them to do something different
You Overload methods (in the same class or its Superclass) when you want
to do the same /sort/ of thing, but supplying different arguments.
although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.

So ask the base class if /it/ think things are the same, based on /its/
view of the world and, if it says that every thing's OK, then "second
guess" it, based on the subclasses view of things. (I haven't done
overloaded operators yet, but here's the same idea with functions):

Class Super
Public Property X() as ...
Public Property Y() as ...

Public Overridable Function Equals( _
other as Super _
) as Boolean
If Me.X <other.X Then
Return False
End If
If Me.Y <other.Y Then
Return False
End If
Return True
End Function
End Class

Class Subb
Public Property Z() as ...

Public Overrides Function Equals( _
other as Subb _
) as Boolean
If Not MyBase.Equals( other ) Then
Return False
End If

If Me.Z <other.Z Then
Return False
End If

Return True
End Function
End Class

HTH,
Phill W.
Hi Phill, yeah I meant overrides (doh). What you've suggested wont
work as to overload an operator you have to have arguments for each
class at the side of the operator in the expression.

eg -

Public Shared Operator =(ByVal olhs As CampaignDeal, ByVal orhs As
CampaignDeal) As Boolean
This means that I do not have access to the mybase functionality for
the passed in arguments. I have figured it out now anyway, to compare
the base class I do a directcast of the two arguments to the inherited
class type and compare those, then if they equal start checking any
attributes that the derived class has extra.

Mar 15 '07 #3
On 15 Mar, 13:01, "Noodle" <d...@headnoodl e.comwrote:
On 15 Mar, 12:22, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:


Noodle wrote:
I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.
I think you mean /Overridden/, not Overloaded.
You override methods in base classes where the arguments are the same,
but you want them to do something different
You Overload methods (in the same class or its Superclass) when you want
to do the same /sort/ of thing, but supplying different arguments.
although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.
So ask the base class if /it/ think things are the same, based on /its/
view of the world and, if it says that every thing's OK, then "second
guess" it, based on the subclasses view of things. (I haven't done
overloaded operators yet, but here's the same idea with functions):
Class Super
Public Property X() as ...
Public Property Y() as ...
Public Overridable Function Equals( _
other as Super _
) as Boolean
If Me.X <other.X Then
Return False
End If
If Me.Y <other.Y Then
Return False
End If
Return True
End Function
End Class
Class Subb
Public Property Z() as ...
Public Overrides Function Equals( _
other as Subb _
) as Boolean
If Not MyBase.Equals( other ) Then
Return False
End If
If Me.Z <other.Z Then
Return False
End If
Return True
End Function
End Class
HTH,
Phill W.

Hi Phill, yeah I meant overrides (doh). What you've suggested wont
work as to overload an operator you have to have arguments for each
class at the side of the operator in the expression.

eg -

Public Shared Operator =(ByVal olhs As CampaignDeal, ByVal orhs As
CampaignDeal) As Boolean

This means that I do not have access to the mybase functionality for
the passed in arguments. I have figured it out now anyway, to compare
the base class I do a directcast of the two arguments to the inherited
class type and compare those, then if they equal start checking any
attributes that the derived class has extra.- Hide quoted text -

- Show quoted text -
damn done it again, i mean override :(

Mar 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2013
by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the rigth hand side of < did not work. Expanded version: In a class of numbers I have been able to overload the four arithmetic operators +,-,* and / by defining the methods __add__ and __radd__ (etc..) in my class. This enables me to evaluate...
16
3099
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2276
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this group probably would know. By "overloading composite operators" I mean conversion of an expression like A = B * C; into a single function call (instead of three calls; one to "*", another to copy and another to "="). Here A, B and C are of a
20
1847
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having a bit of a problem in seeing what needs to happen here. I am just not sure how I do this. Will the overloading function recognize a < and a usual <? Do I do an IF (a.letters < b.letters){ return true }
5
3632
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
15
1750
by: PengYu.UT | last post by:
Hi, It seems that C++ does not allow overloading operators for primative types, e.g. int, double. I'm wondering whether it is ture or there is some walk-around? Thanks, Peng #include <iostream>
8
2977
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
7
1745
by: Rahul | last post by:
Hi Everyone, I was overloading the operator= function as a class member function, #include <iostream.h> class A { int value; public : A& operator = (const A& ref)
2
2950
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the modification step. My thought was that this might be able to be completed via overloading, but I am not sure if (or how) it could be done. overloadings: + ==OR
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.