I have difficulties with separating this class into a header and .cpp file because c++ is relatively new for me. This class is an example my teacher programmed with us in class. It isn't homework, but it will help me understand my homework.
#include <iostream>
#include <AstUtils.h>
#include <vector>
#include <cassert>
#include <Vector2.h>
#define WIDTH 1377
#define HEIGHT 768
using namespace std;
using namespace astu;
class Stone {
public:
Stone(double x = 0, double y = 0)
: x(x), y(y), vx(0), vy(0)
{
//empty
}
void SetPos(double x, double y){
this->x = x;
this->y = y;
}
void Render() {
SetRenderColor(255,0,0);
RenderRectangle(x,y,20,20,true);
}
double GetPosX() const {
return x;
}
double GetPosY() const {
return y;
}
void SetVel(double vx, double vy){
this->vx = vx;
this->vy = vy;
}
void SetAcc(double ax, double ay){
this->ax = ax;
this->ay = ay;
}
void Update(double dt) {
x += vx*dt;
y += vy*dt;
vx += ax*dt;
vy += ay*dt;
ax = 0;
ay = 0;
}
private:
double x,y;
double vx, vy;
double ax, ay;
};
void PlotCurve(vector<double> & vertices) {
assert(vertices.size() % 2 == 0);
assert(vertices.size() >= 4);
auto it = vertices.begin();
double x1 = *it++;
double y1 = *it++;
while(it != vertices.end()){
double x2 = *it++;
double y2 = *it++;
RenderLine(x1,y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
void ReportError()
{
std::cout << "An error has occured: " << GetLastErrorMessage() << std::endl;
std::cout << GetErrorDetails() << std::endl;
}
int main()
{
if (InitApp(WIDTH,HEIGHT, "Demo") != NO_ERROR) {
ReportError();
return -1;
}
Vector2<double> center(WIDTH/2, HEIGHT/2);
Stone stone(WIDTH * 0.3, HEIGHT/2);
stone.SetVel(100, -150);
vector<double> posCurve;
posCurve.push_back(stone.GetPosX());
posCurve.push_back(stone.GetPosY());
while(!IsAppTerminated()) {
ClearCanvas();
Vector2 p(stone.GetPosX(), stone.GetPosY());
Vector2 d = center - p;
double dist = d.LengthSquared();
d.Normalize();
d *= 50000000 / dist;
stone.SetAcc(d.x,d.y);
stone.Update(GetDeltaTime());
stone.Render();
posCurve.push_back(stone.GetPosX());
posCurve.push_back(stone.GetPosY());
SetRenderColor(255,255,255);
PlotCurve(posCurve);
UpdateApp();
}
QuitApp();
}
This is what I tried:
(Didn't know how to define the constructor and also had problems with this, since the variables are in the header.)
.cpp:
Stone::Stone(double x = 0, double y = 0)
: x(x), y(y), vx(0), vy(0)
{}
void SetPos(double x, double y){
this->x = x;
this->y = y;
}
void Render() {
SetRenderColor(255,0,0);
RenderRectangle(x,y,20,20,true);
}
double GetPosX() const {
return x;
}
double GetPosY() const {
return y;
}
void SetVel(double vx, double vy){
this->vx = vx;
this->vy = vy;
}
void SetAcc(double ax, double ay){
this->ax = ax;
this->ay = ay;
}
void Update(double dt) {
x += vx*dt;
y += vy*dt;
vx += ax*dt;
vy += ay*dt;
ax = 0;
ay = 0;
}
.h:
class Stone{
public:
Stone(double x = 0, double y = 0);
void SetPos(double x, double y);
void Render();
double GetPosX();
double GetPosY();
void SetVel(double vx, double vy);
void SetAcc(double ax, double ay);
void Update(double dt);
private:
double x,y;
double vx, vy;
double ax, ay;
};
Stone::Stonebut you didn't writeStone::SetPosor similar. Also, did you#includeyour header file anywhere in the cpp file?Stoneconstructor by prefacing it with the qualifyingStone::? Yeah, you need to do that for every member you likewise implemented out of the class namespace.#includemy header file in the .cpp and in a main.cppStone(double x, double y);