Q: What happens if I add a member to his class called "date" that has a type string ...
A. You will get another class. You had class Time and now you have class DateTime. They are different classes with different symantic.
You can include class Time as a member of class DateTime and use all operators of class Time in the realization of class DateTime. You need to change nothing in class Time itself.
A: Anywhere my code uses:
Timp object0, object1;
cout << (object0 + object1);
Or similar, needs to be rewritten right? Now user must change class code and all code that used + operator...
No, your code need not to be rewritten You simply need to write interface for class DateTime. One more Time and DateTime are different classes with different symantic.
If you are speaking about that you had class Date and added to it some member of type string then there is no difference whether you will rewrite operator + that allow to display the new member or you will rewrite some your function with strange name and will use instead of crystal clear statement
1 2 3
|
Date birthDate;
std::cout << birthDate << std::endl;
|
some much less clear code
1 2 3
|
Date birthDate;
Display( std::cout, birthDate );
|
The first code snip is well-known for everybody. You are using
std::string s;
std::cout << s;
the same way as
Date birthDate;
std:;cout << birthDate;
@pata
If I use member function to modify members like combine, I dont change anything, class works as I wrote it and I am free to implement the member in the class however I want. So which would you rather do in a large class?
|
There is no difference whether to use some function or an operator. In either case for the user interface is not changed. And in both cases you have to change realization of the function or of the operator. By the way operator << usually does not change members of the class for which it is written.
Moreover you always forgot that you need operation that does not change the object itself. I already showed that operator += and operator + have different symantic.
And I do not understand why you are not free to implement the operator <<. As I said operator << is a part of the interface of a class.