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 8639775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:06:20+00:00 2026-06-12T11:06:20+00:00

Recently I have done an assignment, on overloading basic functionalists(+,-,conjugate…) of complex class using

  • 0

Recently I have done an assignment, on overloading basic functionalists(+,-,conjugate…) of complex class using templates. I had to sweat out a little to find out the proper return type, (casting to higher type), but at the end, I got it working perfectly.
This is how my class looks like –

template <typename T> class complex_t
{ 
private:
    T real;
    T imaginary;

public:

complex_t(T X, T Y)
  {
    real=X;
    imaginary=Y;
  }
}

But I didnt get full marks, because I didn’t implement the +=, -= etc. operators. Why is it important to implement those operators? Whether doing that really provide any particular benefits?
Can anyone share some thoughts?

Thanks in advance,

  • 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-06-12T11:06:21+00:00Added an answer on June 12, 2026 at 11:06 am

    += and friends work in-place, so you don’t have to return a new instance of your class. For complex numbers, that might not be much of a problem, but with larger structures the copying might be expensive.

    As an example, suppose that you’re implementing vectors, in the mathematical sense, supporting arbitrary lengths.

    class Vector
    {
        std::vector<double> elements;
    
      public:
        Vector operator+(double x)
        {
            // must return a copy!
            Vector v(*this);
            for (size_t i=0; i < elements.size(); i++)
                v.elements[i] += x;
            return v;
        }
    
        Vector &operator+=(double x)
        {
            // in-place operation
            for (size_t i=0; i < elements.size(); i++)
                elements[i] += x;
            return *this;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As of now I have done effort estimation based on experience and recently using
I have done Objective-C way back when, and have recently (i.e. just now) read
I have recently done a very simple highlighting with jQuery and a highlight plugin.
I'm using Visual Studio 2005 on a Windows XP SP3 machine and recently have
I have recently started using the Rackspace Cloudfiles CDN (Limelight), about which I have
Recently I have done this part of code. It does work, but select() works
I have recently done some website work for a charity. The site I have
I have recently install the C++ in order to re-learn the language (have done
I recently have done some refactoring to my project and I receive the following
Recently I've studied the backpropagation network and have done some manual exercise. After that,

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.