user42225: I wish to know if there is a way to use a single property Serial0 to hold a HardwareSerial or a SoftwareSerial or other class instances supporting basic methods such as available(), read() and write(), in order to make a CustomSerial Class (polymorphism?).
class CustomSerial{
    <GenericSerial??> Serial0;
    char c;
    void read(void){
        c=Serial0.read();
    }
};

The calls should be:
CustomSerial SerialCus;
int n=SerialCus.available();
char c=SerialCus.read();
SerialCus.write(c);

What should I use as GenericSerial class, or what should be the proper use? I tried the Stream class (as the answer pointed), but for some reason the functionality is not preserved (it writes unknown characters). Should indeed Stream be the generic class I need for this?

user37622: A base type for Arduino hardware serial classes, SoftwareSerial and other software serial classes and some other Arduino classes is Stream (reference). Stream.h on GitHub

class CustomSerial {

private:
  Stream &stream;

public:
  CustomSerial(Stream &_stream) : stream(_stream) {}

  int read() {
    return stream.read();
  }

  int available() {
    return stream.available();
  }

  int peek() {
    return stream.peek();
  }

  void write(byte b) {
    stream.write(b);
  }

};

SoftwareSerial Serial0(10, 11);
CustomSerial SerialCus(Serial0);

void setup() {
}

void loop(){
  if (SerialCus.available()) {
    char c = SerialCus.read();
    SerialCus.write(c);
  }
}


42225: <p>I wish to know if there is a way to use a single property <code>Serial0</code> to hold a <code>HardwareSerial</code> or a <code>SoftwareSerial</code> or other class instances supporting basic methods such as <code>available()</code>, <code>read()</code> and <code>write()</code>, in order to make a <code>CustomSerial</code> Class (<em>polymorphism</em>?).</p>

<pre><code>class CustomSerial{
    &lt;GenericSerial??&gt; Serial0;
    char c;
    void read(void){
        c=Serial0.read();
    }
};
</code></pre>

<p>The calls should be:</p>

<pre><code>CustomSerial SerialCus;
int n=SerialCus.available();
char c=SerialCus.read();
SerialCus.write(c);
</code></pre>

<p>What should I use as <code>GenericSerial</code> class, or what should be the proper use? I tried the <code>Stream</code> class (as the answer pointed), but for some reason the functionality is not preserved (it writes unknown characters). Should indeed <code>Stream</code> be the generic class I need for this?</p>
 37622: <p>A base type for Arduino hardware serial classes, SoftwareSerial and other software serial classes and some other Arduino classes is <a href="https://www.arduino.cc/reference/en/language/functions/communication/stream/" rel="nofollow noreferrer">Stream (reference)</a>. <a href="https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Stream.h" rel="nofollow noreferrer">Stream.h on GitHub</a></p>

<p><a href="https://i.stack.imgur.com/3OV45.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3OV45.png" alt="enter image description here"></a></p>

<pre><code>class CustomSerial {

private:
  Stream &amp;stream;

public:
  CustomSerial(Stream &amp;_stream) : stream(_stream) {}

  int read() {
    return stream.read();
  }

  int available() {
    return stream.available();
  }

  int peek() {
    return stream.peek();
  }

  void write(byte b) {
    stream.write(b);
  }

};

SoftwareSerial Serial0(10, 11);
CustomSerial SerialCus(Serial0);

void setup() {
}

void loop(){
  if (SerialCus.available()) {
    char c = SerialCus.read();
    SerialCus.write(c);
  }
}
</code></pre>
