You haven't been suddenly thrown in the water to swim, after living in the desert for your entire life, so the question is what have you been doing on your classes until now? :)
Start coding it (the task is very clear) and let's see where and why you get stuck.
Comment has been collapsed.
Well it would be a lot easier if we actually covered the information last semester, but we ran out of time and he just expects us to know the stuff this semester.....
Comment has been collapsed.
Welcome to the world of coding. Everyday you have to learn.
After semester of C++ you should know how to write simple class. Task you were given doesn't need any advanced knowledge.
Don't expect that any coder will do your homework for you. Try doing it yourself and we will try to help you if you get stuck.
Comment has been collapsed.
Not gonna do your work for you but I'll give you a brief overview of what you have to do here.
1) Design a class, say Month. Should Look something like this:
class Month { int month_id; };
//Good idea to design a constructor too.
Month(int month);
//Some Methods
int getMonth(); int setMonth();
2) Design a method(for the class) to increment the months. Something like this.
void Month::advance(int inc_num) { } // where inc_num is the value by which you want to increment the month of the class.
void Month::advance() { } // Overloaded method without Arguments to increment by 1.
3) Now, we want months in a String notation too. For this, simply overload the methods you've created previously to use character arrays. Create a logic to associate the Strings to int. For instance, 1 means JAN, 2 means FEB, etc.
Comment has been collapsed.
This is a pretty good starting point and I agree with moonstruckhorrors.
1) Your class should probably have 2 constructors, one taking int as a param and 1 taking strings which you should parse
2) Your advance method should take an int as a param, a good idea to be able to scroll through years would be something along
currentMonth + (monthsToAdd % 12)
3)I completely agree, overload your getters and setters and parse your months.
If you want to write something more complete take a look at "std::strftime" and all the time manipulating methods( which I think your teacher would probably like )
Comment has been collapsed.
2) should be: currentMonth = (currentMonth + monthsToAdd) % 12; //for zero-indexed months
Comment has been collapsed.
You might get better results if you ask a question on stackoverflow
Comment has been collapsed.
I'm pretty sure the first question would be "what have you got so far?", stackoverflow is indeed helpful if you get stuck, but users there aren't willing to write complete programs for you.
Comment has been collapsed.
First, design the public interface of your class. Do not try to implement it. Just provide empty public functions. You can then try implementing them one by one. For example, implement a parser first that can take a string and deduct the month from it. You can do it the naive way first, like:
if (month = "aug" or month == "august") ...
You can fix bugs and optimize later (like converting to lower-case first before comparing, putting all strings in a vector for easier lookup, etc.)
Your interface should probably look something like this:
class Month {
public:
explicit Month(int month);
explicit Month(std::string month);
int getMonth() const;
bool operator< (const Month month) const;
bool operator> (const Month month) const;
bool operator== (const Month month) const;
void print() const;
};
Edit: this forum messes up "<" and ">" in code blocks. The above are supposed to be "operator<" and "operator>".
Comment has been collapsed.
What should the functions "input()" and "output()" do, compared to "setMonth()" and "getMonth()"?
Comment has been collapsed.
You never know what it does, unless you wrote it yourself, but then the next guy won't know what it does. Is "==" really "==", or does it just compare two properties of a complex class?
Hacks (which includes operator overloading) are never a good thing when you work with others.
Comment has been collapsed.
How '==' is different from 'isEqual' than? In yours example the problem lies in team member, not in syntactic sugar. He and his code would be a problem in every langauage.
Operators, as any other method, must behave accordingly to their name. That's common sense.
Hacks are never good, not only in team projects. However I'll use and encourage using of anything that improves readability and maintainabilty of code (and that includes operator overloading). That means excluding things like '||' and '&&' operator overloads, as they might behave differently than built-in ones.
Comment has been collapsed.
It's different because you don't bake "IsEqual()" into the class and use it to compare two instances. You use an external FooComparer(Foo foo1, Foo foo2) for such tasks.
The only time you could/should overload an operator is if you're doing it for numbers; say, complex numbers, or BigInt stuff.
If you're doing numbers, foo1 + foo2 = foo2 + foo1 (in the traditional math way). If you're doing something else, like strings, that's not always going to be true. I mean, that's fairly obvious and it's a thing that Visual Basic got right ("&" for string concatenation), but I'm still saying :)
Comment has been collapsed.
And that external comparer is either friend or "foo1.isEqualTo(foo2);". I still see no difference. Comparer functions/methods are acceptable when there is more than one way of comparing. But then, 'operator==' will be made private.
Nope, can't agree. Operators should be overloaded every time they behave the same as for simple types. I can't even start to imagine how smart pointer would look like without overloads. And that's only top of iceberg...
I write quite a lot in C and asm, and if I only could, i would change C for C++. The improvement in readability of code is enormous.
Comment has been collapsed.
Primitive types already have defined, working operators. We're talking complex types here.
Let's use this simple example:
You have a Person (ID, Name, GovernmentID, DateAdded). GovernmentID is unique, but for reasons which aren't in your domain, that's not enforced on the DB level, and the only actual unique thing is the ID as identity bigint.
Someone or something fucks up. Your database now contains this:
Person1(143344323, "John Doe", "12345", "2005-03-16")
Person2(302930343, "John Doe", "12345", "2008-05-02")
Person1 as an object is NOT equal to Person2 as an object. "Person1 == Person2" does not hold true and should not hold true.
Assert(Person1 == Person2) is false. It needs to be false.
This is where you use a PersonComparer.
Assert(PersonComparer.AreEqual(Person1, Person2)) needs to be true.
Comment has been collapsed.
That's what i assumed from the beginnig. I'm stating, that for complex types, if operator uses the same logic as is used for primitive types, it should be overloaded and not replaced by external function. If you misunderstood me, than sorry for my lacking english.
Person class has two different equality relations. One on problem logic level (implemented in comparer) and other on implementation details level. This can be achieved only using friends or methods, so no difference here.
Assert(Person1==Person2): yes and yes. And it will work as intended, as long as you don't need specialized copy constructor for Person class. Or comparison of binary representation is somehow useful at this point. And that's what you will get without operator overloading.
Example:
Lets assume that Name is declared as char*, and in constructor is 'Name = new char[len];'. That would be standard (simplified) approach to Person class. Now we add Person3(143344323, "John Doe", "12345", "2005-03-16"). Person1==Person3 is false, and needs to be true...
Hmm, we strayed a bit from original subject of overloading evilness vs goodnes...
Comment has been collapsed.
When did this site became a homework help destination?
Comment has been collapsed.
2,284 Comments - Last post 5 minutes ago by Axelflox
16,599 Comments - Last post 21 minutes ago by HomieOhmie
534 Comments - Last post 39 minutes ago by Hawkingmeister
258 Comments - Last post 52 minutes ago by RobbyRatpoison
212 Comments - Last post 58 minutes ago by sensualshakti
39 Comments - Last post 1 hour ago by sensualshakti
168 Comments - Last post 1 hour ago by CalamityUP
190 Comments - Last post 2 minutes ago by XiaoLong
288 Comments - Last post 3 minutes ago by Myklex
2,843 Comments - Last post 11 minutes ago by ozo2003
40 Comments - Last post 12 minutes ago by AmikoNovich
51 Comments - Last post 37 minutes ago by Vasharal
29,260 Comments - Last post 59 minutes ago by MikeWithAnI
161 Comments - Last post 1 hour ago by TinaG
Hey guys just checking if anyone here is able to help me out. Figure where so many play games there are bound to be some coders here :).
Design and code a class that represents a single month of the year. It should have a single integer-typed data member to represent the month. Make sure to provide proper construction, access, and mutation for this member as well as input and output methods. In addition, methods for comparing two months chronologically for ordering would be useful as would a method to advance to the next month (note that January follows December and begins the cycle anew) or previous month. In fact, thinking about it, it might be good to be able to advance to a month an arbitrary number of whole months in the future or past: January '+' 5 = June; March '-' 5 = November.
Place your Month class in a library.
Write a test application (driver) for your class.
Oh, and the programmers who commissioned this class wanted to make sure it could handle not only integer-valued representation of a Month but also either whole-word names or three-letter abbreviations for the Months. That means that they should be able to construct a Month object from either an integer value or a string -- which could be either the whole month name or a 3-letter version there-of.
Similarly, they'd like to be able to retrieve the Month's information as either an integer value or a string (of either form), mutate the Month by any of these means, display by any of the three forms, and even allow their user to enter any of the three forms at the console.
But, to keep things as simple as possible, they'd not like a whole lot of different names to remember. Please use the following names for all functions that perform such tasks: set_month, get_month, input, output, advance, before, after, same. (There may be only one of some methods, but others may need at least two methods to offer all requested functionality.)
^^^^^^^^This is the descripton of the program I should be building, but I'm not even completely sure what it's asking me. Any help or suggestions on places to look to learn concepts would be grateful. I'm not trying to get someone to do it for me just need help.
Comment has been collapsed.