CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    11

    Studying operator overloading

    I'm studying operator overloading and can not understand how that is identified using the + + operator pre-set or post-set.

    ------ CODE --------------------------------------------------

    #include <iostream>
    using namespace std;

    class ponto
    {
    private:
    int x,y;
    public:
    ponto(int x1=0, int y1=0) {x=x1;y=y1;} // Constructor

    ponto operator ++() //Function operator PRE-FIXED
    {
    ++x;++y;
    return ponto(x,y);
    }

    ponto operator ++(int) //Function operator POS-FIXED
    {
    ++x;++y;
    return ponto(x-1,y-1);
    }

    void printpt() const
    {
    cout <<'('<< x <<','<<y<<')';
    }
    };

    void main()
    {
    ponto p1,p2(2,3),p3;
    cout << "\n p1 = "; p1.printpt();
    cout << "\n p2 = "; p2.printpt();
    cout << "\n++p1 = ";
    (++p1).printpt(); //increases after use
    cout << "\np2++ = ";
    (p2++).printpt(); //uses after incrases
    cout << "\n p2 = "; p2.printpt();
    p3 = p1++; //assigns after increases
    cout << "\n p3 = "; p3.printpt();
    cout << "\n p1 = "; p1.printpt();
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Studying operator overloading

    Quote Originally Posted by Alex_Brazil View Post
    I'm studying operator overloading and can not understand how that is identified using the + + operator pre-set or post-set.
    First, use proper code tags when posting code. There is no need for "Indent" tags if you just posted using code tags:


    [code]
    Your code goes here
    [/code]

    Example:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
    }
    See how the code looks when I post as compared to when you post code?

    Second, if you're studying operator overloading, why are you using such a confusing and wrong piece of code to study this? Please see the FAQ on overloading ++ here, and compare the simple example and explanation of this operator and how it is to be overloaded to what you're doing.

    http://www.parashift.com/c++-faq/inc...erloading.html

    The first glaring issue is that the prefix ++ should be returning a reference to the current object, and your code doesn't do that.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2012
    Posts
    11

    Re: Studying operator overloading

    I am grateful for the support I have received from members of this forum and apologize for the mistakes of posting, no formatting required.

    I created a new code to study the overhead of a binary operator, where the operator +, but the compiler returns the following error: "main.obj: -1: error: LNK2019: unresolved external symbol" public: __ thiscall sale sale :: (void) "(? 0venda @ @ QAE @ XZ) referenced in function _main".

    I do not understand what is generating the error. Can anyone help me?

    Follow the code:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class venda
    {
        private:
            int npecas;
            float precos;
         public:
            venda();
            venda(int np, float p) {npecas = np;precos = p;}
            venda operator + (venda v) const;
            void getvenda()
            {
                cout << "\nInsira o nr de pecas: "; cin >> npecas;
                cout << "\nInsira o preco: "; cin >> precos;
            }
            void printvenda() const;
    };
    
    //int venda::npecas;
    //float venda::precos;
    
    venda venda::operator +(venda v) const
    {
        int pec = npecas + v.npecas;
        float pre = precos + v.precos;
        return venda(pec,pre);
    }
    void venda::printvenda() const
    {
        cout << setiosflags(ios::fixed)
             << setiosflags(ios::showpoint)
             << setprecision(2)
             << setw(10) <<npecas;
        cout << setw(10) << precos << '\n';
    }
    
    void main()
    {
        venda A(58,34.53), B, C(30,60.3), T, Total;
        B.getvenda();
        T = A + B;
        Total= A + B + C;
    
        cout << "Venda: A .............."; A.printvenda();
        cout << "Venda: B .............."; B.printvenda();
        cout << "Venda: A + B .........."; T.printvenda();
        cout << "Venda: Totais A+B+C...."; Total.printvenda();
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Studying operator overloading

    This line.
    venda();

    You declared a constructor but didn't give it a body.

  5. #5
    Join Date
    Oct 2012
    Posts
    11

    Re: Studying operator overloading

    Thank you. The problem was solved.

  6. #6
    Join Date
    Oct 2012
    Posts
    11

    Re: Studying operator overloading

    Operator Overloading - object returns wrong value

    Another example I created for my study, but the objects s3, s4 and s5 are returning wrong values​​. Someone knows you explain why?

    Code:
    #include <iostream>
    #include <string>
    
    const int MAX = 80;
    using namespace std;
    
    class string2
    {
        private:
            char str[MAX];
        public:
            string2() {str[0]='\0';}
            string2(char s[]) {strcpy(str,s);}
    
            string2 operator +=(string2 s) const //Concatena
            {
                string2 temp;
                if(strlen(str)+strlen(s.str) < MAX)
                {
                    strcpy(temp.str,str);
                    strcat(temp.str,s.str);
                }
                else
    
                    cout << "\nMemor;ia Cheia!";
        
                return temp;
             }
    
                string2 operator +(string2 s) const // Concatena
                {
                    string2 temp;
                    temp +=s.str;
                    return temp;
                }
    
                void print() const {cout << str;}
    };
    
    int main()
    {
        string2 s1("Feliz Aniversario! "),
                s2("Denise."),
                s3("Bom Dia! "), s4, s5;
        cout << "\ns1 = "; s1.print();
        cout << "\ns2 = "; s2.print();
    
        s3 += s2;
        s4 = s1 + s2;
        s5 = s1 + s1;
    
        cout << "\ns3 = "; s3.print();
        cout << "\ns4 = "; s4.print();
        cout << "\ns5 = "; s5.print();
    }

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Studying operator overloading

    Quote Originally Posted by Alex_Brazil View Post
    Operator Overloading - object returns wrong value

    Another example I created for my study,
    You need to study a bit more before creating your own examples:
    Code:
           string2 operator +=(string2 s) const //Concatena
    Why are you passing string2 objects by value? You should be passing them by const reference, not by value.
    Code:
    string2 operator +=(string2 s) const //Concatena
    Why is operator += returning a new object? It should be returning a reference to the existing object. That is the whole purpose of +=. It takes the existing object, adds something to it, and returns the existing object.

    Everything I pointed out is covered in good books. Are you learning from real books? If you're just throwing things together without any guidance, that is not the way you learn C++. You're supposed to be using good books (and tutorials) on the language, i.e. see how the experts in the language do things. Putting stuff together yourself just leads to mistakes and a misunderstanding of how to use C++ properly.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 17th, 2012 at 12:57 AM.

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

Featured