Results 1 to 13 of 13

Thread: Polymorphism?

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Question

    I'm a newbie here, when it comes to C++, but i know how to make functions that overloads each other. I've heard you can overload operators too for isntance
    Code:
    Cls1 Obj1;
    Cls2 Obj2;
    
    Obj1 = Obj2;
    Obj1 = Obj1 + Obj2 / Obj1 * Obj1;
    Obj1 += Obj2;
    how do i achieve this?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Maybe you can use this:
    Code:
    Listing 10.8. Overloading operator++.
    
    1:     // Listing 10.8
    2:     // The Counter class
    3:
    4:     typedef unsigned short  USHORT;
    5:     #include <iostream.h>
    6:
    7:     class Counter
    8:     {
    9:        public:
    10:          Counter();
    11:          ~Counter(){}
    12:          USHORT GetItsVal()const { return itsVal; }
    13:          void SetItsVal(USHORT x) {itsVal = x; }
    14:          void Increment() { ++itsVal; }
    15:          void operator++ () { ++itsVal; }
    16:
    17:       private:
    18:          USHORT itsVal;
    19:
    20:    };
    21:
    22:    Counter::Counter():
    23:    itsVal(0)
    24:    {};
    25:
    26:    int main()
    27:    {
    28:       Counter i;
    29:       cout << "The value of i is " << i.GetItsVal() << endl;
    30:       i.Increment();
    31:       cout << "The value of i is " << i.GetItsVal() << endl;
    32:       ++i;
    33:       cout << "The value of i is " << i.GetItsVal() << endl;
    34:     return 0;
    35: }
    Output: The value of i is 0
    The value of i is 1
    The value of i is 2
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Thanks, i appreciate that much Can you explain how i would go doing these operations i mentioned?

    Code:
    15:          void operator++ () { ++itsVal; }
    so if this works for Obj1++ then how do i get Obj1 = Obj1 + Obj2; to work?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    An Example, What my book says....

    #include <iostream.h>

    class number
    {
    public:
    int n;
    number() {n = 1;}
    number operator+ (number);
    };

    number number:perator+ (number N)
    {
    cout << "\noperator+() function is working ";
    number T;
    T.n = N + N.n;
    return T;
    }

    void main()
    {
    number x,y,z;

    Z = X.operator+(Y) //Explicit
    cout << Z.n; //2

    Z = X + Y; //Implicit
    cout << Z.n; //2

    Z = Z + X + Y; //Chain of calls
    cout << Z.n; //4
    }

  5. #5
    Guest

    Sorry for the post, but...

    Originally posted by kedaman
    I'm a newbie here, when it comes to C++
    Join the party kedaman.
    For C++, we have to look to parksie, vlatko, and a few others .

    I wish C++ coding was as easy as VB.
    I'm still learning..still reading the tutorials at http:///www.cprogramming.com .

  6. #6

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Thanks, Psy, very nice of you helping me thanks for the tutorial Matt i'll have a look at it. Maybe i should add parksie to my ICQ list?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is a sample of how to overload the + operator.
    Code:
    1:     // Listing 10.14
    2:     //Overload operator plus (+)
    3:
    4:     typedef unsigned short  USHORT;
    5:     #include <iostream.h>
    6:
    7:     class Counter
    8:     {
    9:     public:
    10:       Counter();
    11:       Counter(USHORT initialValue);
    12:       ~Counter(){}
    13:       USHORT GetItsVal()const { return itsVal; }
    14:       void SetItsVal(USHORT x) {itsVal = x; }
    15:       Counter operator+ (const Counter &);
    16:    private:
    17:       USHORT itsVal;
    18:    };
    19:
    20:    Counter::Counter(USHORT initialValue):
    21:    itsVal(initialValue)
    22:    {}
    23:
    24:    Counter::Counter():
    25:    itsVal(0)
    26:    {}
    27:
    28:    Counter Counter::operator+ (const Counter & rhs)
    29:    {
    30:       return Counter(itsVal + rhs.GetItsVal());
    31:    }
    32:
    33:    int main()
    34:    {
    35:       Counter varOne(2), varTwo(4), varThree;
    36:       varThree = varOne + varTwo;
    37:       cout << "varOne: " << varOne.GetItsVal()<< endl;
    38:       cout << "varTwo: " << varTwo.GetItsVal() << endl;
    39:       cout << "varThree: " << varThree.GetItsVal() << endl;
    40:
    41:     return 0;
    42: }
    
    Output: varOne: 2
    varTwo: 4
    varThree: 6
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Maybe i should add parksie to my ICQ list?
    If you want. I'm 91747330
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well good, now i have someone who i can ask anything about
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well...C++ related anyway
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well, here's something related, about inheritance
    Is it possible for a class to inherit two other classes?
    I was thinking about letting a windowcomponent inhering both visualcomponent and a scriptcomponent.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Most things are possible. That of course depends on whether your brain explodes while debugging it .
    Code:
    class MyClass : public WindowClass, public ScriptClass {
        MyClass() { }
        virtual ~MyClass() { }
    
        int WindowClassFunction() { } // Overload from WindowClass
    
        int ScriptClassFunction() { } // Overload from ScriptClass
    };
    You also inherit any data members. Although I think you get an error (maybe a warning) if two classes you inherit from have two identical members.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Thanks parksie, that was i was hoping for
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width