#include <iostream>
using namespace std;

class Moth {
public:
    Moth(double initial_position); // Constructor
    void move_to_light(double light_position); // Move moth towards a light source
    double get_position(); // Get current position of the moth
private:
    double position; // Position of the moth along the x-axis
};

Moth::Moth(double initial_position) {
    position = initial_position;
}

void Moth::move_to_light(double light_position) {
    position = (position + light_position) / 2.0; // Move halfway towards the light source
}

double Moth::get_position() {
    return position;
}

int main() {
    Moth moth(0.0); // Create a moth starting at position 0

    moth.move_to_light(10.0); // Move moth towards light source at position 10
    cout << "Moth's position after moving towards light source at position 10: " << moth.get_position() << endl;

    moth.move_to_light(16.0); // Move moth towards light source at position 16
    cout << "Moth's position after moving towards light source at position 5: " << moth.get_position() << endl;

    moth.move_to_light(2.0); // Move moth towards light source at position 2
    cout << "Moth's position after moving towards light source at position 5: " << moth.get_position() << endl;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: