cpp Constructor Destructor

Published on: 04 February 2025

Resources

  1. Video Lecture

Aim: Create a program that implements constructor (simple, copy, parameterized) and destructor.

Practice Exercise 5: Write a program to calculate the kinetic energy of a particle implementing simple constructor and destructor.

Theory:

Algorithm:

Code:

#include <iostream>
using namespace std;

class Particle {
public:
    float mass;     // Mass of the particle
    float velocity; // Velocity of the particle

    // Simple constructor to initialize mass and velocity to default values
    Particle() {
        mass = 1.0;       // Default mass of 1.0 kg
        velocity = 0.0;   // Default velocity of 0.0 m/s
        cout << "Particle object created." << endl;
    }

    // Method to calculate kinetic energy
    float calculateKineticEnergy() {
        return 0.5 * mass * velocity * velocity;
    }

    // Destructor to display message when object is destroyed
    ~Particle() {
        cout << "Particle object destroyed." << endl;
    }
};

int main() {
    // Create a Particle object
    Particle p;

    // Set initial velocity
    p.velocity = 5.0;  // Set velocity to 5 m/s

    // Calculate and display kinetic energy
    float kineticEnergy = p.calculateKineticEnergy();
    cout << "Kinetic Energy: " << kineticEnergy << " Joules" << endl;

    // Destructor will automatically be called when the object goes out of scope
    return 0;
}

Practice Exercise 6: Write a program to calculate the kinetic energy of a particle implementing copy constructor and destructor.

Theory:

Algorithm:

Code:

#include <iostream>
using namespace std;

class Particle {
public:
    float mass;     // Mass of the particle
    float velocity; // Velocity of the particle

    // Simple constructor to initialize mass and velocity to default values
    Particle() {
        mass = 1.0;     // Default mass of 1.0 kg
        velocity = 0.0; // Default velocity of 0.0 m/s
        cout << "Particle object created." << endl;
    }

    // Copy constructor
    Particle(Particle & other) {
        mass = other.mass;
        velocity = other.velocity;
        cout << "Particle object copied." << endl;
    }

    // Method to calculate kinetic energy
    float calculateKineticEnergy() {
        return 0.5 * mass * velocity * velocity;
    }

    // Destructor to display message when object is destroyed
    ~Particle() {
        cout << "Particle object destroyed." << endl;
    }
};

int main() {
    // Create a Particle object
    Particle p1;

    // Set initial velocity for p1
    p1.velocity = 5.0;  // Set velocity to 5 m/s

    // Calculate and display kinetic energy of p1
    float kineticEnergy1 = p1.calculateKineticEnergy();
    cout << "Kinetic Energy of p1: " << kineticEnergy1 << " Joules" << endl;

    // Create a copy of p1 using the copy constructor
    Particle p2 = p1;

    // Calculate and display kinetic energy of p2
    float kineticEnergy2 = p2.calculateKineticEnergy();
    cout << "Kinetic Energy of p2 (copied from p1): " << kineticEnergy2 << " Joules" << endl;

    // Destructor will automatically be called when the objects go out of scope
    return 0;
}

Practice Exercise 7: Write a program to calculate the kinetic energy of a particle implementing parameterized constructor and destructor.

Theory:

Algorithm:

Code:

#include <iostream>
using namespace std;

class Particle {
public:
    float mass;     // Mass of the particle
    float velocity; // Velocity of the particle

    // Parameterized constructor to initialize mass and velocity
    Particle(float m, float v) {
        mass = m;       // Initialize mass
        velocity = v;   // Initialize velocity
        cout << "Particle object created with mass: " << mass << " and velocity: " << velocity << endl;
    }

    // Method to calculate kinetic energy
    float calculateKineticEnergy() {
        return 0.5 * mass * velocity * velocity;
    }

    // Destructor to display message when object is destroyed
    ~Particle() {
        cout << "Particle object destroyed." << endl;
    }
};

int main() {
    // Create a Particle object with parameterized constructor
    Particle p(2.0, 5.0);  // Set mass to 2.0 kg and velocity to 5.0 m/s

    // Calculate and display kinetic energy of p
    float kineticEnergy = p.calculateKineticEnergy();
    cout << "Kinetic Energy of p: " << kineticEnergy << " Joules" << endl;

    // Destructor will automatically be called when the objects go out of scope
    return 0;
}

Extra Self Practice Material

Code:

#include <iostream>
#include <cmath>

using namespace std;

class Projectile
{
private:
    float mass;
    float velocity;
    float angle;
    float initialHeight;
    const float g = 9.81; // acceleration due to gravity

public:
    Projectile(float m, float v, float a, float h)
    {
        mass = m;
        velocity = v;
        angle = a;
        initialHeight = h;
    }

    void calculateTrajectory()
    {
        // Convert angle to radians
        float theta = angle * M_PI / 180;

        // Calculate time of flight
        float timeOfFlight = 2 * velocity * sin(theta) / g;

        // Calculate range
        float range = velocity * cos(theta) * timeOfFlight;

        // Calculate maximum height
        float maxHeight = initialHeight + (velocity * sin(theta) * timeOfFlight) - (0.5 * g * pow(timeOfFlight, 2));

        // Output results
        cout << "Time of flight: " << timeOfFlight << " seconds" << endl;
        cout << "Range: " << range << " meters" << endl;
        cout << "Maximum height: " << maxHeight << " meters" << endl;
    }
};

int main()
{
    // Create a projectile object with mass 0.1 kg, velocity 50 m/s, angle 30 degrees, and initial height 0 meters
    Projectile projectile(0.1, 50, 30, 0);

    // Calculate the trajectory and output the results
    projectile.calculateTrajectory();

    return 0;
}

References

There may be some AI Generated content in this article used for demonstration purposes.