0% found this document useful (0 votes)
17 views

Assignment3 Tutorial Letter 103 2024

Uploaded by

mxolisi gxuluwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment3 Tutorial Letter 103 2024

Uploaded by

mxolisi gxuluwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tutorial Letter 10/2024

Introduction to Programming II
COS1512
Assessment 3

BARCODE
COS1512/103/2024

ASSESSMENT 3

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 with
single line spacing and convert it to a PDF file before you submit it. See Additional
Resources on MyUnisa for instructions on how to create a PDF file.

Question 1
Consider the following structure used to keep record of a student’s scores:
struct Student
{
string
name; int
quiz1;
int
quiz2;
int
midtermExam;
int finalExam;
}

A student is assessed according to the following policies:


1. The two quizzes are each marked out of 10.
2. The midterm exam and the final exam are each marked out of 100 marks.
3. The final exam counts for 50% of the grade, the midterm counts for 25%, and the
two quizzes together count for a total of 25%. (Do not forget to normalize the quiz
scores. They should be converted to a percentage before they are averaged in.)

Turn the student record into a class type rather than a structure type. The student
record class should have member variables for all the input data. Make all member
variables private. Include public member functions for each of the following:

• a default constructor that sets the student ‘s name to a blank string, and all the scores 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 function that calculates and returns the student’s weighted average
numeric score for the entire course.

Use this class in program which grades a student. The program should read in the student’s
name and scores and output the student’s record as well as the student’s average numeric
score for the entire course. Use the keyboard to supply input and display the output on the
screen. Test your program with the following input:

Student name: Johnny


Applemac Quiz 1: 7
Quiz 2: 5
Midterm exam: 65
Final exam: 73

2
COS1512/103/2024

Question 2 – a bit of theory and terminology

(a) What is the purpose of the keywords public and private in the class declaration?

(b) What is the difference between a class and an object?


(c) What does it mean to “instantiate” an object?
(d) What is the purpose of a constructor?

(e) What is the difference between the default constructor and the overloaded constructor?
(f) What is the purpose of a destructor?
(g) What is the purpose of an accessor?
(h) What is the purpose of a mutator?
(i) What is the purpose of the scope resolution operator?

(j) What is the difference between the scope resolution operator and the dot operator?
(k) What is the difference between a member function and an ordinary function?
(l) What is an abstract data type (ADT)?
(m) How do we create an ADT?

(n) What are the advantages of using ADTs?


(o) What is separate compilation?
(p) What are the advantages of separate compilation?
(q) What is a derived class?
(r) What is the purpose of inheritance?

3
COS1512/103/2024

Question 3
Consider the following class declaration:

class PersonType
{
public:
PersonType();
PersonType(string n, int id, string bd);
private:
string
name; int
ID;
string birthday;
};

Explain what is wrong with the following code fragment and write code to correct it:

int main()
{
PersonType family[20],
newBaby("Anny Dube", 20180912, "2 Sept");
//Assume family has been
initialised for (int i = 0;
i < 20; i++)
if (family.birthday[5] == newBaby.birthday)
cout << family.name[5] << " share a birthday with "
<<newBaby.name;
return 0;
}

Question 4
Consider the following class declaration:

class
Date{
public:
//constructors
Date();
Date(int day, int month, int year);
//accessors
int getDay()
const; int
getMonth() const;
int getYear()
const;

//mutators
void setDay(int day);
void setMonth(int
month); void
setYear(int year);
//operators to calculate next and previous
days Date &operator++();

4
COS1512/103/2024

Date &operator--();
bool operator<(const Date
&d);

private:
//the current day month and year
int theday;
int themonth;
int theyear;
//return the length of current month, taking into
//account leap
years int
monthLength();
};

Implement and test the Date class, taking the following guidelines into account:

a) The default constructor should initialise the date to 14 September 1752.


b) The overloaded constructor should initialise the date with the given day, month and year.
c) The functions getDay(), getMonth() and getYear() should return the current
day, month and year respectively.
d) The functions setDay(), setMonth() and setYear() should change the current
day, month or year to the given value.
e) The operator ++ should advance the date by one, and return the new date.
f) The operator -- should set the date back by one day, and return the new date.
g) The operator < should calculate whether the receiving date object (left
argument) precedes the parameter date (right argument).
For example, Date(1,1,2002) < Date(1,3,2002).
h) The private member function monthLength() should return the length of the current
month, taking into account leap years. A year is a leap year if it is either (i) divisible by
4, but not by 100, or (ii) divisible by 400. In a leap year, February has 29 days,
otherwise it has 28 days.
i) Also overload the insertion operator << to output a date to the screen. For example,
the date in (a) above should be written as: 14 September 1752.

Test the Date class in a C++ program that will do the following:
• Declare a new Date object called d1.
• Display the day, month and year of d1 on the screen.
• Change the date to 28 February 2000.
• Advance this date by one and display the new date on the screen.
• Now change the date to 1 January 2002.
• Set this date back by one and display the new date on the screen.
• Finally change the date to 31 December 2002.
• Advance this date by one and display the new date on the screen.
• Declare a second date object d2(1,1,2003).
• Determine if d1 is earlier than d2 and write the result on the screen.
• Operators ++, -- and < are declared as member functions in the class
declaration above. Implement these operators as friend functions of class
Date also. Run your program twice (each time with a different version of
the overloaded operator ++, -- and <; comment the other versions out during
each run).

5
COS1512/103/2024

Enrichment exercise:

Turn the Date class into an ADT, so that separate files are used for the interface and
implementation. Use separate compilation to compile the implementation separate
from the application program that tests the ADT.
PLEASE NOTE: The enrichment exercises do not form part of the assignment. It is for
practice only.

Question 5

Define a class PhoneCall as an ADT that uses separate files for the interface and the
implementation. This class represents a phone call and has three member variables:

• number, a string that holds the phone number (consisting of 10 digits) to which a call
is placed

• length, an int representing the length of the call in minutes

• rate, a float representing the rate charged per minute.

In addition, the class should contain a default constructor that initializes number to an empty
string, length to 0 and rate to 0. It should also contain an overloaded constructor that
accepts a new phone number and sets length and rate both to 0, as well as a destructor
that does not perform any action.
Include accessor functions that returns the values stored in each of an object of class
PhoneCall’s member variables respectively.

Class PhoneCall also contains a member function calcCharge() to determine the


amount charged for the phone call. Use the following prototype:
float calcCharge();

Overload the equality operator== as a friend function to compare two phone calls. Use the
following prototype:
bool operator==(const PhoneCall & call1, const PhoneCall & call2)

This function returns true if both call1 and call2 have been placed to the same number
and false otherwise.

Overload the stream extraction operator >> (implemented as a friend function) so that it
can be used to input values of type PhoneCall, and the stream insertion << (implemented
as a friend function) so that it can be used to output values of type PhoneCall.

Demonstrate the class in an application program (main()) that is used to determine the total
amount spend on phone calls to a specific phone number in one month. Allow the user to
enter the phone number for which the total amount spent should be determined. Use the
overloaded constructor to initialise the PhoneCall object theCall to the number the user
specified. The PhoneCall objects representing the calls made during one month is stored
in a file MyCalls.dat. Use a while loop to read the phone calls from MyCalls.dat, use
the overloaded equality operator== to compare the phone numbers read from MyCalls.dat
one by one with theCall, and determine the total amount spend on phone calls to theCall,
as well as the number of calls made to this number. Also determine the longest call made to
theCall and display this call together with the total amount spent on calls to theCall, and

6
COS1512/103/2024

the number of calls to theCall.


Test your program with the following data:

Phone calls in file MyCalls.dat:

0123452347 12 3.50
0337698210 9 3.15
0214672341 2 1.75
0337698210 15 3.15
0442389132 8 1.75
0232189726 5 3.50
0124395623 6 3.50
0337698210 2 3.15
0337698210 5 3.15

Phone number to test:


0337698210

Question 6
Overload the stream extraction operator >> for the class Student in Question 1 to read
values for each member variable from a file. Also overload the stream insertion operator
<< to print the record for a student (name, two quiz scores, midterm score and final exam
score) as well as the weighted average for the student either on the screen or to a file.

Use separate compilation and write a program that uses the overloaded extraction operator
>> to read records for students from a file named Student.dat into an array. Assume
that the file will never contain data for more than 20 students. Use the array to determine the
weighted average for each student, as well as the average for all of the students (i.e. the
class average). Display the output on the screen.

Use the following data:


Peter Pan 5 3 45 51
Wendy Hill 7 5 63 58
Alice Mokgaba 8 6 51 67
Precious Petersen 5 7 49 46
Thumi Tebogo 4 7 69 65

Enrichment exercise:
Adapt the application program to use a vector instead of an array. It should not be
necessary to change the class interface or implementation file in any way.

Question 7

Define a class Student with member variables for a student’s name, student number,
address and degree. All of these member variables are strings. Add appropriate constructors
and accessors for class Student and include the following member functions:

• a member function display_info()that overloads the stream insertion operator <<


to display the values of all the member variables of a student.

• a member function calcFee() to calculate the initial registration fee for a student.
For undergraduate students the initial registration fee is R500 and for postgraduate

7
COS1512/103/2024

students the initial registration fee is R600. All undergraduate student degrees begin
with a “B” which will allow you to determine whether a student is an undergraduate or
postgraduate student.

(a) Implement class Student.

(b) Test class Student in a driver program that does the following:

• instantiates an object of class Student, with the following details:name: Mary Mbeli

student number: 12345678


address: Po Box 16, Pretoria, 0818
degree: BSc

• use the accessor functions to display the specifications of the instantiated object
on the console

• display the specifications of the instantiated object on the console with the
member function display_info().

• calculate and display the fee for the student.

(c) Derive and implement a class PostgradStd from class Student. This class has an
additional member variable, dissertation (the title of the Masters of doctorate the
student is doing). Class PostgradStd also has an overloaded constructor and an
accessor member to return the member variable dissertation. The class
PostgradStd should override function display_info() in order to display the
values of all the member variables of PostgradStd. The class PostgradStd should
also override function calcFee() to determine the additional fee for a postgraduate
student which is R12000.

Implement the overloaded constructor for the class PostgradStd by invoking the
base class constructor.

(d) Test class PostgradStd in a driver program that does the following:

• instantiates an object of class PostgradStd, with the following


details: name: Mary Mbeli

student number: 12345678


address: Po Box 16, Pretoria, 0818

degree: PhD
dissertation: How to get a PhD

• use the accessor functions to display the specifications of the instantiated object
on the console

• display the specifications of the instantiated object on the console with the
member function display_info().
calculate and display the outstanding fee for the student.
--- © Unisa 2024 ---

You might also like