Starting from:

$30

Lab #2 Separate Compilation and Basic C++ Solution

*All programs will be tested on the machines in the Q22 lab. If your code does not run on the system in this lab, it is considered non-functioning EVEN IF IT RUNS ON YOUR PERSONAL COMPUTER. Always check that your code runs on the lab machines before submitting.*





Guidelines





This is an individual lab assignment. You must do the vast majority of the work on your own. It is permissible to consult with classmates to ask general questions about the assignment, to help discover and fix specific bugs, and to talk about high level approaches in general terms. It is not permissible to give or receive answers or solution details from fellow students.





You may research online for additional resources; however, you may not use code that was written specifically *to* solve the problem you have been given, and you may not have anyone else help you write the code or solve the problem. You may use code snippets found online, providing that they are appropriately and clearly cited, within your submitted code.





*By submitting this assignment, you agree that you have followed the above guidelines regarding collaboration and research.*





***





The goal of Lab 2 is to familiarize you with the few basic C++ features and aspects that we have discussed in the first weeks of class. In particular, you will implement multiple classes with various methods; you will use iostream to produce formatted output; you will do just a bit more separate compilation by building two separate object (i.e. _.o_) files, and then separately link them into a single executable.





## Part A: Star Program





You should implement two classes, Star and Planet. Both these classes are already defined in a Star.h file and should be implemented in the Star.cpp file. All methods you write must be external.





### Planet

The Planet class has two constructors. One that creates a Planet and initializes the id to -1, and another that takes a distance and an #id number as a parameter. Your planet class should have the following interface:

* _public_:

* Planet(); //already implemented for you

* A default constructor so you can allocate memory for an empty array. This is for utility purposes only and should be considered an invalid object.

* Planet(int distance, int id); //already implemented for you

* A value constructor that sets the Planet’s `id` to a unique value and its `distance` to a random number between 0 to 3000 (in millions of miles).

* The `pos` should be set to 0

* int orbit();

* Moves the planet +1 unit around the Star.

* Your position should always be in the range 0-359.

* Returns the planet's current position

* int getID();

* returns the Planet’s ID number

* int getDistance();

* returns the Planet's distance from the Star

* int getPosition();

* returns the Planet's position relative to the Star





:bulb: Notice I only gave you the public interface. Although I wrote the private interface for you in this lab, normally the private interface will be up to you.





__Show your TA your code.__





__--END OF IN LAB REQUIRED WORK--__





__You may continue to work on the remainder of the lab on your own time or in lab__





### Star

The Star class will have a constructor that takes a random number of planets it is allowed to hold as a parameter, between 0 and 20. You will also need an array of Planet objects. For now, the Planet array is allocated to buffer size of 20. We will make this more dynamic in a later lab. Below is the expected interface for your Star class.

* _public_:

* Planet planets[20];

* An array of Planets. We will make this more dynamic in a later lab.

* Star(int);

* A constructor that takes the maximum number of Planets as a parameter

* bool addPlanet(Planet p);

* Adds a new Planet object to the Star with a unique id and a random distance. The new planet is only added if the maximum number of planets the Star can support has not yet been reached.

* Return true on success and false on failure.

* Planet getFurthest();

* Returns the furthest planet form the Star.

* void orbit();

* calls orbit on all planets orbiting the Star

* void printStarInfo();

* Prints the Star information. (See sample output above)

* int getCurrentNumPlanets();

* returns the current number of planets

* int getMaxPlanets();

* returns the maximum number of planets





## Part B: Testing and Running Your Code

### Make

You are going to be using separate compilation to build your program. This means you will need to create two object files. One from the provided lab2.cpp file, and one from your Star.cpp file. The header file, or .h file, contains your class declarations. You never compile header files. They are always included in other files that get compiled and should be dependencies for a target, never a target themselves. Often, they will be included in multiple files and as multiple dependencies.





For this class we are going to be using `make` and makefiles to compile our code. Write a makefile that compiles lab2.cpp and Star.cpp to separate object files. Then link them as shown in class. Your makefile will need to have the following targets (in addition to the compile and linking targets).

* __all__

* This will compile your code to an executable, and should have your executable target as a dependency

* __run__

* This target has ‘all’ as a dependency, then runs your executable

* __clean__

* removes object files and the executable. Be careful not to delete your source code.

* :bulb: You may add any additional targets you feel are useful

* For example, I always add a `submit` target that first cleans, then adds all files, commits, and pushes to github.

 

[Click here for more information on makefiles](http://mrbook.org/blog/tutorials/make/)





You must use variables in your makefile to accomplish the following:

* A compiler flag variable that will easily allow you to switch compiler flags

* example compiler flags I recommend:

* `-Wall -Wextra -DDEBUG -g -std=c++14`

* A project name variable that you can use throughout your makefile





### Constraints





The above public methods for your classes are the minimum interface requirements. You may add additional methods or instance variables to your classes if you feel they are needed. For full credit, please adhere to the following constraints, in addition to what is described above:

* You may not use any __global variables__. The only exception to this is constant values, and they should be declared in all uppercase.

* Your lab must build from 2 different C++ source files (See expected structure below).

* Your ‘Star’ /’Planet’ classes should be defined in a header file, and implemented in a separate implementation (.cpp) file.

* The C++ file that contains the main() is provided below. You must use the provided driver code since it contains tests you must pass.

* _lab2.cpp_ and _Star.cpp_ should build into .o files called _lab2.o_ and _Star.o_, respectively, and should then link into an executable called _lab2_. Therefore, after building your program with make, we should be able to type `./lab2` to run it.





## Part C : Code Organization and Submission

* Required code organization:

* lab2.cpp (driver code - You must include this file in your submission)

* Star.h

* Star.cpp

* makefile

* executable should be called: lab2

* _do not add a .exe extension_





Below is just a reminder of the commands you should use to submit your code. If you cannot remember the exact process, please review lab 1.





_These commands all presume that your current working directory is within the directory tracked by `git`._





You will need to do the following when your submission is ready for grading.

```shell

git commit -am "final commit"

git push

```





To complete your submission, you must copy and paste the commit hash into MyCourses. Go to MyCourses, select CS110, and then assignments. Select Lab 2, and where it says text submission, paste your commit hash. You can get your latest commit hash with the following command:





```shell

git rev-parse HEAD

```





:warning: Remember, you __MUST__ make a submission on mycourses before the deadline to be considered on time.

More products