// File: registerData.h
#include "registerData.h"
#include <vector>
#include <memory>
// Class definition for 'z'
class z {
protected:
HardwareSerial *serial;
registerData *data_Ptr;
int a = 0; // Common member variable for both 'x' and 'y', initialized to 0
int b = 0; // Common member variable for both 'x' and 'y', initialized to 0
public:
z(HardwareSerial *serial, registerData *data_Ptr) : serial(serial), data_Ptr(data_Ptr) {}
virtual ~z() {} // Add a virtual destructor
virtual void printToSerial() = 0;
virtual void printToData() = 0;
virtual int add() = 0;
virtual int subtract() = 0;
virtual void performOperation() = 0; // Common function to perform an operation
};
// Class definition for 'x' (derived from 'z')
class x : public z {
public:
x(HardwareSerial *serial, registerData *data_Ptr) : z(serial, data_Ptr) {
// Initialize values for 'x'
a = 5;
b = 3;
}
// Override functions from 'z'
void printToSerial() override {
serial->println("@1. Printing from class x");
Serial2.println("@2. Printing from class x");
}
void printToData() override {
serial->println("x:\t" + String(data_Ptr->analogint[200]));
}
// New functions for 'x'
int add() override {
return a + b;
}
int subtract() override {
return a - b;
}
void performOperation() override {
int result = a * b;
serial->println("x: Performing multiplication, result = " + String(result));
}
};
// Class definition for 'y' (derived from 'z')
class y : public z {
public:
y(HardwareSerial *serial, registerData *data_Ptr) : z(serial, data_Ptr) {
// Initialize values for 'y'
a = 10;
b = 4;
}
// Override functions from 'z'
void printToSerial() override {
serial->println("@1. Printing from class y");
Serial2.println("@2. Printing from class y");
}
void printToData() override {
serial->println("y:\t" + String(data_Ptr->analogint[200]));
}
// New functions for 'y'
int add() override {
return a + b;
}
int subtract() override {
return a - b;
}
void performOperation() override {
if (b != 0) {
int result = a / b;
serial->println("y: Performing division, result = " + String(result));
} else {
serial->println("y: Cannot perform division, denominator is zero.");
}
}
};
// Global variables
registerData data;
std::vector<std::unique_ptr<z>> objects;
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
// Dynamically allocate objects and add them to the vector
objects.push_back(std::make_unique<x>(&Serial, &data));
objects.push_back(std::make_unique<y>(&Serial, &data));
// Use the objects in the vector
for (const auto& obj : objects) {
auto& objRef = *obj; // Create a reference to the object
objRef.printToSerial();
objRef.performOperation();
}
// Call add and subtract functions and print the results
for (const auto& obj : objects) {
auto& objRef = *obj; // Create a reference to the object
int resultAdd = objRef.add();
int resultSubtract = objRef.subtract();
Serial.println("Result of add: " + String(resultAdd));
Serial.println("Result of subtract: " + String(resultSubtract));
}
}
void loop() {
// put your main code here, to run repeatedly
Serial.println("loop");
// No need to delete objects manually, unique_ptr handles the memory
delay(3000); // Add a delay if needed
}