example.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <cmath>

/* Questions (get answers from GPT4o)
 * - why use variable_? Answer: variables not meant to be used
 * - how does Vector work? 
 * - use of public/private  (eventually protected)
 * - initializer lists
 */
 

// Vector class to represent both 2D and 3D vectors
class Vector {
public:
    Vector(std::vector<double> components) : components_(components) {}

    Vector operator+(const Vector& other) const {
        if (components_.size() != other.components_.size()) {
            throw std::runtime_error("Vector dimensions do not match");
        }
        std::vector<double> result(components_.size());
        for (size_t i = 0; i < components_.size(); ++i) {
            result[i] = components_[i] + other.components_[i];
        }
        return Vector(result);
    }

    // Override ouput stream operator. 
    friend std::ostream& operator<<(std::ostream& os, const Vector& v) {
        os << "(";
        for (size_t i = 0; i < v.components_.size(); ++i) {
            os << v.components_[i];
            if (i < v.components_.size() - 1) os << ", ";
        }
        os << ")";
        return os;
    }

    std::vector<double> components_;
};

// Function overloading for vector norm
double norm(const Vector& v) {
    if (v.components_.size() == 2) {
        return std::sqrt(v.components_[0] * v.components_[0] + v.components_[1] * v.components_[1]);
    } else if (v.components_.size() == 3) {
        return std::sqrt(v.components_[0] * v.components_[0] + v.components_[1] * v.components_[1] + v.components_[2] * v.components_[2]);
    } else {
        throw std::runtime_error("Norm function only supports 2D and 3D vectors");
    }
}

// Norm function for 2D vectors
double norm(double x, double y) {
    return std::sqrt(x * x + y * y);
}

// Norm function for 3D vectors
double norm(double x, double y, double z) {
    return std::sqrt(x * x + y * y + z * z);
}

// Norm function for an array (generalized for any dimension)
double norm(const double* components, size_t size) {
    double sum = 0.0;
    for (size_t i = 0; i < size; ++i) {
        sum += components[i] * components[i];
    }
    return std::sqrt(sum);
}

class Particle {
public:
    Particle(double mass, const Vector& position, const Vector& velocity)
        : mass_(mass), position_(position), velocity_(velocity) {
        std::cout << "Particle created at position " << position_ << std::endl;
    }

    ~Particle() {
        std::cout << "Particle destroyed at position " << position_ << std::endl;
    }

    void updatePosition(double time) {
        std::vector<double> displacement(velocity_.components_.size());
        for (size_t i = 0; i < displacement.size(); ++i) {
            displacement[i] = velocity_.components_[i] * time;
        }
        position_ = position_ + Vector(displacement);
    }

    void printState() const {
        std::cout << "Particle - Mass: " << mass_ << ", Position: " << position_
                  << ", Velocity: " << velocity_ << ", Speed: " << norm(velocity_) << std::endl;
    }

private:
    double mass_;
    Vector position_;
    Vector velocity_;
};

int main() {
    // Create a 2D particle
    Particle particle2D(5.0, Vector({0.0, 0.0}), Vector({1.0, 2.0}));
    std::cout << "Initial state of 2D particle:" << std::endl;
    particle2D.printState();

    particle2D.updatePosition(2.0);
    std::cout << "2D particle after 2 seconds:" << std::endl;
    particle2D.printState();

    // Create a 3D particle
    Particle particle3D(10.0, Vector({0.0, 0.0, 0.0}), Vector({1.0, 2.0, 3.0}));
    std::cout << "\nInitial state of 3D particle:" << std::endl;
    particle3D.printState();

    particle3D.updatePosition(2.0);  // ERROR
    std::cout << "3D particle after 2 seconds:" << std::endl;
    particle3D.printState();

    // Demonstrate vector addition
    Vector v1({1.0, 2.0});
    Vector v2({3.0, 4.0});
    Vector v3 = v1 + v2;
    std::cout << "\nVector addition: " << v1 << " + " << v2 << " = " << v3 << std::endl;

    double n1 = norm(1.0, 2.0);
    double n2 = norm(1.0, 2.0, 3.0);
    double n3 = norm(v1);
    std::cout << "norm n1= " << n1 << std::endl;
    std::cout << "norm n2= " << n2 << std::endl;
    std::cout << "norm n3= " << n3 << std::endl;

    return 0;
}
Back to wk02_OO_basics