New Reply

Comparing class objects

 
Share Thread Thread Tools
Aug16-11, 11:12 PM   #1
 

Comparing class objects


Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Heat-related deaths in Manhattan projected to rise
>> Dire outlook despite global warming 'pause': study
>> Sea level influenced tropical climate during the last ice age
Aug16-11, 11:21 PM   #2
 
Recognitions:
Gold Membership Gold Member
Quote by russel.arnold View Post
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
I'm pretty sure object equality has to be done manually by overriding the equality operator and comparing each field of the two objects.
 
Aug17-11, 12:28 PM   #3
 
Mentor
Quote by russel.arnold View Post

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
A loop isn't appropriate at all. There is no way you can write a loop that will compare two strings and an int in one class instance with variables of the same types in another class instance. You will need to do a comparison in the way that DavidSnider suggests.
 
New Reply
Thread Tools


Similar Threads for: Comparing class objects
Thread Forum Replies
Waitlists to get into class at junior college math class ? Academic Guidance 16
How could I call a base class virtual function from the derived class ? (C++) Programming & Comp Sci 2
Why does Chern class belong to INTEGER cohomology class? Differential Geometry 41
How does a Physics class differ from an Engineering class? Academic Guidance 38
Help- comparing objects in free fall Introductory Physics Homework 5