|
| 1 | +<p align="center"> |
| 2 | + <img src="docs/bjarne.jpg" alt="Bjarne Stroustrup writing the declaration of a class on a whiteboard" /> |
| 3 | +</p> |
| 4 | + |
| 5 | +# Assignment 3: Make a Class |
| 6 | + |
| 7 | +Due Saturday November 1st at 11:59PM |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +<pre> |
| 12 | +(\_/) |
| 13 | +(•x•) <b>Howdy</b> |
| 14 | +(<☕) |
| 15 | +</pre> |
| 16 | + |
| 17 | +Now that we've learned about classes, it’s time for you to make your own! Have fun with this, let the creative juices flow. Your class can represent anything, and feel free to make more than one if you'd like. There are some requirements though. As long as you meet these requirements, you’ll get credit for this assignment! 🙂 |
| 18 | + |
| 19 | +There are four files you'll work with for this assignment: |
| 20 | + |
| 21 | +* `class.h` - This is the header file for your class, where the class **declaration** will go. |
| 22 | +* `class.cpp` - This is the `.cpp` file for your class, where the class **definition** will go. |
| 23 | +* `sandbox.cpp` - You'll construct an instance of your class here. |
| 24 | +* `short_answer.txt` - You'll answer a few short answer questions here. |
| 25 | + |
| 26 | +To download the starter code for this assignment, please see the instructions for [**Getting Started**](../README.md#getting-started) on the course assignments repository. |
| 27 | + |
| 28 | +## Running your code |
| 29 | + |
| 30 | +To run your code, first you'll need to compile it. Open up a terminal (if you are using VSCode, hit <kbd>Ctrl+\`</kbd> or go to **Terminal > New Terminal** at the top). Then make sure that you are in the `assignment3/` directory and run: |
| 31 | + |
| 32 | +```sh |
| 33 | +g++ -std=c++20 main.cpp class.cpp -o main |
| 34 | +``` |
| 35 | + |
| 36 | +Assuming that your code compiles without any compiler errors, you can now do: |
| 37 | + |
| 38 | +```sh |
| 39 | +./main |
| 40 | +``` |
| 41 | + |
| 42 | +which will actually run the `main` function in `main.cpp`. |
| 43 | + |
| 44 | +As you are following the instructions below, we recommend intermittently compiling/testing with the autograder as a way to make sure you're on the right track! |
| 45 | + |
| 46 | +> [!NOTE] |
| 47 | +> |
| 48 | +> ### Note for Windows |
| 49 | +> |
| 50 | +> On Windows, you may need to compile your code using |
| 51 | +> |
| 52 | +> ```sh |
| 53 | +> g++ -static-libstdc++ -std=c++20 main.cpp class.cpp -o main |
| 54 | +> ``` |
| 55 | +> |
| 56 | +> in order to see output. Also, the output executable may be called `main.exe`, in which case you'll run your code with: |
| 57 | +> |
| 58 | +> ```sh |
| 59 | +> ./main.exe |
| 60 | +> ``` |
| 61 | +
|
| 62 | +## Part 1: Making your class |
| 63 | +
|
| 64 | +Let your creative juices flow! Fill in `class.h` and `class.cpp` to create your own custom class. Please refer to the relevant slides from Tuesday's lecture on classes for more information. Your class can represent pretty much anything you want, as long as it meets the following requirements. |
| 65 | +
|
| 66 | +> [!IMPORTANT] |
| 67 | +> ### Class Requirements |
| 68 | +> |
| 69 | +> Your class must: |
| 70 | +> 1. Have a custom constructor taking **one or more** parameters. |
| 71 | +> 2. Have a default (parameterless) constructor (i.e. constructor overloading). |
| 72 | +> 3. Have one or more private member fields (i.e. variables). |
| 73 | +> 4. Have one or more private member functions. |
| 74 | +> - Remember, private functions are like what happens underneath the hood of your car! They are a necessary part of the implementation of a class, but shouldn't be exposed in the public interface. Try to think of a private member function that logically makes sense in the context of your class. |
| 75 | +> 5. Have **at least one** public getter function for one of the private fields. |
| 76 | +> - E.g. if `int data` is the field, you must have a function called `get_data` or `getData` with the signature <pre lang="cpp">int getData();</pre> |
| 77 | +> - The getter function should also be marked `const`. Refer to Thursday's lecture on `const` correctness if you are unfamiliar! |
| 78 | +> 6. Have at least one public setter function for one of the private fields. |
| 79 | +> - E.g. if `int data` is the field, you must have a function called `set_data` or `setData` with the signature <pre lang="cpp">void setData(int value);</pre> |
| 80 | +
|
| 81 | +Note that this is the bare minimum to get credit for the assignment. Please feel free to go above and beyond these requirements or create more than one class if you want extra practice! |
| 82 | +
|
| 83 | +> [!NOTE] |
| 84 | +> For brownie points, you can choose to create a class template instead of a regular class using the `template <typename T>` notation discussed on Thursday's lecture. This is totally optional! |
| 85 | +> |
| 86 | +> Note that if you do decide to create a class template, you **must remove class.cpp |
| 87 | +> from the compilation command.** For example, on Mac/Linux, the compilation |
| 88 | +> command will be: |
| 89 | +> |
| 90 | +> ```sh |
| 91 | +> g++ -std=c++20 main.cpp -o main |
| 92 | +> ``` |
| 93 | +> |
| 94 | +> Remember to also swap the includes so that the `.h` file includes the `.cpp` |
| 95 | +> file at the end of the file, as discussed in Thursday's lecture. |
| 96 | +
|
| 97 | +Now that you've created your class, let's actually use it. **Inside of the `sandbox` function in `sandbox.cpp`, construct an instance of your class!** You can do so however you like (call default constructor, use uniform initialization, etc.). |
| 98 | +
|
| 99 | +To see if you did everything correctly, compile and run your code! The autograder will give you feedback on your class and check if it meets the specifications above. |
| 100 | +
|
| 101 | +## Part 2: Short answer questions |
| 102 | +
|
| 103 | +Please answer the following questions inside `short_answer.txt`. We expect about 2-3 sentences per question. |
| 104 | +
|
| 105 | +> [!IMPORTANT] |
| 106 | +> `short_answer.txt` |
| 107 | +> - **Q1:** What’s const-correctness and why is it important? |
| 108 | +> - **Q2:** Is your class const-correct? How do you know? |
| 109 | +
|
| 110 | +## 🚀 Submission Instructions |
| 111 | +
|
| 112 | +To submit the assignment: |
| 113 | +1. Please complete the feedback form [at this link](https://forms.gle/GmhzW9NycQ44hyF86). |
| 114 | +2. Submit your assignment on [Paperless](https://paperless.stanford.edu)! |
| 115 | +
|
| 116 | +Your deliverable should be: |
| 117 | +
|
| 118 | +* `class.h` |
| 119 | +* `class.cpp` |
| 120 | +* `sandbox.cpp` |
| 121 | +* `short_answer.txt` |
| 122 | +
|
| 123 | +You may resubmit as many times as you'd like before the deadline. |
0 commit comments