Example Assignment 2

Example Assignment 2 - Example Assignment 2 TUTORIAL...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Example Assignment 2 TUTORIAL MATTER: Chapters 10, 11 and 12 of the Study Guide Chapters 10, 11 and 12 (excluding pages 700 to 712) of Savitch Appendices 7 and 8 in Savitch NB: section 6.4 in the 6 th edition corresponds to section 10.4 in the 7 th edition Answer all the questions. Submit all the programs you are required to write, as well as the input and output of all programs. Copy the programs and the required input and output to ONE word processor file. WE DO NOT ACCEPT ANY FLOPPIES, STIFFIES OR CDs. Question 1 Consider the following structure used to keep record of an invoice for a hardware store: struct Invoice { string description; int partNr; int quantity; float itemPrice; } Turn the invoice record into a class type rather than a structure type. The invoice record class should have private member variables for all the data. Include public member functions for each of the following: • a default constructor that initialises the description to a blank string, and all the other member variables to 0; • member functions to set each of the member variables to a value given as an argument to the function (i.e. mutators); • member functions to retrieve the data from each of the member variables (i.e. accessors); • and a member function that calculates the invoice amount (i.e. multiplies the quantity by the price per item) and returns the amount. Use this class in a program which prints an invoice. The program should read in a value for each of the member variables and then print an invoice consisting of the values for the member variables as well as the invoice amount. Use the keyboard to supply input and display the output on the screen. Test your program with the following input: Description: Hammer Part number: 342 Quantity: 2 Price per item: R89.99 Question 2 The questions below refer to the following class declaration: // Days in each month if not a leap year const int DaysInMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; class Date{ public: Date(); Date(int day, int month, int year); int getDay() const; int getMonth() const; int getYear() const; void setDay(int day); void setMonth(int month); void setYear(int year); //operators to calculate next and previous days friend Date &operator++(Date &d); friend Date &operator--(Date &d); friend bool operator<(const Date &d1, const Date &d2); private: int monthLength();//return the length of current month, taking // into account leap years int theDay; //the current day int theMonth; //the current month int theYear; //the current year }; (a) What is the purpose of the keywords public and private in the class declaration? (b) What is the purpose of a constructor? (c) Is setDay() an accessor or mutator? Explain your answer....
View Full Document

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 33

Example Assignment 2 - Example Assignment 2 TUTORIAL...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online