Could u suggest a good example for operator overloading other than the fraction or the inches problem.
Could u suggest a good example for operator overloading other than the fraction or the inches problem.
wut do u mean
Suggest an example or provide one? How about this?
Output:Code:#include <iostream> #include <string> using namespace std; typedef enum gender{male,female}; class person { string fname, mname, lname; unsigned age; gender sex; public: person(string first=string(""),string middle=string(""),string last=string(""), unsigned yrs=30,gender gen=male) : fname(first), mname(middle), lname(last), age(yrs), sex(gen) {} friend ostream& operator<<(ostream&,const person&); }; ostream& operator<<(ostream& os,const person& pers) { return os << "Name: " << pers.fname << ' ' << pers.mname << ' ' << pers.lname << '\n' << "Age: " << pers.age << '\n' << "Sex: " << (pers.sex == male ? "Male" : "Female"); } int main() { person homer("Homer","Jay","Simpson",45,male); cout << homer << endl; return 0; }
Code:Name: Homer Jay Simpson Age: 45 Sex: Male
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
well, i know this simple example:
Code:abstract class text { abstract void drawText(Shape x); abstract void drawText(Shape x,Color c); //this one overloads the previous one }
That is function overloading, not operator overloading.
That's function overloading....and 'abstract' is not a C++ keyword. This looks more like Java to me.Originally Posted by wakish
yeah, it's Java...but the principle and logic is the same!
and yeah, sry it is function overloading, i read that in a haste..i just noticed it's about operator..
You've given me a great example(person) .Thanx a ton.