Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:04:56+00:00 2026-05-30T18:04:56+00:00

I must write operator overloading += but I don’t know how to do it

  • 0

I must write operator overloading += but I don’t know how to do it correctly (I began to write it a the end of code it wasn’t correct so i delete all except you see).

#include <iostream>
using namespace std;

class dek
{
private:
    int *x,na4,kon,n,razmer;
public:
    dek(int m)
    {
        x=new int[m];
        n=m;
        na4=1;
        kon=0;
        razmer=0;
    }
    ~dek()
    {
        delete [] x;
    }
    void v_n(int a)
    {
        if(razmer!=n)
        {
            na4--;
            if(na4<0)na4=n-1;
            x[na4]=a;
            razmer++;
        }
        else cout<<"dek polon\n";
    }

    void v_k(int b)
    {
        if(razmer!=n)
        {
            kon++;
            if(kon>n-1)kon=0;
            x[kon]=b;
            razmer++;
        }
        else cout<<"dek polon\n";
    }

    int size()
    {
        return razmer;
    }

    void u_n()
    {
        if(razmer!=0)
        {
            na4++;
            if(na4>n-1)na4=0;
            razmer--;
        }
        else cout<<"dek pust\n";
    }

    void u_k()
    {
        if(razmer!=0)
        {
            kon--;
            if(kon<0)kon=n-1;
            razmer--;
        }
        else cout<<"dek pust\n";
    }

    void pe4at()
    {
        int i=na4;
        if(razmer!=0)
        {
            while(1)
            {
                cout << x[i] << "  ";
                if(i==kon)break;
                i++;
                if(i>n-1)i=0;
            }
            cout << "\n";
        }
    }

    dek& operator = (dek const& b)
    {
        if(&b!=this)
        {
            delete [] x;
            x=new int[b.n];
            n=b.n;
            razmer=b.razmer;
            na4=b.na4;
            kon=b.kon;
            if(razmer!=0)
            {

                int i=na4;
                while(1)
                {
                    x[i]=b.x[i];
                    if(i==kon)break;

                    i++;
                    if(i>n-1)i=0;

                }
            }
        }
        return *this;
    }

    dek const operator +(dek const& b)const
    {
        dek s(n+b.n);
        s.n=n+b.n;
        s.razmer=razmer+b.razmer;
        s.na4=0;
        s.kon=s.razmer-1;

        if(razmer!=0)
        {
            int j=0,i=na4;

            while(1)
            {
                s.x[j]=x[i];
                if(i==kon)break;
                i++;
                if(i>n-1)i=0;
                j++;
                if(j>s.n-1)j=0;
            }
        }

        if(b.razmer!=0)
        {
            int j=razmer,i=b.na4;

            while(1)
            {
                s.x[j]=b.x[i];
                if(i==b.kon)break;
                i++;
                if(i>b.n-1)i=0;
                j++;
                if(j>s.n-1)j=0;
            }
        }
        return s;
    }

    dek operator +=(dek const& b)
    {

    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:04:57+00:00Added an answer on May 30, 2026 at 6:04 pm

    Well, the results of a += b; should be equivalent to a = a + b;;
    since you have already defined an operator+, you know what these
    semantics are. Once common practice is to define operator+= first,
    and then implement operator+ (usually as a free function) in terms of
    +=:

    MyClass
    operator+( MyClass const& lhs, MyClass const& rhs )
    {
        MyClass results( lhs );
        results += rhs;
        return results;
    }
    

    You then define operator+= to operate directly on the class members:

    MyClass&
    MyClass::operator+=( MyClass const& other )
    {
        n += other.n;
        razmer += other.razmer;
        //  ...
        return *this;
    }
    

    (Although there are good reasons for making it a non-member,
    traditionally, operator+= is a member. Probably because operator=
    is required to be a member.)

    Also, traditionally, operator+= returns a reference, because this
    most resembles the behavior of the operator on built-in types.

    Finally, on a completely different issue: you’re missing a copy
    constructor (which in your case means a double deletion if you do copy),
    and your operator= is broken (think of what will happen if the x =
    new int[b.n];
    fails and throws an std::bad_alloc). The classical
    solution for this would be to implement deep copy in the copy
    constructor (using more or less the same logic you use in setting the
    variables in your assignment operator), and in the assignment operator,
    to construct a copy, then swap the elements. This isn’t strictly
    necessary, but whatever you do, you must do the new (and anything else
    which may fail) before changing the values in the object being assign.
    (If you do this, the test for self assignment is useless; the need for a
    test for assignment is usually a signal that the assignment operator is
    broken.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i must write (at start of app) and delete is content (at the end
I know the = operator can't be overloaded, but there must be a way
I have this snippet of the code and I must write appropriate code with
I'm confused about friend operator overloading. It has no problem if I write the
I'm trying to write a class that overloads the insertion operator but in my
I must write array of struct Data to hard disk: struct Data { char
What I must write in my .emacs file so that the *scratch* buffer is
I need to write a generic class where the type parameter must be something
i write row click handler: $('#mTable tr').unbind().live('click', function() { //specialFunction() }); specialFunction must not
I'm kinda stuck with something which must be appalingly simple. I'm trying to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.