Lab 9: Passing data with structs and classes
Contents
Instructions
This lab will give you experience using structs and classes in C++. There are two parts: in the first, you will convert your Lab 5 to use a struct to represent contacts; in part two, you'll update that code to use a class with methods instead of a struct.
Part 1: structs
Copy your Lab 5 code (you can download what you submitted from Canvas) and save it as lab9-part1.cpp. Follow these steps:
- create a struct to hold the information about a contact; be sure to give it a reasonable name
- alter the program to use an array of struct instances instead of parallel arrays to store the contacts' information
- create a function to create new instances of the struct and update your code to use that function
- create a function to print the struct instances and update your code to use that function
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:
- copy your lab9-part1.cpp to a new file called lab9-part2.cpp
- convert your struct to a class—check out the class notes and the topic page on Classes/Structs
- modify your code so that the print function is a method inside of your class; update your code in main accordingly
- 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)