good example

This is a discussion on good example within the C++ Programming forums, part of the General Programming Boards category; Could u suggest a good example for operator overloading other than the fraction or the inches problem....

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    12

    good example

    Could u suggest a good example for operator overloading other than the fraction or the inches problem.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    wut do u mean

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Suggest an example or provide one? How about this?

    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;
    }
    Output:
    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

  4. #4
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    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
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That is function overloading, not operator overloading.

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by wakish
    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's function overloading....and 'abstract' is not a C++ keyword. This looks more like Java to me.

  7. #7
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    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..

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    12
    You've given me a great example(person) .Thanx a ton.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  2. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 04:38 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 03:23 PM
  4. what is good for gaphics in dos
    By datainjector in forum Game Programming
    Replies: 2
    Last Post: 07-15-2002, 03:48 PM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21