Project 11.14 Section 11 ⚡ Embedded Relevance: Core Value Semantics Operator Overloading Copying

11.14 Modeling Value Semantics without Pointer Overhead

Executive Summary: Exploring value types and operator overloading in simulation modeling.

💻 1. Annotated Source Code

#ifndef CROP_H
#define CROP_H

class Crop {
	public:
		Crop(int height, int yield, int droughtResistance);
		int getHeight() const;
		int getYield() const;
		int getDroughtResistance() const;
		int getScore() const;

		bool operator==(const Crop& other) const;
		bool operator!=(const Crop& other) const;
		bool operator<(const Crop& other) const;
		bool operator>(const Crop& other) const;
		bool operator<=(const Crop& other) const;
		bool operator>=(const Crop& other) const;
		Crop operator+(const Crop& other) const;
		void operator=(const Crop& other);

	private:
		int height;
		int yield;
		int droughtResistance;
};
#endif
#include "Crop.h"
#include <cstdlib>
#include <ctime>

using namespace std;

Crop::Crop(int height, int yield, int droughtResistance) : height(height), yield(yield), 
droughtResistance(droughtResistance) {
}

int Crop::getHeight() const {
	return height;
}

int Crop::getYield() const {
	return yield;
}

int Crop::getDroughtResistance() const {
	return droughtResistance;
}

int Crop::getScore() const {
	return (height * 2) + (yield * 3) + (droughtResistance * 4);
}

bool Crop::operator==(const Crop& other) const {
	return getScore() == other.getScore();
}

bool Crop::operator!=(const Crop& other) const {
	return !(*this == other);
}

bool Crop::operator<(const Crop& other) const {
	return getScore() < other.getScore();
}

bool Crop::operator>(const Crop& other) const {
	return getScore() > other.getScore();
}

bool Crop::operator<=(const Crop& other) const {
	return getScore() <= other.getScore();
}

bool Crop::operator>=(const Crop& other) const {
	return getScore() >= other.getScore();
}

Crop Crop::operator+(const Crop& other) const {
	srand(time(nullptr));
	auto randomize = [](int value) {
		int variation = (rand() % 3) - 1;   // -1, 0, or +1
			return value + variation;
	};

	int newHeight = randomize((height + other.height) / 2);
	int newYield = randomize((yield + other.yield) / 2);
	int newDrought = randomize((droughtResistance + other.droughtResistance) / 2);

	return Crop(newHeight, newYield, newDrought);
}

void Crop::operator=(const Crop& other) {
	height = other.height;
	yield = other.yield;
	droughtResistance = other.droughtResistance;
}
#include <iostream>
#include <vector>
#include <memory>
#include "Crop.h"
using namespace std;

void printCrop(const Crop& crop);

int main() {
	vector<unique_ptr<Crop>> crops;

	crops.push_back(make_unique<Crop>(30, 40, 20));
	crops.push_back(make_unique<Crop>(25, 50, 15));
	crops.push_back(make_unique<Crop>(32, 38, 22));

	Crop hybrid = *crops[0] + *crops[1];

	cout << "Crop 1:" << endl;
	printCrop(*crops[0]);

	cout << "Crop 2:" << endl;
	printCrop(*crops[1]);

	cout << "Crop 3:" << endl;
	printCrop(*crops[2]);

	cout << "Hybrid crop:" << endl;
	printCrop(hybrid);

	cout << boolalpha;
	cout << "Hybrid == Crop 3?" << (hybrid == *crops[2]) << endl;
	cout << "Hybrid > Crop 3?" << (hybrid > *crops[2]) << endl;

	return 0;
}

void printCrop(const Crop& crop) {
	cout << "Height: " << crop.getHeight()
		<< ", Yield: " << crop.getYield()
		<< ", Drought Resistance: " << crop.getDroughtResistance()
		<< ", Score: " << crop.getScore() << endl << endl;
}

📐 2. Architecture & UML Class Model

📐 Crop Hybridization Genetic Operator Architecture
+ Public - Private # Protected
<<class>> Crop Agricultural Model
-cropName : std::string
-yieldPerAcre : double
-diseaseResistance : double
+Crop(name: string, yield: double, resist: double)
+hybridizeWith(other: const Crop&) : Crop
+printCrop() : void const
+getYield() : double const
+getResistance() : double const

📚 3. Core C++ Concepts Deep-Dive

Value Semantics

Value types manage their own state cleanly without requiring pointer indirection.

⚡ 4. Embedded Systems & Hardware Reality

Stack-Based Physics & Control

Value objects are placed on the stack or in static arrays with zero dynamic allocation.

💡 5. Production-Ready Embedded Refactoring

💡 Production-Ready Refactor
struct Vector3D { float x, y, z; Vector3D operator+(const Vector3D& o) const { return {x+o.x, y+o.y, z+o.z}; } };

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. What is an advantage of value types in embedded systems?
A They reside on the stack or in static memory with zero heap allocation.
B They require heap allocation.
C They must use virtual functions.
D They cannot be copied.
Detailed Explanation: Value types avoid pointer dereferencing and heap allocation overhead.