0

I have written a class for a curve in the complex plane in c++. It looks like this,

#ifndef CURVE_H
#define CURVE_H

#include <iostream>

#include "complex_numbers.h"

class Curve {
    double min;
    double max;
    Complex(*f)(double);
public:
    Curve(Complex(*g)(double), double min_t, double max_t) {
        f = g;
        min = min_t;
        max = max_t;
        if (min > max) {
            std::cout << "Domain error." << std::endl;
        }
    }

    double get_min();

    double get_max();

    Complex at(double t);
};

#endif

This also relies on another class I have written (Complex) which is just a simple class for the complex numbers.

If a curve is defined on the interval [a,b], I don't want a curve with b<a to be able to be defined. Can I prevent this in my constructor somehow? As of right now I'm printing an error message but I can still define a curve (f,1,0) for example. Can I cancel the initialization in some way, if min_t > max_t?

9
  • 1
    throw exception? Commented Oct 27, 2021 at 10:34
  • 1
    Off-topic: There's already std::complex in header <complex> – you might consider if it matches your needs as well and possibly replace your custom class (no need to re-invent the wheel). Commented Oct 27, 2021 at 10:35
  • @spinosarus123 Just swap them. Commented Oct 27, 2021 at 10:35
  • Off-topic 2: You should get used to implementing the constructor's initialiser list (not to be confused with std::initializer_list): Curve(/*...*/) : f(g), min(min_t), max(max_t) { /*still can throw here*/ } – this prefers direct initialisation by value over default initialisation + assignment, which can be especially relevant with complex types. Additionally some types only can be initialised that way (references, const members, non-default-constructible types, ...). Commented Oct 27, 2021 at 10:42
  • @VladfromMoscow Hm... Thought about that as well, discarded, though, as it abets erroneous usage of the class. Passing min as 12 and receiving 10 from getter afterwards maybe is a bit odd, isn't it? Commented Oct 27, 2021 at 10:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.