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

  • SEARCH
  • Home
  • 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 6578155

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:44:04+00:00 2026-05-25T15:44:04+00:00

I understand how polymorphism and inheritance works in C++, but my problem is: how

  • 0

I understand how polymorphism and inheritance works in C++, but my problem is: how do you make operators polymorphic for the following specific example?

Say I have a Foo class and two Foo instances, fooA and fooB. I want to redefine the plus sign operator so that “fooA + fooB;” does something specific to Foo instances (whatever that may be). How would the function prototype look? It’s confusing me because I’m used to functions starting with a letter… Any help would be greatly appreciated.

By the way, this isn’t a homework question — more like a wonderment (I was thinking about polymorphism in Ruby).

  • 0 0 Answers
  • 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-25T15:44:05+00:00Added an answer on May 25, 2026 at 3:44 pm

    Example from http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr318.htm:

    #include <iostream>
    using namespace std;
    
    class complx 
    {
          double real, imag;
    public:
          complx(double real = 0., double imag = 0.); // constructor
          complx operator+(const complx&) const;      // operator+()
    };
    
    // define constructor
    complx::complx(double r, double i)
    {
          real = r; imag = i;
    }
    
    // define overloaded + (plus) operator
    complx complx::operator+(const complx& c) const
    {
          complx result;
          result.real = this->real + c.real;
          result.imag = this->imag + c.imag;
          return result;
    }
    
    int main()
    {
          complx x(4,4);
          complx y(6,6);
          complx z = x + y; // calls complx::operator+()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand the concepts of polymorphism and overloading. I have the following
I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc.
can you give me a simple example of inheritance and polymorphism, so it could
I am trying to understand Java's polymorphism, and I have one question about downcasting
I am a competent C++ developer. I understand and use polymorphism, templates, the STL,
How can polymorphism be described in an easy-to-understand way? We can find a lot
I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand that some countries have laws regarding website accessibility. In general, what are
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the

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.