Question: C++ /** file: ball.h ** brief: Ball class ** author: Andrea Vedaldi **/ #ifndef __ball__ #define __ball__ #include simulation.h class Ball : public Simulation {
C++
/** file: ball.h
** brief: Ball class
** author: Andrea Vedaldi
**/
#ifndef __ball__
#define __ball__
#include "simulation.h"
class Ball : public Simulation
{
public:
// Constructors and member functions
Ball() ;
void step(double dt) ;
void display() ;
protected:
// Data members
// Position and velocity of the ball
double x ;
double y ;
double vx ;
double vy ;
// Mass and size of the ball
double m ;
double r ;
// Gravity acceleration
double g ;
// Geometry of the box containing the ball
double xmin ;
double xmax ;
double ymin ;
double ymax ;
} ;
#endif /* defined(__ball__) */
/** file: ball.cpp
** brief: Ball class - implementation
** author: Andrea Vedaldi
**/
#include "ball.h"
#include
Ball::Ball()
: r(0.1), x(0), y(0), vx(0.3), vy(-0.1), g(9.8), m(1),
xmin(-1), xmax(1), ymin(-1), ymax(1)
{ }
void Ball::step(double dt)
{
double xp = x + vx * dt ;
double yp = y + vy * dt - 0.5 * g * dt * dt ;
if (xmin + r
x = xp ;
} else {
vx = -vx ;
}
if (ymin + r
Task 5: Member functions and separation of concerns. By isolating data members in the protected part of the class definition, the representation of the data (protected data members) can be separated from the operations that apply to it (public member functions). Answer the following questions: How should the class be changed so that a user could be able to get and set the position of the ball? The member functions of a class are often said to encode its "behaviour". Can you find a practical example demonstrating why separating the data representation from its behaviour is useful y = yp ;
vy = vy - g * dt ;
} else {
vy = -vy ;
}
}
void Ball::display()
{
std::cout
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
