Cos1512 Assignment 4 2023
Cos1512 Assignment 4 2023
WEIGHT: 25%
QUIZ AVAILABLE: 26 August 2023
DUE DATE: 18 September 2022
CUT-OFF DATE 18 September 2022
• a part where you write and implement program code (this part) and
• an MCQ part where you answer questions on the code you have written,
and the material covered in this assignment.
The MCQ part of the assignment will be available in the Assessment Shell for
Assignment 4 on the myModules site for COS1512.
You will not be able to do the MCQ part unless you have completed the
coding part.
Question 1
The program below contains an incomplete recursive function raised_to_power().
The function returns the value of the first parameter number of type float raised to
the value of the second parameter power of type int for all values of power greater
than or equal to 0.
The algorithm used in this question to write a recursive function to raise a float
value number to a positive power uses repeated multiplication as follows:
numberpower =1 if power= 0
Open Rubric
and otherwise numberpower can be calculated with the formula:
number x numberpower-1
Question 2
Examine the code fragment below and answer the questions that follow:
1: #include <iostream>
2: using namespace std;
3:
4: //
5:
6: class A
7: {
8: private:
9: int x;
10: protected:
11: int getX();
12: public:
13: void setX();
14: };
15:
16: int A::getX()
17: {
18: return x;
19: }
20:
(d) Class C has public inheritance with the class A. Identify and list class C’s
private, protected and public member variables resulting from the
inheritance.
(e) If class C had protected inheritance with the class A, identify and list class
C’s private, protected and public members variables resulting from the
inheritance.
Question 3
Consider the class definition below and answer the questions that follow:
class InsurancePolicy
{
public:
InsurancePolicy();
InsurancePolicy(int pNr, string pHolder, double aRate);
~InsurancePolicy();
void setPolicy(int pNr, string pHolder, double aRate);
int get_pNr()const;
string get_pHolder()const;
double get_aRate()const;
private:
int policyNr;
string policyHolder;
double annualRate;
};
(b) Code the interface for a class CarInsurance derived from class
InsurancePolicy (the base class). This class has an additional member
variable, excess. Class InsurancePolicy also has member functions,
get_excess() and set_excess()to return the value of member
variable excess and update the value of member variable excess
respectively. The class CarInsurance should override function
showPolicy() in order to display the member variables of
CarInsurance and also override member function setPolicy() in
order to update the member variables of CarInsurance.
(c) Implement the class CarInsurance and use the code below to implement
setPolicy():
void CarInsurance:: setPolicy(int pNr, string pHolder,
double aRate, double eValue)
{
policyNr = pNr;
policyholder = pHolder;
annualRate = aRate;
excess = eValue;
}
(e) Use the following driver program to test your classes InsurancePolicy and
CarInsurance:
#include <iostream>
#include <fstream>
#include "Insurance.h"
#include "CarInsurance.h" using namespace std;
int main()
{
InsurancePolicy myPolicy(123456, "Peter Molema", 3450.67);
CarInsurance yourPolicy(456891, "Wilson Ntemba", 5550.67,
15000.00);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
myPolicy.showPolicy(cout);
cout << endl;
yourPolicy.showPolicy(cout);
cout << endl << "AFTER UPDATES:" << endl;
return 0;
}
Question 4
(a) Write a function called found() to determine whether a specific value occurs
in a vector of integers. The function should receive two parameters: the vector
to be searched (v) and the value to search for (val). The function found()
should return a Boolean value to indicate whether or not val occurs in vector
v.
Test your function found() in a program by declaring a vector and initializing it,
and then call function found() to determine whether a specific value occurs in
the vector.
Question 5
Many application programs use a data structure called a dictionary in which one can
use a key value to retrieve its associated data value. For example, we might want to
associate automobile part numbers with the names of the corresponding parts:
Key Value
100000 tire
100001 wheel
100002 distributor
100003 air filter
Consider the following implementation of the class Dictionary and convert it into a
template class. In other words, re-design the Dictionary interface so that it may be
used to create a Dictionary containing keys and values of any type. For instance,
the value could be of type double, whereas the key could be of type char. Note the
key and value may be most likely of different types hence we need two different
template arguments to be supplied.
Also test your template class by declaring two objects of template class Dictionary
with different template arguments.
Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Dictionary
{
public:
Dictionary();
void add(int key, const string &value);
string find (int key) const;
void display();
private:
vector<int> keys;
vector<string>
values;
};
#endif // DICTIONARY_H
Dictionary.cpp
#include "Dictionary.h"
#include <vector>
#include <iostream>
using namespace std;
Dictionary::Dictionary()
{
//nothing to do, vector member variables are empty on
//declaration
};
void Dictionary::display()
{
for (unsigned int i = 0; i < keys.size(); i++)
cout << keys[i] << ' ' << values[i] << endl;
return;
}
Main.cpp
#include <iostream>
#include <cstdlib>
#include "Dictionary.h"
#include <vector>
using namespace std;
int main()
{
Dictionary parts;
string part;
int key;
parts.display();
cout << endl;
return 0;
}
© Unisa
2023