Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Planet.h"
- #include "Star.h"
- #include <stdlib.h>
- #include <cstdio>
- #include <iostream>
- using namespace std;
- Star::Star(){
- current_planets = 0;
- next_id = 1;
- planets = NULL;
- }
- Star::~Star(){
- for(int i = 0; i < (int)current_planets; i++){
- delete planets[i];
- }
- delete[] planets;
- }
- int Star::addPlanet(){
- Planet * p = new Planet((unsigned int)(rand() % 3001), next_id);
- Planet ** pList = new Planet * [current_planets + 1];
- for(int i = 0; i < (int)current_planets; i++){
- pList[i] = planets[i];
- }
- pList[current_planets] = p;
- current_planets = current_planets + 1;
- delete[] planets;
- planets = pList;
- return next_id++;
- }
- bool Star::removePlanet(int i){
- for(int j = 0; j < (int)current_planets; j++){
- if(planets[j]->getID() == i){
- Planet ** pList = new Planet * [current_planets - 1];
- for(int k = 0; k < j; k++){
- pList[k] = planets[k];
- }
- for(int k = j + 1; k < (int)current_planets; k++){
- pList[k - 1] = planets[k];
- }
- current_planets = current_planets - 1;
- delete[] planets;
- planets = pList;
- return true;
- }
- }
- return false;
- }
- Planet * Star::getPlanet(int i){
- for(int j = 0; j < (int)current_planets; ++j){
- if(planets[i]->getID() == i){
- return (planets[j]);
- }
- }
- return NULL;
- }
- void Star::orbit(){
- for (int i = 0; (unsigned int)i < current_planets; ++i){
- planets[i]->orbit();
- }
- }
- void Star::printStarInfo(){
- printf("This star currently has %u.\n", current_planets);
- for(int i = 0; (unsigned int)i < current_planets; i++){
- printf("Planet id %c%d\n", planets[i]->getType(), planets[i]->getID());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement