Lab 9: Classes

Quiz:

The quiz will be available during the first 5 minutes of lab:

    • Your lab TA will tell you the password you will need to get into the quiz for your section.

    • You may use either one of the lab machines or your own laptop.

      • For the lab computers your account name and password are the same as your UIC NetID and password.

    • Please Click on Your Lab Time: 9am, 10am, 11am, 12pm, 1pm, 2pm, 3pm, 4pm, 5pm


In this lab, we will be writing classes and see how they work with data members and member functions. For similar examples, see Zybooks and the class notes from the last several lectures.


Zybooks Lab: Section 9.24


Stage 1: Get(), Set() member functions (1 Point)


Create a class Vacation with:

  1. Two data members, a destination (one-word place as a string) for example "Acapulco" or "Istanbul" or "Peoria" cost (integer) which represents the overall cost in thousands of dollars for the vacation

  2. A set() function for each data member: main() accepts the user input for the respective values and passes them to the set() function for each data member.

  3. A get() function for each data member:The function returns the values of the respective fields for display in main().

  4. Running the program should like: (input indicated in bold)

  5. Enter vacation destination and cost: Cancun 1000

  6. The vacation is in Cancun.

  7. Please enter test case number: -1

Starting code for this portion looks like:

#include <iostream>

#include <string>

using namespace std;



// MAKE/INSERT THE VACATION CLASS HERE


int main(){



// DECLARE AN INSTANCE OF VACATION AND CALL IT V1

string destination; // Vacation destination as a single-word

int cost; // Vacation cost, used for user input


// Taking in the destination and cost parameters

cout << "Enter vacation destination and cost: ";

cin >> destination >> cost;


// SET THE DESTINATION AND COST PARAMETERS HERE


// UPDATE THE VARIABLE "theDestination" to equal the actual destination of v1

string theDestination;

cout << "The vacation is in " << theDestination << "." << endl;



// Uncomment these lines only when implementing Stage 2

// Use v1 to call the display() function, which displays destination and cost,

// as shown in the sample output below


// Enter either 2 or 3 to check those test cases -1 to check neither

// To check the default constructor please enter 3 for the qualified enter 4

int testCaseCheck;

cout << "Please enter test case number: ";

cin >> testCaseCheck;


switch(testCaseCheck) {

case 2: {

// UNCOMMENT THE CODE BELOW WHEN YOU START PART 2

// v1.displayCost();

break;

}

case 3: {

cout << "Second General Vacation: " << endl;

// AFTER MAKING YOUR DEFAULT CONSTRUCTOR INITIALIZE A DEFAULT VACATION

// NAME THIS VACATION v2 AND DISPLAY THE VACATION COST BY UNCOMMENTING THE CODE

// v2.displayCost();

break;

}

case 4: {

cout << "Second Specific Vacation: " << endl;


string pDestination;

int pCost;


// Taking in the destination and cost parameters

cout << "Enter parameterized vacation destination and cost: ";

cin >> pDestination >> pCost;


// AFTER MAKING YOUR PARAMETERIZED CONSTRUCTOR INITIALIZE A PARAMETERIZED VACATION

// NAME THIS VACATION v3

// ***INSERT INITIALIZED PARAMETERIZED VACATION HERE***

// Display v3 information using displayCost

// UNCOMMENT BELOW CODE TO DISPLAY v3 INFORMATION USING displayCost

// v3.displayCost();

break;

}

default:

break;

}



return 0;

}


Stage 2: Display Function (1 Point)

Write a displayCost() function to display the vacation cost values on the screen. The function should call the appropriate get() function.

Running the program should look like (input indicated in bold and output of displayCost() is in RED):

Enter vacation destination and cost: Cancun 4

That vacation is in: Cancun

Vacation information: Cancun with cost: 4


Stage 3: Constructors (1 Point Extra Credit)


a) Write a default constructor for the class to accept "NYC" as the default destination and 3 as the default cost.

Enter vacation destination and cost: Miami 300

The vacation is in Miami.

Please enter test case number: 3

Second General Vacation:

Vacation information: NYC with cost: 3

b) Write a fully qualified constructor for the class, which accepts input from user in main() for both destination and cost.

Enter vacation destination and cost: Miami 300

The vacation is in Miami.

Please enter test case number: 4

Second Specific Vacation:

Enter parameterized vacation destination and cost: Boston 500

Vacation information: Boston with cost: 500