Small question , can you help me ??

This is a discussion on Small question , can you help me ?? within the C++ Programming forums, part of the General Programming Boards category; Hi for ever I am asking about : How to addition two object with different type and then store the ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Smile Small question , can you help me ??

    Hi for ever

    I am asking about :

    How to addition two object with different type and then store the result in the object have same type of one of these two object
    by using operator overloading ?

    for example :

    we have class called ThreeD

    Code:
    ThreeD operator +(ThreeD op1, ThreeD op2) 
      { 
        ThreeD result = new ThreeD(); 
     
        /* This adds together the coordinates of the two points 
           and returns the result. */ 
        result.x = op1.x + op2.x;  
        result.y = op1.y + op2.y;  
        result.z = op1.z + op2.z;  
     
        return result; 
      }
    in these example we add two object of same type in the same class

    What about if I have two classes
    and I wanted to add object from class 1 to object from class 2
    and then store the result in objcet of type of class 1


    Thats all ,
    thanks to reed my question ,
    I wait the answer
    Last edited by Salem; 12-19-2008 at 09:57 AM. Reason: Pointless font abuse removed

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You would just define operator+ to take the objects you want to add together:
    Code:
    ReturnType operator+(FirstType left_hand, SecondType right_hand)
    {
        //whatever it means to add together
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It might also be possible if FirstType has a non-explicit constructor that accepts a SecondType.

    Code:
    class First
    {
        int m;
        public:
        First(int n): m(n) {}
        int get() const { return m; }
    };
    
    class Second
    {
        int m;
        public:
        Second(int n): m(n) {}
        Second(First first): m(first.get()) {}  //First can be inplicitly converted into Second
    
        int get() const { return m; }
    };
    
    Second operator+(Second lhv, Second rhv)
    {
        return Second(lhv.get() + rhv.get());
    }
    
    int main()
    {
        Second s1(10), s2(5);
        First f(5);
        s2 = s1 + f;
        s1 = f + s2;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    but :

    if the code is written as :

    Code:
    FirstType operator+(FirstType left_hand, SecondType right_hand)
    {
    
        FirstType result;
       result=left_hand+right_hand;
       return result ;
    
    }
    this what I need

    but it make error

    How can I make it true ??

    tabstop & anon
    thanks very much
    Last edited by Salem; 12-19-2008 at 10:54 AM. Reason: F-ugly fonts and colours, rendering code tags useless

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Last chance to stop colouring your posts, like this is some kindergarden forum using wax crayons.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The error in this code seems to be that you are hoping to implement operator+ in terms of the same operator+.

    If that worked here's my first MMORPG

    Code:
    void Mmorgp()
    {
        Mmorgp();
    }
    
    int main()
    {
        Mmorgp(); 
        //profit
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well. You need to figure out what left_hand+right_hand means -- right now, you're just defining + by saying "if you see +, what you really need to do is +", which means we look + up again, and so on. You have to actually write the code inside the function.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Last chance to stop colouring your posts, like this is some kindergarden forum using wax crayons.
    oh , soory .!!
    I dont know that it is dissenting !
    I will never do it

    ..

    tabstop & anon
    thanks very very very much
    I will try the ways .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Hi all, I have a small question...
    By Pandora in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2003, 05:21 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. A small Question
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 08:08 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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