Contents

Instructions

This lab will give you experience using structs and classes in C++. There are two parts: in the first, you will create a program that uses structs to store information about something of your choosing; in part two, you'll update that code to use a class with methods instead of a struct.

Part 1: structs

In this part, you will make your very own struct. You'll need to come up with an object type that can be represented as a struct and that you can create instances of. For example, in class we saw a struct called User, which represented a generic user. We could then create an instance of User for a specific user (e.g., a user named Bob, another named Jane, etc.). Come up with a different object type from those we've used in class or that are in the Classes/Structs topic page. If you're stuck, here are some suggestions: a movie, a grocery item, or a song. Don't feel limited to those!

Once you've picked an object type, follow these steps:

  1. make a new file called lab7-part1.cpp
  2. pick a name for your struct
  3. define your struct—you should have at least three fields
  4. create a function to create new instances of your struct—it should take one parameter per field in your struct and return an instance of your struct
  5. create a function to print out an instance of the struct
  6. in main, create three instances of your struct using the create function from above
  7. print out each instance using the print function you created

Use this template to get started:

Part 2: classes

In this part, you'll modify your part 1 to use classes instead of structs. It'll be helpful to check out the Structs/Classes topic page. Follow these steps:

  1. copy your lab7-part1.cpp to a new file called lab7-part2.cpp
  2. convert your struct to a class—check out the class notes and the topic page on Classes/Structs
  3. modify your code so that the print function is a method inside of your class; update your code in main accordingly
  4. remove the function to create new instances of your class, and instead create a constructor for your class—see the topic page on Classes/Structs for the syntax for a constructor; be sure to update your code in main to use this constructor

Submission

Submit your .cpp files to here. You can view the rubric here.

(Back to top)