Quick Question

What mechanisms can we use to represent different degrees of similarity among entities in C++?
closed account (o3hC5Di1)
Smells like homework - different way to put the question: Which ways can you compare data of equal or differing datatypes?

There are ones that operate easily, others function differently, even others can be overloaded with complexity.
*hint*

All the best,
NwN
It doesn't really affect my homework, the assignment is an actual code, but this question was asked and I don't understand what it is they are asking.

Is there some special function we use for comparison? The code i am writing deals with IF statements as well as Inheritance, but I don't see where I need this knowledge asked based on the code I am currently writing. My instructor is a bit off topic at times.

Only thing I can think of is when I combine operators into the same line (forgive me if my jargon is incorrect). For instance, this is part of my code:

Hourly::Hourly():Employee(),MIN_WAGE(10),MAX_WAGE(75),MIN_HOUR(0),MAX_HOUR(50) //Default constructor of Hourly class calling default construcor of parent Employee class
//Overload constructor of Hourly class.
{
wage = MIN_WAGE; //Initialize wage to default min wage value
hours = MIN_HOUR; //Initialize hour to default min hour value
category = "Unknown"; //Initialize category to default value
}
It would be prudent to include a few notes in that code that tells us more specifically what it does/what its for.
The task for the above code was:
"Create a class called Hourly that is also derived from Employee."

I have a ton of code to write but this is the section I thought the Professor may be talking about. I still don't understand his original question that I posted originally.
closed account (o3hC5Di1)
Well, if he means what I think he means he's asking which ways you have to compare data.

You can use relational operators (==, <, >, etc) on variables with the same datatype, some built in classes, such as std::string, have member functions that will compare ( string1.compare(string2); ), or you can overload the relational operators of a class so that you can compare objects of that class to other datatypes.

Does that make sense to you, or would you prefer a more in depth explanation?

All the best,
NwN
Makes sense, I finished my code without much need for the original question asked, i think that is why I get so confused because the Professor gets ahead of himself, so I begin to question my current assignment.

So far I have not had to use any compare functions, perhaps next week i may so this conversation is helpful. Currently I am only dealing with overload constructors and such.

I'm going to read more into the compare function to get a better grasp on future projects. Thank you for your help!
there isn't a "compare" function. Only operators.

And you use operators a WHOLE lot, especially with if, then, else statements.
Last edited on
IWishIKnew wrote:
there isn't a "compare" function

Oh? What's this? (from: http://www.cplusplus.com/reference/string/string/compare/)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 // comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;
}
Topic archived. No new replies allowed.