HOMEWORK HELP - Just so i stick by the rules.
The second part of my assignment is to create an array object, and love and behold, im having problems passing my small array into the function.
Bellow is my code of what i have done so far, the solution seems to scream pointers in my head, but classes is new to me and i just cant see how it would work?!
So Code bellow:
Header File
//Create objects called asteroid, and apply conditions to them. Once the asteroids have values tied to them,
//then need to check to see if the energy matches and if the asteroids have collided.
#pragma once
class coord
{
private:
int xx;
int yy;
int velocity;
int energy;
//Methods
public:
//Constructors
coord(int xxIn, int yyIn, int velocityIn, int energyIn);
coord(void);
//Getters
int getxx(void);
int getyy(void);
int getvelocity(void);
int getenergy(void);
//Setters
void updatex(int xxIn);
void updatey(int yyIn);
void setvelocity(int velocityIn);
void setenergy(int energyIn);
void collision(coord[]);//pass the class in. set to array[] in attempt to fix code
void energycomp(coord[]);//pass the class in. set to array[] in attempt to fix code
void printing(coord[]);//Printing values. set to array[] in attempt to fix code
//hoping that by changing the prototypes to match the rest of the code, passing arrays will work
};
Implimentation CPP
#include "coord.h"
#include<iostream>
using namespace std;
coord::coord(void)
:xx(0)
,yy (0)
,velocity(0)
,energy(0)
{
}
coord::coord(int xxIn, int yyIn, int velocityIn, int energyIn)
{
xx = xxIn;
yy = yyIn;
velocity = velocityIn;
energy = energyIn;
}
int coord::getxx(void)
{
//To get x
return xx;
}
int coord::getyy(void)
{
//To get y
return yy;
}
int coord::getvelocity(void)
{
//To get velocity
return velocity;
}
int coord::getenergy(void)
{
//To get energy
return energy;
}
//Setters
void coord::updatex(int xxIn)
{
//To store in x value
xx = xx + xxIn;
}
void coord::updatey(int yyIn)
{
//To store in y value
yy = yy + yyIn;
}
void coord::setvelocity(int velocityIn)
{
//To store velocity
velocity = velocity + velocityIn;
}
void coord::setenergy(int energyIn)
{
//To store energy
energy = energy + energyIn;
}
void coord::collision(coord asteroid[])
{
//To check if the objects have collided
for(int i = 0; i<2; i++)
{
if(this->xx == asteroid[i].getxx() && this->yy == asteroid[i+1].getyy())
{
cout<<"Collision"<<endl;
}
}
}
void coord::energycomp(coord asteroid[])
{
//To check if the objects energies match
for(int i = 0; i<2; i++)
{
if(asteroid[i].getenergy() == asteroid[i+1].getenergy())
{
cout<<"Energy Signatures match"<<endl;
}
}
}
void coord::printing(coord asteroid[])
{
//To print values
for(int i = 0;i < 2; i++)
{
cout<<"Asteroid"<<asteroid[i]<<"Details: "<<endl;
cout<<asteroid[i].getxx()<<","<<asteroid[i].getyy()<<endl;
cout<<"Energy: "<<asteroid[i].getenergy()<<endl;
cout<<"Velocity: "<<asteroid[i].getvelocity()<<endl;
cout<<endl;
}
}
Main
#include "coord.h"
#include<iostream>
using namespace std;
int main()
{
//Variables
int astx = 0;
int asty = 0;
int asten = 0;
int astvel = 0;
////////////
//Creating objects
coord asteroid[2];
for(int i = 0; i<2; i++)
{
astx = 0;
asty = 0;
asten = 0;
astvel = 0;
//////
cout<<"Set asteroid "<<i<<" X"<<endl;
cin>>astx;
asteroid[i].updatex(astx);
cout<<"Set asteroid "<<i<<" Y"<<endl;
cin>>asty;
asteroid[i].updatey(asty);
cout<<"Set asteroid "<<i<<" Energy"<<endl;
cin>>asten;
asteroid[i].setenergy(asten);
cout<<"Set asteroid "<<i<<" Velocity"<<endl;
cin>>astvel;
asteroid[i].setvelocity(astvel);
}
//Collision check
for(int i = 0; i< 2; i++)
{
asteroid[i].collision(asteroid[i+1]);
}
//Energy check
for(int i = 0; i< 2; i++)
{
asteroid[i].energycomp(asteroid[i+1]);
}
//Printing to screen
for(int i = 0; i< 2; i++)
{
asteroid[i].printing(asteroid[i+1]);
}
//
system("pause");
}
My layout may seem strange, my lecturer insists that everything be kept in this style, class definition in header, all functions in a seperate .cpp and minimal main.
Thanks for any help offered