2010 Lab-11
Lab elements include:
• Classes
• Constructors
• Access specifiers
• Setters (mutator)
• Getters (accessor)
• Hexadecimal numbers
Homework-11 Finish the lab programs as written. Date format program ------------------- 1. Copy your dates.cpp program to save-dates.cpp 2. Make a file named dates.h 3. The dates.h file will contain the Date class definition. 4. Remove the Date class definition from your dates.cpp program. 5. Pound-include dates.h into your dates.cpp program. Car program ----------- 1. Copy car.cpp to save-car.cpp 2. Make a file named car.h 3. The car.h file will contain the Car class definition only. No executable code is allowed in the car.h file. Your class will contain only data and function headers. 4. Remove the Car class definition from your car.cpp program. 5. Pound-include the car.h in your car.cpp program. 6. Make another file named carclass.cpp In this file define the function bodies of the Car class. 7. Compile your program like this... g++ car.cpp carclass.cpp -Wall
Getting started Do your work in your Odin 2010/b folder. Note: Programs 1 and 2 were derived from Chapter-13 Algorithm Workbench. You may have to look in the textbook to figure them out.
Date formats
------------
Program name: dates.cpp
Design a class named Date.
The class should store a date in just three integers: month, day, and year.
Data members should have private access.
There should be 3 member functions to print the date in the following forms:
12/25/2025
December 25, 2025
25 Dec 2025
Demonstrate the class in your program.
Input Validation:
Accept only months 1 through 12.
Do not accept day values that are not valid for the given month.
An invalid date should cause a "Usage" statement.
You should use this array to validate the day:
const int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
Also setup an array like this...
const string monthName[] = {"January","February","March","April","May",
"June","July","August","September","October",
"November","December"};
format of output:
Lab-11 Date formats
-------------------
Please enter a month, day, and year: 3 2 2019
Your three date formats are:
03/02/2019
March 2, 2019
2 Mar 2019
Stack overflow: pad with zeros
Your completed program will have the following...
1. No constructors in the Date class.
2. No cin allowed inside the Date class.
3. Setter functions for month, day, and year individually.
4. Date class must have 3 date-format member functions.
For easier testing you can run your program like this...
$ ./a.out <<< "3 2 2019"
Car going and stopping Program name: car.cpp Write a class named Car that has the following member variables: yearModel An integer that holds the car's year model. make A C-string or string that holds the make of the car. speed An integer that holds the car's current speed. The class should have a constructor and other member functions. Constructor The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make member variables. The constructor should assign 0 to the speed member variables. Accessors Appropriate accessor functions to get the values stored in an object's yearModel, make, and speed member variables. acceleration An accelerate function should add to the speed member variable each time it is called. Add a random value from 2 to 6. slowing A brake function should subtract from the speed member variable each time it is called. Subtract a random value from 2 to 6. Demonstrate the class in your program. Create a Car object, and then call the accelerate function multiple times, until the car gets up over 25. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function over and over until the car reaches a speed of zero or less. After each call to the brake function, get the current speed of the car and display it. If your car starts going backwards, set the speed to zero. You're done. No cout allowed in the Car class! format of output:lab-11 Car going & stopping --------------------------- The car is on the starting line... accelerate to 5 accelerate to 10 accelerate to 15 accelerate to 20 accelerate to 25 speed is 25 brake to 20 brake to 15 brake to 10 brake to 5 brake to 0 The car has stopped.Your output will look different each time your program is run.
Hexadecimal numbers
Program name: hex.cpp
Write a program with a function that uses a for-loop to count to 14.
Inside the for-loop display a period at each iteration.
Pause exactly one-eighth of a second during each loop iteration.
Required:
All numeric literals in your program must be in hexadecimal format.
At least 2 hexadecimal numeric literals must be used in your code.
There will be more than that.
do this...
· Call usleep() for your delay in printing.
· The usleep() argument must be a literal value.
· Calculate the hex value yourself using hex values.
Show your work in the comments.
format of output:
lab-11 Hexadecimal numbers
--------------------------
Counting to 0xE
123456789abcdef
for-loop is running now ..............
for-loop has finished.
Look at the animation below.
hint: use flush with cout so each dot displays as it is printed.
Retail items Program name: inventory.cpp Write a program with a class named RetailItem. The RetailItem class holds data about an item in a retail store. The class should have the following member variables: • description - A string that holds a description of the item. • unitsOnHand - An int that holds the number of units in inventory. • price - A double that holds the item's retail price. Write a constructor that accepts arguments for each member variable, appropriate mutator functions that store values in these member variables, and accessor functions that return the values in these member variables. Your program will create three RetailItem objects. Store the data shown in the sample output. No cin or cout is allowed from inside your class.
Tips: You should make an array of RetailItem objects. Your array must have a size of 3 or more.
format of output:lab-11 Retail items ------------------- Retail store inventory Units Description On Hand Price ------------- ------- ----- Item #1 Flak Jacket 12 59.95 Item #2 Designer Jeans 40 34.95 Item #3 Bowling Shirt 20 24.95