COS110 Tutorial8-28Sept2011-MEMO

COS110 Tutorial8-28Sept2011-MEMO - COS110 Tutorial 8 Date:...

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

View Full Document Right Arrow Icon
1 COS110 Tutorial 8 MEMO-MEMO-MEMO Date: 28 September 2011 Time: 50 minutes Marks: 34 Student Surname and Initials:____________________________________________ Student number:__________________ Employee number:___________________ Question 1 Exceptions [10 marks] (Scope: Lecture 21, textbook section 16.1) Study the following code for the Person class and complete the indicated sections. //file Person.h //--------------- class Person { public: Person( ); Person(char *, int); virtual ~Person(); //get/set member functions go here class AgeException { public: AgeException(int); void print(); //print value & message private: int value; //invalid value for age static const char[] message; //message generated by exception }; protected: char *name; int age; };
Background image of page 1

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

View Full DocumentRight Arrow Icon
//file Person.C //---------------------- const Person::AgeException::message = “invalid value for age”; a. Complete the following code: (2 marks) Person::AgeException::AgeException(int ageVal) { //code to store the ageVal into the exception object __________________________________________ } void Person::AgeException::print() { //code to print the exception message __________________________________________ } b. Complete the following code: (4 marks) Person::Person(char *nameValue, int ageValue) { if (age < 0) || (age > 120) //code to create an exception _____________________________________________________________ else { //code to execute if age is a valid value _____________________________________________________________ _____________________________________________________________ ______________________________________________________________ } c. Complete the following code so that when the code executes the user is given five chances to enter a valid value for age: (4 marks) //file main.C #include “Person.h” int main() { const int MAX_TRIES = 5; Person *person; char name[20]; int age, tryCount = 0; bool validAge = false; cout << “Enter name: “; cin >> name; while ( ________________________________________________________)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the 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 / 7

COS110 Tutorial8-28Sept2011-MEMO - COS110 Tutorial 8 Date:...

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