How to Develop a C++ Class to Simulate a Falling Object?

output, class, output, C++ simulate a falling object

Write a C++ class to simulate a falling object and a C++ driver in a main function to use this new class. The driver should ask for the initial height of the falling object, in feet. The output of the simulated falling object class should be the time for the object to fall to the ground, and the impact velocity, in feet per second and in miles per hour.
The C++ class requires a header file (Falling.h) to declare the class member variables and prototype the class member functions (methods) and a C++ file (Falling.cpp) to define and initialize class member variables and methods.
Your class should use Euler’s method to numerically solve the differential equations describing the motion of a falling object. In Euler’s method, the state of the object at some small future time is calculated using the values at the present time. This small future timestep is typically called delta-time (dt). Thus the position (p) and speed (v) of the object in the next time-step (t + dt) is written as a simple function of the position and speed at the current time-step t using the acceleration due to gravity (g) as shown below.
v(t+dt) = v(t) + g * dt
p(t+dt) = p(t) + v(t+dt) * dt
You should actually start out with the velocity as zero, and the position at the initial height from which the object is to be dropped. Then your position (above the ground) would be:
p(t+dt) = p(t) – v(t+dt) * dt
And you would integrate until the position becomes less than or equal to zero. Because floating point representations are only an approximation of real numbers, it is important to include the test for less than zero when you test for the final position of the falling object.

Solution:

Falling.cpp

#include “Falling.h”
#include <iostream>

using namespace std;

double ti , tj , pos ;

Falling:: Falling(){
          }
void Falling::startFall(double FEET ){
//   double t1 =  getTime();
        
    double     meter = (.3048) * FEET;
       
            double d =  meter;
        double vi = 0;
     //eucler equations
        //d = vi • t + ½ • a • t2
     // ½ • (-9.8 m/s2) • (t)2
     //val/(-4.9 m/s2) = t2
      
        double  g = 9.8/2;
        double t2 = (meter/g);
        double  t = sqrt(t2);
   
        cout<<“\nFalling time = “<<t<<” sec”;
      
       double meterPerSecond = 2 * 9.8 * meter ;
      double ftpersec =   3.280839895013 *  sqrt(meterPerSecond);// conversion to meterPerSecond
      double mph = 0.6818 * ftpersec;  // conversion to ftpersec
       cout<< “\nImpact speed =  “<<ftpersec;
       cout<< “\nImpact speed =  “<<mph;
      
          for (double dt =FEET  ; dt  >=  0 ; dt–){
             setTime(dt);
             double gp = getVelocity();
             double dtd = dt/gp;
              cout<<“\n position : “<<dt<<“ft”<<” : velocity after dt :”<<gp <<”   delta time : “<<dtd <<“sec” ;
              
              
            }
          }

double Falling::getTime(){
       
      
       return   ti+1;  

       }

void Falling::setTime(double s){
     
     ti = s;
 
     }

double Falling::getPosition(){
       
      double t1 =  getTime();
        double v =  getVelocity();
        double ty = (t1+1);
  double  p =  (p*t1 + v)/ty;
       return p;
       
       }

void Falling::setPosition(double p){
     pos = p;
     
     }

double Falling::getVelocity(){
        double t1 =  getTime();
        //dt = 1
        //v(t+dt) = v(t) + g * dt
     // v =g * dt / t1
   double  t = t1 + 1;
     double  v = (9.8 * 1 / t);
     return v;
       }

void Falling::moveOneClick(){}

Main.cpp

#include <stdio.h>
#include <conio.h>
#include “Falling.h”
#include <iostream>
using namespace std;
 double foot , meter;

 
int main(int argc, char *argv[])
{ Falling fall;
    cout<<“dropped from a specific height. Enter initial height in feet: “;
     cin>>foot;
    fall.setTime(0.0);
     meter = (.3048) * foot;
     fall.startFall(foot);
       cin >>meter;
     
     
    return EXIT_SUCCESS;
}

Output Screen:

class, output, C++ simulate a falling object

 

class, output, C++ simulate a falling object

LEAVE A REPLY

Please enter your comment!
Please enter your name here