Assignment3 Tutorial Letter 103 2024
Assignment3 Tutorial Letter 103 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;
}
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:
2
COS1512/103/2024
(a) What is the purpose of the keywords public and private in the class declaration?
(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?
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:
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
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.
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
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
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.
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 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.
(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
• 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().
(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:
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 ---