Define Serial in sketch and use it in a library

Good afternoon.
I'm starting make an own libraries, because my projects begins be confused. But I don't understand them fully.
I need define right Serial port in a ino project and then use it in my library for print log messages.
Could You help me how to do it?

you can pass Serial, Serial1, Serial2 or Serial3 as an argument to a constructor (?)

You can pass a pointer to the Stream class, then the code will work with both hardware serial ports and software serial.

So always over class, defined in header file ? And after include header will be accessable in sketch. And cpp take it from header too. Undersand I good ?

So i'm not a winner. :roll_eyes:
As my first trial work with classes it ends poorly.
I think, that I must make an object with parent class Hardwareaserial to have the inheritance as a Serial objects and then define this object as Serial0, 1...
Think I good ?

Could You show me any example ?

A poorly written example:

sketch file:

#include "testclass.h"

TestClass tc;

void setup() {

void loop() {

testclass.h file:


#include <Arduino.h>

class TestClass
    void begin(Stream& s) {
      stream = &s;

    size_t prnt(const char* text){
      return stream->print(text);

    Stream* stream;


#endif //TESTCLASS_H