I'm writing a simple complex number class in c++ and we're supposed to overload the four arithmetic operators to work on complex+-/*complex, complex+-*/double, and double+-*/complex.

This is easy enough, but I wanted to try and make it a little more generic and instead of coding for doubles, extending the functionality to any number.

Is there a facility in c++ for doing this?

Recommended Answers

All 2 Replies

Templates perhaps?

Nah, because then you'd have to declare a complex object that will work with only one of the numerical datatypes. The complex number isn't supposed to be generic, the arithmetic is.

I had to make a java equivalent as well, and for it I performed all operations on the abstract Number class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.