Beruflich Dokumente
Kultur Dokumente
Introduction to Programming II
COS1512
Semesters 1 & 2
School of Computing
IMPORTANT INFORMATION:
This tutorial letter contains important information
about your module.
CONTENTS
1 INTRODUCTION AND WELCOME .................................................................................................. 4
8 ASSESSMENT ............................................................................................................................... 12
2
COS1512/101
Please note / important note: COS1512 is a semester module. You need AT LEAST eight
hours per week for this module.
If you do not receive your study material immediately after registration, you have to download it
from myUnisa so that you are able to start IMMEDIATELY with your studies. See this tutorial
letter for details about the downloading of study material.
To gain admission to the examination you have to submit Assignment 1 in time. The due
date is 25 February if you are registered for the first semester and 12 August if you are
registered for the second semester.
The COSALLF/301/0/2013 tutorial letter contains important general information that you will
need during the year such as the names and contact details of lecturers assigned to the
different modules.
3
1 INTRODUCTION AND WELCOME
Dear Student
Welcome to COS1512. We hope that you will find this module interesting and stimulating and
that you will increase your knowledge about and your skills in programming in C++. We shall do
our best to make your study of this module successful. You will be well on your way to success
if you start studying early in the semester and resolve to do the assignments properly.
This Tutorial Letter 101 contains important information about the scheme of work, resources
and assignments for this module. We urge you to read it carefully and to keep it at hand when
working through the study material, preparing the assignments, preparing for the examination
and addressing questions to your lecturers.
Please read Tutorial Letter 301 and the my Studies @ Unisa brochure in combination with
Tutorial Letter 101 as it gives you an idea of generally important information when studying at a
distance university and within a particular College.
In Tutorial Letter 101, you will find the assignments and assessment criteria as well as
instructions on the preparation and submission of the assignments. This tutorial letter also
provides all the information you need with regard to the prescribed study material and other
resources and how to obtain it. Please study this information carefully and make sure that you
obtain the prescribed material as soon as possible.
We have also included certain general and administrative information about this module.
Please study this section of the tutorial letter carefully.
Right from the start we would like to point out that you must read all the tutorial letters you
receive during the semester immediately and carefully, as they always contain important and,
sometimes, urgent information.
We hope that you will enjoy this module and wish you all the best!
1.1 Tutorial Matter
The tutorial matter for this module consists of the following:
4
COS1512/101
Please do not contact the School about missing tutorial matter, cancellation of a module,
payments, enquiries about the registration of assignments, and so on, but rather the
relevant department as indicated in the my Studies @ Unisa brochure. The School
should only be contacted about academic matters.
If you have not received all of the above mentioned tutorial matter, please contact our
DESPATCH DEPARTMENT, using the contact details as given in the My Studies @ Unisa
brochure. In the meantime, please download the study material from myUnisa.
All information that is made available electronically in the form of tutorial letters will also be sent
to you on paper (except in a very few rare cases). Note that the determining information comes
from the paper documents that you receive. If there is any conflict re for example, dates, prices,
times, due dates, assignment numbers, et cetera, refer to the information published on paper,
unless otherwise instructed.
Assessment criteria:
• identify all steps necessary to solve a problem and order the steps in the correct logical
sequence;
• write down the logical sequence of operations that a computer should perform to solve
a particular problem;
Assessment criteria:
Through assignments, including multiple choice and written assignments and an examination at
the end of the semester, you are assessed on your ability to:
• use the different C++ programming constructs appropriately and correctly, in order to
implement a solution to a programming problem;
Assessment criteria:
Through assignments, including multiple choice and written assignments and an examination at
the end of the semester, you are assessed on your ability to:
• explain the purpose of a particular C++ programming construct and identify problem
descriptions where they are applicable;
6
COS1512/101
Email: Email is a convenient and the most effective way of communicating with a lecturer.
Students registered for the first semester should send e-mail queries to
COS1512-13-S1@unisa.ac.za
and students registered for the second semester should send e-mail queries to
COS1512-13-S2@unisa.ac.za.
Do not email the lecturer directly - rather use the module email address, as a specific lecturer
may not be available. Using the module email ensures someone will read it. Include the module
code and your student number in the subject header of the message. Ask specific questions. It
is difficult to respond properly via email to a request such as 'I don't understand problem 5.1.
Please explain.' Always state exactly what it is that you do not understand.
Phone us: You are welcome to phone us directly, but please consult your tutorial letters and the
myUnisa discussion forum to ensure that your query has not already been addressed. We are
sometimes unavailable due to other departmental, research or university duties. If you fail to
reach us directly, please phone the secretary. In urgent cases you may also leave a message at
a secretary for us to call you back. The best contact hours are between 9:00 and 13:00 in the
mornings, except Wednesdays when we have our departmental meetings.
3.2 Department
The School of Computing can be contacted telephonically at (012) 429 6122. Should you be
unable to reach any of the lecturers for COS1512, please leave a message with one of the
secretaries, who can be contacted via the number given above. Remember to include the
module code and your student number with the message.
Please note that the School of Computing will be moving to Florida during the course of 2013
and that contact numbers will change. You will be informed in due course.
3.3 University
Lecturers for this module are only responsible for content-related queries about the study
material used for COS1512.
If you need to contact the University about matters not related to the content of this module,
please consult the publication my Studies @ Unisa that you received with your study material.
This brochure contains information on how to contact the University (e.g. to whom you can write
for different queries, important telephone and fax numbers, addresses and details of the times
certain facilities are open).
NB: Always have your student number at hand when you contact the University.
7
Studies @ Unisa. If you have any difficulties with obtaining books from these bookshops, please
contact vospresc@unisa.ac.za.
We will refer to the prescribed book as Savitch.
4.2 Recommended books
You do not have to consult any other textbooks apart from Savitch. However, some of you may
want to read more widely, and consult alternative references. The following useful books are
available in the Unisa library. Please note that the library does not have multiple copies of
these books and that only limited waiting lists are kept.
DS Malik. C++ Programming: From Problem Analysis To Program Design. Course
Technology, Thomson Learning, 2009.
HM Deitel and PJ Deitel. C++ How to Program. 6 th edition. Prentice Hall, 2007.
John R. Hubbard. Programming with C++. 2 nd edition. Schaum’s Outlines, 2000.
4.3 Prescribed software
The prescribed software for this module is Code::Blocks 10.05. We will refer to the software as
Code::Blocks. Code::Blocks includes the MinGW C++ compiler and an Integrated Development
Environment (IDE), which we use to create program files. The prescribed software is provided
on the CD Disk2013 that you should have received in your study package when you registered.
The Disk2013 contains instructions on how to install the software, and how to use the IDE to
write, compile and execute your programs.
These instructions can be accessed from the file index.html on Disk2013. There are a number
of ways to view this file. After inserting Disk2013 into the CD-ROM drive of your computer, do
one of the following:
• Click on Run... on the Start menu. In the dialog box that appears, type d:index.html
and click on the OK button.
• Double-click on the My Computer icon on your desktop. In the window that appears,
double-click on the CD-ROM icon (D:). In the window that appears, double click on the
file index.html.
• Load Windows Explorer and locate index.html on the CD-ROM drive. Double click on
this file.
After doing any one of the above, the file index.html should be loaded into Internet Explorer
(or whatever web browser is installed on your computer). Click on the link for COS1512 and
follow the instructions.
If you did not receive Disk2013 upon registration, you should download the software
immediately from myUnisa so that you are able to start with your studies at once. The
software is available under Additional Resources on the COS1512 webpage.
4.4 E-reserves
There are no e-reserves for this module.
8
COS1512/101
Chapter 8 8.1 and 8.3, plus the subsection Converting Between string Objects and C
Strings, thus excluding 8.2 with the exception of the subsection Converting
Between string Objects and C Strings
Note that some of the sections (in Chapters 1, 4 and 5) are omitted, because they have already
been covered in COS1511. The other sections that are omitted fall outside the scope of this
module.
9
6.2 Planning your academic year
In overview, the undergraduate academic year is as follows:
10
COS1512/101
• Read the corresponding discussion given in the Study Guide, Appendix D in this tutorial
letter.
11
• Scan the chapter in Savitch to get an overview of what the chapter is about.
• Read the chapter again, making sure that you process the information. Relate the text to
the given program listings. You will sometimes have to read a little ahead or read a whole
section to make meaningful sense of a program listing or discussion. Many students
merely read the code and not the accompanying text that explains the code.
• Take the source listing of the sample programs in the textbook, type it into a text file,
compile it and execute it. Observe the output produced. Some of the source listings of
the examples can be found on the CD that you received with your textbook. Appendix B
contains instructions on how to gain access to the source listings.
• Do as many as possible of the self-check questions on a section as you study it. Answers
to the self-check questions are available at the end of each chapter.
• Answer the assignment questions on the chapter. Implement all programming questions
on your computer.
It is important to realise that the process of learning how to program follows a learning curve:
The more programs you write, the more proficient you will become. Remember that COS1512
has a large practical component and that it is essential to gain a lot of programming experience.
Programming modules also require much more time than other modules with no practical work.
You will probably find that you need to work hard and consistently throughout the semester to
develop the necessary programming skills. Plan to spend at least 8 hours per week on this
module.
8 ASSESSMENT
8.1 Assessment plan
Assignments are seen as part of the learning material for this module. As you do the
assignment, study the reading texts, consult other resources, discuss the work with fellow
students or tutors or do research, you are actively engaged in learning. Looking at the
assessment criteria given for each assignment will help you to understand what is required of
you more clearly. The assessment criteria for each assignment correspond to a large extent to
the learning outcomes specified in the Study Guide (Appendix D) for the study material covered
by the assignment.
12
COS1512/101
Two sets of assignments for this year are given at the end of this tutorial letter. The first set of
assignments have to be submitted by students registered for the first semester, and the second
set of assignments have to be submitted by students registered for the second semester. The
tutorial matter you have to master in order to complete each assignment appears in the study
programme in Section 6 and at the start of each assignment. The study guide, Appendix D,
contains details on each section.
Give yourself enough time to do the assignments properly, bearing in mind that a single session
in front of the computer will not be sufficient to complete a programming task. We suggest that
you do the assignment question(s) on a specific chapter as soon as you have studied it. This
will allow you to master the study material and to start timeously with your assignments.
The time constraints under the semester system do not allow us to accept late assignments.
All the assignments require practical work, i.e. programs that you have to implement on your
computer. Submit a printout of each program, as well as the input and corresponding output for
the program. Assignments1 and 2 have to be submitted by the due date. Assignment 3 is for
self-assessment, i.e. you do not have to submit it to Unisa, but will ‘mark’ it yourself by
comparing your attempt with the model solution.
PLEASE ADHERE STRICTLY TO THE FOLLOWING RULES:
• Each assignment has a unique number. Use the correct assignment number and unique
number on your assignment cover, or on myUnisa if you submit your assignments
electronically.
• Do not send assignments directly to any of the lecturers or to the COS1512 e-mail
address.
• Check on myUnisa, or contact the Assignments Section to ensure that your assignment
was received by UNISA.
• Use single spacing for the WORD or pdf document that you submit.
13
We may mark only selected questions in the assignment and not the entire assignment.
However, as mentioned before, we discuss each assignment question in a detailed tutorial letter
that you will receive after the due date.
When we mark assignments, we comment on your answers. Many students make the same
mistakes and consequently we discuss general problems in the solutions to the assignments.
As mentioned before, it is therefore, important to work through these tutorial letters and to make
sure you understand our solutions and where you went wrong.
The marks you obtain for an assignment are converted to a percentage. If you for instance
obtained 25 marks out of a possible 50 marks for Assignment 1, you received 50% for
Assignment 1. For Assignment 1 this percentage in turn contributes a weight of 20% to the year
mark, and for Assignment 2 this percentage contributes a weight of 80% to the year mark.
You are welcome to work in small groups. However, every member of the group must write and
submit his or her own individual assignment. Therefore, discuss the problem, find solutions, etc.
in the group, but then do your own programming and submit your own effort. You will learn to
program only if you sit down in front of the computer, type in the code, debug the program and
get it to work. It is unacceptable for students to submit identical assignments on the basis that
they worked together. That is copying (a form of plagiarism) and none of these assignments will
be marked. It is dishonest to submit the work of someone else as your own, i.e. to commit
plagiarism. Such unethical behaviour does not become our profession.
Assignment assessment and semester mark calculation: Your mark for this module is made
up of a semester mark (10%) and an examination mark (90%). The final semester mark is
calculated based on your performance in assignments throughout the semester. Therefore,
assignments not only give you the opportunity to evaluate your understanding of the materials
covered in the module, but also contribute towards your final mark.
The weights allocated to the assignments for COS1512 are summarized as follows:
1 (compulsory) 20%
2 80%
3 0% (self-assessment)
To explain how this will work, assume that a student receives 75% for assignment 1, and 80%
for assignment 2. His/her year mark will then be calculated as follows:
Total: 0.79
When the total of 0.79 is converted to 10% of the final mark, it will be 7.9%, thus the student’s
semester mark will be 7.9%. The examination will form the remaining 90% of the final mark for
14
COS1512/101
the module. Note that the semester mark will not form part of the final mark for the
supplementary examination.
The following formula will be used to calculate your final semester mark:
Semester mark (out of 100) x 10% + Examination mark (out of 100) x 90%
8.2 General assignment numbers and submission dates
Assignments are numbered consecutively starting from 01 using Arabic numerals. The
assignments are marked and a percentage is awarded according to your achievement. These
assignments have a very important learning function. Please attempt (not necessarily submit) all
assignments, and compare them to the solutions provided.
8.3 Unique assignment numbers
• Go to myUnisa at http://my.unisa.ac.za/.
• Click on the assignment number for the assignment that you want to submit.
16
COS1512/101
8.6 Assignments
FIRST SEMESTER ASSIGNMENTS
ASSIGNMENT 1 (FIRST SEMESTER)
WEIGHT: 20%
EXTENSION: None
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.
WE DO NOT ACCEPT ANY MEMORY STICKS OR CDs.
Question 1
Write a program to determine the tuition fees for a student. The program should use two
overloaded functions, each named calcFees, to determine the tuition fees for a student.
Students who repeat a module pay a different fee for the modules which are repeated. The
program should first ask if the student repeats any modules. If the student repeats, the program
should ask for the number of modules which are repeated.
One of the overloaded functions should accept the number of modules enrolled for the first time
and the fee for those modules as arguments, while the other function accepts arguments for the
number of modules enrolled for the first time and the fee for those modules as well as the
number of modules repeated and the fee for those modules. Both functions should return the
tuition fees for the student.
Question 2
Write a program that calculates the average of a group of five test scores, where the lowest
score in the group is dropped. The program should not accept test scores lower than 0 or higher
than 100. Use the assert function to verify that the test scores entered are legitimate.
17
Question 3
Write a program for your local bank to prepare a statement for a customer’s checking account at
the end of each month. The data is stored in a .dat file in the following format:
46780976 3750.40
W 250.00
D 1200.00
W 75.00
W 375.00
D 1200.00
I 5.50
W 400.00
W 600.00
D 450.50
W 35.65
The first line of code shows the account number followed by the account balance. For each line
of code there is a transaction code and the transaction amount. The transaction codes are as
follows:
W = Withdrawal
D = Deposit
I = Interests
The program has to display the account statement on the console. It should update the balance
after each transaction. During the month, if at any time the balance goes below R 1000, a R25
service fee is charged. The program should print the following information: account number,
opening balance at the beginning of the month, each transaction as it occurs, service fees when
charged, interest paid by the bank and closing balance at the end of the month. Banking costs
(i.e. total of all service fees incurred) are deducted at the end of the month. An example of the
output your program should produce for the input file above, is shown below. Note that a
deposit is shown as a credit (‘Ct’) on the statement:
Sample output:
Account number: 46780976
Opening balance: R3750.40
18
COS1512/101
Question 4
To make phone numbers easier to remember, some companies use letters to show their phone
numbers. For example, using letters, the telephone number 438 5626 can be shown as GET
LOAN. In some cases, to make numbers more meaningful, companies may use more than
seven letters. For example, 225 5466 can be displayed as CALL HOME, which uses eight
letters.
Write a C++ program that reads a list of telephone numbers expressed in capital letters from a
text file and converts the telephone numbers in capital letters, to the corresponding telephone
numbers in digits. Each converted phone number should be displayed on the console in both
formats, alphabetic and digital. At the same time, the program creates another file in which the
telephone numbers appear in digital format, one number per line. Allow the user to specify the
name of the input file, as well as the name of the output file.
Apply the following convention: If a number consists of more than seven letters, only the first
seven letters are processed. A space is displayed between the third and fourth digits. Letters A,
B and C corresponds to digit 2; letters D, E and F to digit 3; letters G, H and I to digit 4; letters J,
K and L to digit 5; letters M, N and O to digit 6; letters P, Q R and S to digit 7; letters T, U and V
to digit 8; and letters W, X, Y and Z to digit 9. If any other character is encountered, a * is
displayed.
The input file contains the following data, with one number per line:
CALL HOME
GET LOAN
Hi THERE
BYE FOR NOW
HELP ME PASS
For example, once the first number in the input file has been processed, the console window
should display the following:
Question 5
(a) What is a pointer?
(b) What is a dereferencing operator?
19
(f) What is the purpose of the delete operator?
i. Define a pointer type int_ptr for pointer variables that contain pointers to int
variables.
iii. Dynamically allocate an integer variable and store its address in p1.
vii. Free the memory allocated to the variable that p1 is pointing to.
i. Define a pointer type int_ptr for pointer variables that contain pointers to int
variables.
iii. Obtain an integer value nrElements from the user indicating the number of
elements to allocate.
iv. Dynamically allocate an array of nrElements integers and store its address in
p2.
vi. Assume p2 has been initialized and copy the elements of p2 one by one to the
corresponding elements in a.
vii. Free the memory allocated to the variable that p2 is pointing to.
20
COS1512/101
EXTENTION: None
WEIGHT: 80%
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.
WE DO NOT ACCEPT ANY MEMORY STICKS OR CDs.
Question 1
Consider the following structure used to keep record of a meeting:
struct Meeting
{
string speaker;
string topic;
string venue;
string date;
}
Turn the meeting struct into a class. The class should have member variables for all the
values in the corresponding struct. Make all member variables private. Include public
member functions for each of the following:
• a default constructor that sets the string data members to blank strings;
21
• 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);
Test the class in a program that instantiates an object of class Meeting (i.e. ‘declare’ an object
of ‘type’ Meeting). The program should then input values for the object (obtained from the
keyboard), and use the mutators to assign values to the member variables. Use the accessors
to obtain the values of the member variables of the object and display those values on the
screen. Test your program with the following input:
(a) What is the purpose of the keywords public and private in the class declaration?
22
COS1512/101
Question 3
3 (a) Consider the following class declaration:
class Employee
{
public:
Employee ();
string getName();
private:
bool citizen();
string lastName;
string firstName;
string employeeNumber;
};
Question 4
Design and implement a C++ class called Module that handles information regarding your
assignments for a specific module. Think of all the things you would want to do with such a
class and write corresponding member functions for your Module class. Your class declaration
should be well-documented so that users will know how to use it.
Write a main program that does the following:
• Declare an array of all your modules. The elements of the array must be of type Module.
• Initialise the array with the modules you are registered for. Initialise each module with the
assignment marks you have obtained for the module.
• Determine your semester mark for each module: the first assignment contributes 30%
and the second assignment 70%.
• Adjust the marks for Assignment 2 for COS1512 with +5%.
• Determine your semester mark for COS1512 again to see what effect the update had.
• Display a list of your semester marks for all the modules you are registered for.
Enrichment exercises:
(a) Turn your Module 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 file that tests the ADT.
(b) 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 5
Define a class Voter as an ADT that uses separate files for the interface and the
implementation. This class represents one voter voting in an election. This class has three
member variables:
24
COS1512/101
In addition, the class should contain a default constructor that initializes ID to an empty string,
nr_times_voted to 0, and voted to false. It should also contain an overloaded constructor
that creates a new voter and sets ID to a specified value, nr_times_voted to 0 and voted to
false. The destructor should not perform any action.
Include accessor functions that returns the values stored in each of an object of class Voter’s
member variables respectively, as well as a mutator function called set_voted() that sets the
voted member variable of a Voter to true.
Overload the prefix increment operator++ (implemented as a friend function) to return the
current instance of the class Voter after incrementing the nr_times_voted by 1. Use the
following prototype:
Voter operator++(Voter& V);
Overload the stream extraction operator >> (implemented as a friend function) so that it can
be used to input values of type Voter. Overload the stream insertion << (implemented as a
friend function) so that it can be used to output values of type Voter.
Demonstrate the class in an application program (main()) that is used to obtain a voter’s ID
from the user, find the voter on the voters’ roll (kept in a file called VotersRoll.dat), check
whether the voter has voted in the current election, and if not, prints a note that allows him/her
to vote. If a voter has already voted, a message should indicate that the voter is not allowed to
vote again. When the user votes, the operator++ is used to increment the number of times
the voter has voted, and the set_voted() member function is used to indicate that the voter
has now voted in the current election.
While the voter’s roll (file VotersRoll.dat) is processed, a new updated voters’ roll (another
file called UpdatedVoters.dat) is created simultaneously. Take care to make sure that all the
voters on the original voters’ roll also appear in the updated voters’ roll.
Test your program with the following data:
VotersRoll.dat:
19810102009 1 0
19792003008 2 0
19851010890 3 1
19900909897 2 0
19561812567 6 0
19682703345 7 1
(b) Test class Computer in a driver program that does the following:
speed: 1600
main memory: 4
hard disk: 16
• 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_specs().
(c) Derive and implement a class Laptop from class Computer. This class has additional
member variables, battery_time and weight. Class Laptop also has an overloaded
constructor and member functions, get_battime() and get_weight()to return
member variables battery_time and weight respectively. The class Laptop should
override function display_specs() in order to display the member variables of Laptop.
Implement the overloaded constructor for the class Laptop by invoking the base class
constructor.
26
COS1512/101
(d) Test class Laptop in a driver program that does the following:
speed: 1333
main memory: 2
hard disk: 8
battery time: 8
weight: 1.25
• use the accessor functions to display the specifications of the instantiated object on
the console.
27
SECOND SEMESTER ASSIGNMENTS
ASSIGNMENT 1 (SECOND SEMESTER)
WEIGHT: 20%
EXTENSION: None
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.
WE DO NOT ACCEPT ANY MEMORY STICKS OR CDs.
Question 1
Write a program to determine the weekly pay for an employee. The program should use two
overloaded functions, each named calcWeeklyPay, to determine the weekly pay for an
employee. Salaried employees receive their annual salary divided by 52 (the number of weeks
per year) per week, while hourly paid employees receive the hourly pay-rate multiplied by the
number of hours worked during the week. One of the overloaded functions should accept the
annual salary as argument, while the other function accepts arguments for the hourly rate and
the number of hours worked during the week. Both functions should return the weekly pay for
the employee.
Question 2
Write a program that converts a date (dd mm) to julian format. The julian format is the day of the
year the ‘dd mm’ date represents, e.g. the julian format of ’27 03’ (i.e. the 27th of March) is 31 +
28 + 27 , i.e. 31 days in January plus 28 days in February plus 27, which gives a julian date of
86. Use the following definition for the number of days in each month:
intdaysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
You can ignore leap years. The program has to ask for 2 dates – read the day and month of
each date separately. It should then convert both dates to their julian format, display them, and
thereafter calculate and display the difference between the two dates in number of days. The
program has to check which is the smaller date before subtracting – the user may enter the
28
COS1512/101
dates in any order. Verify that the dates entered are legitimate dates, using the assert function
and the above defined array, e.g. 31 6 is not a valid date.
Question 3
Two data files are required to evaluate a multiple-choice examination. The first file
(booklet.dat) contains the correct answers. The total number of questions is 50. A sample
file is given below:
ACBAADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD
The second file (answer.dat) contains the students’ answers. Each line has one student
record that contains the following information:
• Student ID
• Student name
A sample file is given below:
AACCBDBC*DBCBDAAABDBCBDBAA*BCBDD*BABDBCDAABDCBDBDA 6555 MAHMUT
CBBDBC*BDBDBDBABABABBBBBABBABBBBD*BBBCBBDBABBBDC** 6448 SINAN
ACB*ADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD 6559 CAGIL
Write a C++ program that counts the total number of correct answers by each student and
outputs this information to another file called report.dat. In this file, the student’s IDs, names
and the scores must be given. Each correct answer is worth 1 point.
For the sample files given above, the output should be as follows:
6555 MAHMUT 10
6448 SINAN 12
6550 CAGIL 49
Question 4
Write a C++ program to generate personalized junk mail. The program takes input both from an
input file and from the keyboard. The input file contains the text of a letter, except that the name
of the recipient is indicated by the three characters #N#. The program asks the user for a name
and then writes the letter to a second file but with the three letters #N# replaced by a name. The
three-letter string #N# may occur any number of times in the letter.
Your program should define a function that is called with the input- and output-file streams as
arguments. The user supplies the names for both the input file and the output file. Use the
following contents for the input file:
29
LetterBody.txt:
Dear #N#,
Now is the time for all good people to come to the aid of the
University.
For if the good people do not come to the aid of the University the
thing may break up and go down the drain. Should this happen, #N#, the
drain could stop up. Once the Fall monsoons come, with the drains
stopped up, we could drown like rats.
Now, #N#, we know you are a good person. Shouldn't you contribute to
the aid of the country by contributing a large sum to your
impoverished professors' welfare?
Sincerely,
Bjorn Berger, Chairman,
The University Committee for Student Contributions to the Betterment
of Faculty Welfare.
Hints:
The program should obtain the names of the input and output files, as well as the name of the
recipient of the letter before it starts to produce the letter. Both the input and output files are text
files, and should be processed character by character.
Have your program read from the input file until it encounters the three characters #N#, and
have it copy what it reads to the output file as it goes. When it encounters the three letters #N#,
it then outputs the name obtained from the keyboard to the output file, instead of the #N#
characters.
Question 5
(b) What is a pointer?
(b) What is a dereferencing operator?
30
COS1512/101
i. Define a pointer type int_ptr for pointer variables that contain pointers to int
variables.
iii. Dynamically allocate an integer variable and store its address in p1.
vii. Free the memory allocated to the variable that p1 is pointing to.
i. Define a pointer type int_ptr for pointer variables that contain pointers to int
variables.
iii. Obtain an integer value nrElements from the user indicating the number of
elements to allocate.
iv. Dynamically allocate an array of nrElements integers and store its address in
p2.
vi. Assume p2 has been initialized and copy the elements of p2 one by one to the
corresponding elements in a.
vii. Free the memory allocated to the variable that p2 is pointing to.
31
ASSIGNMENT 2 (SECOND SEMESTER)
TUTORIAL MATTER:
Chapters 10, 11, 12 and 15 of the Study
Guide (Appendix D)
Chapters 10, 11, 12 (excluding “Creating a
Namespace”) and 15 (only 15.1 “Inheritance
basics”)
Appendices 7 and 8 in Savitch
EXTENTION: None
WEIGHT: 80%
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.
WE DO NOT ACCEPT ANY MEMORY STICKS OR CDs.
Question 1
Consider the following structure used to keep record of an examination:
struct Exam
{
string Module;
string venue;
int numberStudents;
string date;
}
Turn the examination struct into a class. The class should have member variables for all the
values in the corresponding struct. Make all member variables private. Include public
member functions for each of the following:
• a default constructor that sets the string member variables to blank strings, and the
int member variable to 0;
32
COS1512/101
• 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);
Test the class in a program that instantiates an object of class Exam (i.e. ‘declare’ an object of
‘type’ Exam). The program should then input values for the object (obtained from the keyboard),
and use the mutators to assign values to the member variables. Use the accessors to obtain the
values of the member variables of the object and display those values on the screen. Test your
program with the following input:
(a) What is the purpose of the keywords public and private in the class declaration?
Question 3
3 (a) Consider the following class declaration:
class Person
{
public:
Person();
string getName();
private:
bool citizen();
string lastName;
string firstName;
string phoneNumber;
};
34
COS1512/101
bool citizen();
string lastName;
string firstName;
string phoneNumber;
};
Question 4
Design and implement a C++ class called Assignment that handles information regarding one
assignment. Think of all the things you would want to do with such a class and write
corresponding member functions for your Assignment class. Your class declaration should be
well-documented so that users will know how to use it.
In a separate source file, write a main program that does the following:
• Declare an array of all your assignments. The elements of the array must be of type
Assignment.
• Initialise the array with the assignments you have submitted. Initialise each assignment
with the module, and the date on which it was submitted.
• Display a list with the results of all the assignments you have done.
• Adjust the marks for Assignment 2 for COS1512 with +5%.
• Display the list of assignment results again to see the effect of the adjustment.
Enrichment Exercise:
(a) Turn your Assignment 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 file that tests the ADT.
(b) 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 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
35
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 the
number of calls to theCall.
36
COS1512/101
0337698210 5 3.15
Question 6
Consider the following class:
class Package
{
public:
Package(double the_cost, double the_weight,
const string& the_sender, const string& the_receipient);
double calculate_cost()const;//multiply weight by cost_per_kilogram
string get_recipient() const;
string get_sender() const;
protected:
double cost_per_kilogram;
double weight;
private:
string sender;
string recipient;
};
(b) Test class Package in a driver program that does the following:
• use the accessor functions to display the names of the sender and receiver of the
instantiated object on the console.
37
• Use the member function calculate_cost() to determine the cost of the package
represented by the instantiated object. Display the calculated cost also on the
console.
(c) Derive and implement a new class TwoDayPackage that inherits the functionality of the
class Package. TwoDayPackage should redefine calculate_cost(), where a fixed
fee is added to the weight-based cost. For example, a TwoDayPackage that has a weight
of 10kg, a cost of R3.00 per kilogram and charged a fixed fee of R5.00 would cost R35.00
(10*3.00 + 5.00) to deliver. The class should include a member variable to represent the
fixed fee. TwoDayPackage has an additional member function Print(), which outputs
the cost_per_kilogram, weight, sender, recipient and the total cost of delivery of the
package.
(d) Test class TwoDayPackage in a driver program that does the following:
• use the accessor functions to display the names of the sender and receiver of the
instantiated object on the console.
38
COS1512/101
WEIGHT: None
This assignment is for self-assessment. Do not submit this assignment. The solution to this
assignment appears in Appendix C of this tutorial letter.
Question 1
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: {
39
18: return x;
19: }
20:
21: void A::setX()
22: {
23: x=10;
24: }
25:
26: //----------------------------------------------
27: class B
28: {
29: private:
30: int y;
31: protected:
32: A objA;
33: int getY();
34: public:
35: void setY();
37: };
38:
39: void B::setY()
40: {
41: y=24;
42: int a = objA.getX();
43: }
44:
45: //----------------------------------------------
46:
47: class C: public A
48: {
40
COS1512/101
49: protected:
50: int z;
51: public:
52: int getZ();
53: void setZ();
54: };
55:
56: int C::getZ()
57: {
58: return z;
59: }
60:
61: void C::setZ()
62: {
63: z=65;
64: }
Answer the following questions based on the code fragment given above:
(a) Is line 18 a valid access? Justify your answer.
(b) Is line 32 a valid statement? Justify your answer.
(c) Identify another invalid access statement in the code.
(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 2
Consider the class definitions below and answer the questions that follow:
class Date{
public:
friend ostream & operator<<(ostream & cout, const Date & d);
Date(int y, int m, int d);
41
private:
int year, month, day;
};
class Publication {
public:
Publication(const string & p, const Date & d,
const string & t);
Date GetDate( ) const;
string GetPublisher( )const;
string GetTitle() const;
private:
string publisher;
Date date;
string title;
};
(b) Code the interface of a derived class Book for which the Publication class is the
base class. The Book class has two additional member variables representing the ISBN
number and the author of a book. Furthermore, the Book class contains member
functions getISBN( ) and getAuthor( ) that return the ISBN number and the author
respectively. The declaration must also include a constructor for the class Book.
(d) Recode the following interface such that class Magazine, derives from class
Publication:
class Magazine{
public:
Magazine(const string & p, const Date & d, int ipy);
int GetIssuesPerYear( ) const;
Date getDate( ) const;
string getPublisher( )const
string GetTitle() const;
42
COS1512/101
private:
int issuesPerYear;
string publisher;
Date date;
string title;
};
publisher: FisherKing
date: 01/01/2000
title: Global Warming
isbn : 123456789
author: Ann Miller
(v) Write a statement to overload operator<< as a friend function to the class Book
and add the following implementation to your code:
ostream & operator<<(ostream & out, const Book & B)
{
out<<B.title<<endl;
out<<B.publisher<<endl;
out<<B.date<<endl;
out<<B.author<<endl;
43
out<<B.ISBN<<endl;
}
Question 3
Write a function template for a function that has parameters for a partially filled array and for a
value of the base type of the array. If the value is in the partially filled array, then the function
returns the index of the first indexed variable that contains the value. If the value is not in the
array, the function returns -1. The base type of the array is a type parameter. Notice that you
need two parameters to give the partially filled array: one for the array and one for the number
of indexed variables used. Also write a suitable test program to test this function template.
Question 4
Write a template version of a search function for determining whether an array contains a
particular value.
Question 5
Study the Matrix class interface and answer the questions that follow:
(Refer to the Notes at end of the question if you are unfamiliar with Matrices)
template<class Object>
class Matrix
{
public:
Matrix( int row = 0, int col = 0 );
void SetValue(Object value, int r, int c);
Object GetValue( int r, int c) const;
int GetRow() const;
int GetCol() const;
44
COS1512/101
45
//GetRow returns rows
template<class Object>
int Matrix<Object>::GetRow() const
{
//Complete code here
}
46
COS1512/101
Notes:
In mathematics, a matrix (plural matrices) is a rectangular table of numbers or, more generally,
a table consisting of abstract quantities that can be added and multiplied. For example, a 4 by 3
matrix is represented as:
6 6 6
5 3 2
3 1 2
2 7 9
Two matrices can be added if, and only if, they have the same dimensions. (That is, both
matrices have matching numbers of rows and columns.) We define their sum by constructing a
third matrix whose entries are the sum of the corresponding entries of the original two matrices.
For example:
4 3 4 2 2 1 6 5 5
1 2 3 + 1 3 2 = 2 5 5
2 2 1 3 4 5 5 6 6
Question 6
Write a program that inputs two C string variables, first and last, each of which the user
should enter with his or her name. First, convert both C strings to lowercase. Your program
should then create a new C string that contains the full name in pig latin with the first letter
capitalized for the first and last name. The rules to convert a word into pig latin are as follows:
If the first letter is a consonant, move it to the end and add "ay" to the end.
If the first letter is a vowel, add "way to the end.
47
For example, if the user inputs "Erin" for the first name and "Jones" for the last name, then the
program should create a new string with the text "Erinway Onesjay" and print it.
Question 7
(a) Write a sorting function that is similar to Display 7.12 in Chapter 7 in Savitch, except that it
has an argument for a vector of ints rather than an array. This function will not need a
parameter like number_used as in Display 7.12, since a vector can determine the number
used with the member function size(). This sort function will have only this one
parameter, which will be of a vector type. Use the selection sort algorithm (which was
used in Display 7.12).
(b) Write a program that reads in a list of integers into a vector with base type int. Provide
the facility to either read this vector from the keyboard or from a file, at the user's option. If
the user chooses file input, the program should request a file name. The output is to be a
two-column list. The first column is a list of the distinct vector elements; the second
column is a count of the number of occurrences of each element. The list should be sorted
on entries in the first column, largest to smallest. Adapt the sorting function from (a) as
necessary.
Question 8
Write a recursive function that returns the sum of the integers between any two integer numbers
inclusive. For example if we want to calculate the sum of integers between the integer numbers
13 and 17 then the sum will be 13 + 14 + 15 + 16 + 17 = 75. This recursive function will expect
two integer parameters and will return a double.
48
COS1512/101
9 EXAMINATIONS
A 2 hour examination will be scheduled for this module. Please refer to the my Studies @ Unisa
brochure for general examination guidelines and examination preparation guidelines.
49
What if I don't receive my study material or I lose it?
All study material is downloadable from myUnisa web site for COS1512. Please download
electronic copies (PDF files) of tutorial letters and the study guide from Official Study Material,
and the CD from Additional Resources.
May I send my assignment by email?
No. Assignments have to be registered. Students may submit assignments either by post or
Mobile MCQ submission or electronically via myUnisa. Assignments are not accepted via fax or
e-mail.
Have we received your assignment?
If you want to find out whether an assignment has been received by Unisa, marked or returned,
look at the status of your assignment on MyUnisa.
50
COS1512/101
Launch the Project Wizard through File->New->Project. From the pre-configured templates for
various types of projects, select Console application and click Go.
The console application wizard will appear next. Continue through the menus,
selecting C++ when prompted for a language. In the next screen, give the project a name and
type or select a destination folder. As seen below, Code::Blocks will generate the remaining
entries from these two.
51
Finally, the wizard will ask if this project should use the default compiler (normally GCC) and the
two default builds: Debug and Release. All of these settings are fine. Press finish and the
project will be generated. The main window will turn gray, but that is not a problem, the source
file needs only to be opened. In the Projects tab of the Management panel on the left expand
the folders and double click on the source file main.cpp to open it in the editor.
52
COS1512/101
To add the new file to the project, bring up the file template wizard through either File->New-
>File... or Main Toolbar->New file (button)->File...
Select C/C++ source and click Go. Continue through the menus, same as what you have done
before. The last menu will present you with several options. Enter the new filename and location
(as noted, the full path is required). You can browse for the file by clicking the browse button
(see below) to display the file browser window to save the file's location. Checking Add file to
active project will store the filename in the Sources folder of the Projects tab of
the Management panel. Checking any of the build targets will alert Code::Blocks that the file
should be compiled and linked into the selected target(s). click Finish to generate the file.
Browse button
53
The newly created file should open automatically; if it does not, open it by double clicking on its
file in the Projects tab of the Management panel. You can now add code to the new file. Be
careful not to save your files with .c extension (this is not a C++ extension).
Copy an existing file to your project folder or launch a plain text editor (for example Notepad),
and add the following code.
Interface.h
1. #ifndef INTERFACE_H_INCLUDED
2. #define INTERFACE_H_INCLUDED
3.
4. void hello();
5.
6. #endif // INTERFACE_H_INCLUDED
Save this file as a header (Interface.h) in the same directory as the other source files in this
project. Back in Code::Blocks, click Project->Add files... to open a file browser. Here you may
select one or multiple files (using combinations of Ctrl and Shift). (The option Project->Add files
recursively... will search through all the subdirectories in the given folder, selecting the relevant
files for inclusion.) Select Interface.h, and click Open to bring up a dialog requesting to which
build targets the file(s) should belong. For this example, select both targets.
54
COS1512/101
Note: if the current project has only one build target, this dialog will be skipped.
Returning to the main source (main.cpp) include the header file and replace the cout function
to match the new setup of the project.
main.cpp
1. #include "Interface.h"
2.
3. int main()
4. {
5. hello();
6. return0;
7. }
Press Ctrl-F9 or File->Build, or Compiler Toolbar->Build (button - the gear) to compile the
project. If the following output is generated in the build log (in the bottom panel) then all steps
were followed correctly.
-------------- Build: Debug in MyFirst Project ---------------
Compiling: main.cpp
Linking console executable: bin\Debug\MyFirst Project.exe
Output size is 913.10 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
You can now “run” the project by either clicking the Run button or hitting Ctrl-F10.
Note: the option F9 (for build and run) combines these commands, and may be more useful in
some situations.
Removing a file
You can remove a file by simply right-clicking on the file name in the Projects tab of the
Management panel and selecting Remove file from project.
55
Note: removing a file from a project does not physically delete it.
2.1. Printing
To print a C++ program, choose the "Print" option on the "File" menu of Code::Blocks. (If you
are submitting an assignment via myUnisa, you don't need to print. Simply paste the code of
your program into a word processor file.)
Printing the output of a program is somewhat trickier. There are (at least) two ways to print the
output of a text-based program (a console application):
Method 1
To print the text from the I/O window after running your program, you can copy the text to a
word processor (an editor). The steps involved are as follows:
• Position the mouse over the console window (the output window). Right-click, and
choose Mark from the drop-down menu.
• Hold the Shift key down and use the arrow keys to mark (highlight) the text as a block.
• Press the Enter key to copy the highlighted text to the clipboard.
• You can now paste it in a word processor (editor) of your choice and print it.
Method 2
Sometimes the above method can be somewhat laborious and problematic, especially if there is
so much output that it scrolls off the top of the screen. In this case, you can send the output
directly to the printer (while the program is running) like this:
• Run your program, and when it has finished executing (and you are happy with the
output) close the console window.
• Open a separate DOS window (or Command window) and change the directory to where
your program is. (You'll need to type something like cd \unisa\cos1512.)
56
COS1512/101
• Test whether your program is actually in the current directory by typing its name at the
DOS prompt, eg. first.exe followed by <Enter>. If you get the message "Bad command
or filename" you are either in the wrong directory or the name of the executable file is
incorrect. You must be able to run the program from the DOS prompt before proceeding.
• Make sure that your printer is switched on, is "On-line" and has paper in it, etc.
• Press <Ctrl+P> to ensure that all the output generated from now on is sent to the printer.
• Type the name of the executable file, eg. first.exe and press <Enter> to run your
program again.
• When the program terminates, press <Ctrl+P> again to turn off the printing mode. All the
output (and input) of the program should have been sent to the printer.
57
13 APPENDIX B: SOURCE LISTINGS OF THE SAMPLE PROGRAMS IN THE
TEXTBOOK
The source code listings of the sample programs in the 8th edition of the text book can be found
at the Companion Website for the text book. Go to www.pearsinternationaleditions.com/savitch,
select the text book and then click on Companion Website. Click on the Register button, and
type in the student access code found beneath the pull tab of the ONLINE ACCESS card in
front of the text book.
The source code listings of the sample programs in the 7th edition of the text book can be found
at http://www.aw.com/cssupport. Click on S under “Author Search”, and then on “Savitch” in the
resulting list of authors. Follow the link provided for the text book. Now double-click on
“PSCPP6e-SourceCode.zip” and click on Open. A list of folders, one for each chapter, will be
displayed. Each folder contains the source listings for Displays in the chapter. Display 9.06 for
example will be listed as “09-06.cpp”. If you double-click on the file, the source listing will open
up in Code::Blocks.
58
COS1512/101
Question 1
For this question you had to answer questions based on the following code fragment:
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:
21: void A::setX()
22: {
23: x=10;
24: }
25:
26: //----------------------------------------------
27: class B
59
28: {
29: private:
30: int y;
31: protected:
32: A objA;
33: int getY();
34: public:
35: void setY();
37: };
38:
39: void B::setY()
40: {
41: y=24;
42: int a=objA.getX();
43: }
44:
45: //----------------------------------------------
46:
47: class C: public A
48: {
49: protected:
50: int z;
51: public:
52: int getZ();
53: void setZ();
54: };
55:
56: int C::getZ()
57: {
58: return z;
59: }
60
COS1512/101
60:
61: void C::setZ()
62: {
63: z=65;
64: }
Answer the following questions based on the code fragment given above:
(a) Is line 18 a valid access? Justify your answer?
Yes.
The variable x is a private data member of the class A and therefore it can only be
accessed by other member functions and operators of the class A. getX() is a member
function of class A and therefore line 18 is a valid access.
(d) Class C has public inheritance with the class A. Identify and list class C’s private,
protected and public data members resulting from the inheritance.
With public inheritance, the public and protected members of the base class A are
inherited as public and protected members of the derived class C.
Private data members or member functions resulting from the inheritance: None
Protected data members or member functions resulting from the inheritance: getX()
Public data members or member functions resulting from the inheritance: setX()
(e) If class C had protected inheritance with the class A, identify and list class C’s
private, protected and public data members resulting from the inheritance.
With protected inheritance, public and protected members of the base class
become protected members of the derived class.
Private data members or member functions resulting from the inheritance: None
61
Protected data members or member functions resulting from the inheritance: setX()
and getX()
Public data members or member functions resulting from the inheritance: None
Discussion:
When deriving a class from a public base class, public members of the base class become
public members of the derived class and protected members of the base class become
protected members of the derived class. A base class’ private members are never directly
accessible from a derived class, but can be accessed through calls to the public and
protected members of the base class.
When deriving from a protected base class, public and protected members of the base
class become protected members of the derived class. When deriving from a private base
class, public and protected members of the base class become private members of the
derived class. Private and protected inheritance are not “is-a” relationships [Reference:
Study Guide Appendix D, chapter 15].
Question 2
For this question you had to answer questions based on the following code fragment:
class Date
{
public:
friend ostream & operator<<(ostream & cout, const Date & d);
Date(int y, int m, int d);
private:
int year, month, day;
};
class Publication
{
public:
Publication(const string & p, const Date & d,
const string & t);
Date GetDate( ) const;
string GetPublisher( )const;
62
COS1512/101
63
return date;
}
string Publication::GetPublisher()const
{
return publisher;
}
(b) Code the interface of a derived class Book for which the Publication class is the base
class. The Book class has two additional data members representing the ISBN number
and the author of a book. Furthermore, the Book class contains member functions
getISBN( ) and getAuthor( ) that returns the ISBN number and the author
respectively. The declaration must also include a constructor for the class Book.
#ifndef BOOK_H
#define BOOK_H
#include "Publication.h"
#include "Date.h"
#include <string>
64
COS1512/101
private:
string ISBN;
string author;
};
#endif
string Book::getAuthor()const
{
return author;
}
Note, the constructors of the Book class call the Publication constructor (shown in bold) in
their initializer lists. Constructing a derived class object by first constructing its inherited portion
is a standard practice. (For a complete discussion on this subject - refer to pages 851 -853 (8th
ed) of Savitch / pages 865-868 (7th ed) of Savitch, under the section entitled "Constructors in
Derived Classes").
65
(d) Recode the following interface such that class Magazine, derives from class
Publication:
#ifndef MAGAZINE_H
#define MAGAZINE_H
#include "Publication.h"
class Magazine: public Publication
{
public:
Magazine(const string & p, const Date & d, const string & t,
int ipy);
int GetIssuesPerYear( ) const;
private:
int issuesPerYear;
};
#endif
(f) For this question you had to design a driver program to test your classes:
#include <iostream>
#include "Date.h"
#include "Publication.h"
66
COS1512/101
#include "Book.h"
#include "Magazine.h"
return 0;
}
(g) Write a statement to overload operator<< as a friend function to the class Book and
insert the following implementation to your code:
ostream & operator<<(ostream & out, const Book & B)
{
out<<B.title<<endl;
out<<B.publisher<<endl;
67
out<<B.date<<endl;
out<<B.author<<endl;
out<<B.ISBN<<endl;
}
class Publication
{
public:
Publication(const string & p, const Date & d,
const string & t);
Date GetDate( ) const;
68
COS1512/101
#endif
69
Date::Date(int y, int m, int d): year(y), month(m), day(d){}
ostream & operator<<(ostream & out, const Date & d)
{
out<<d.day<<"/"<<d.month<<"/"<<d.year<<endl;
}
#endif
File name:Publication.cpp
#include <iostream>
#include <string>
#include "Date.h"
70
COS1512/101
#include "Publication.h"
using namespace std;
string Publication::GetPublisher()const
{
return publisher;
}
Filename: Book.h
#ifndef BOOK_H
#define BOOK_H
#include "Publication.h"
#include "Date.h"
#include <string>
71
class Book: public Publication
{
public:
friend ostream & operator << (ostream & out, const Book & B);
Book(const string & p, const Date & d, const string & t,
const string & auth,const string & isbn);
string getAuthor()const;
string getISBN()const;
private:
string ISBN;
string author;
};
#endif
Filename: Book.cpp
#include "Book.h"
#include <string>
using namespace std;
string Book::getAuthor()const
{
return author;
}
72
COS1512/101
Filename: Magazine.h
#ifndef MAGAZINE_H
#define MAGAZINE_H
#include "Publication.h"
class Magazine:public Publication {
public:
Magazine(const string & p, const Date & d, const string & t,
int ipy);
int GetIssuesPerYear( ) const;
private:
int issuesPerYear;
};
#endif
Filename: Magazine.cpp
#include "Magazine.h"
73
Magazine::Magazine(const string & p, const Date & d, const string & t,
int ipy):Publication(p,d,t), issuesPerYear(ipy)
{}
int Magazine::GetIssuesPerYear()const
{
return issuesPerYear;
}
Filename: Test.cpp
#include <iostream>
#include "Date.h"
#include "Publication.h"
#include "Book.h"
#include "Magazine.h"
74
COS1512/101
cout<<M.GetPublisher()<<endl;
cout<<M.GetDate()<<endl;
cout<<M.GetIssuesPerYear()<<endl;
return 0;
}
Question 3
Discussion:
For this question, you had to define a function template that searches an array for a specific
value and returns the index of the first occurrence of that value. The template should have
parameters for a partially filled array and for a value of the base type of the array. If the value is
in the partially filled array, then the function returns the index of the first occurrence of that
value, otherwise the function returns -1. The base type of the array is a type parameter.
Function templates are special functions that can operate with generic types. This allows us to
create a function template whose functionality can be adapted to more than one variable type or
class without repeating the code for each type. For instance, with this program we used the
same search() function for an array of doubles (Doubles), an array of characters
(Characters) and an array of integers (Integers). Without templates we would have had to
write a separate search() function for each type of array. (Section 17.1 of Savitch has
detailed explanations on the declarations of function templates.)
A template parameter is a special kind of parameter that can be used to pass a type as a
parameter. The function templates can use these parameters as if they were regular types.
The declaration for the search() function combined template parameters with a defined
parameter type:
template<class T>
int search(T array[], int n, T target)
Be cautious when using template parameters - you cannot apply it without considering all the
implications.
For example:
template<class T>
int search( T array[], T n, T target)
* This does not make sense as we want to run a loop n number of times to search the
array for our target element - n must certainly be of type integer. Hence it does not make sense
within this context to declare n as a template parameter.
75
Program Listing:
#include <iostream>
template<class T>
int search( T array[], int n, T target)
{
for ( int i = 0; i < n; i++ )
{
if (array[i] == target)
return i;
}
return -1;
}
int main(){
char Characters[14] = { 'M', 'A', 'P', 'C','E' };
int Integers[14] = { 1, 4, 3, 5, 3, 6, 8, 9, 10, 7};
double Doubles[14] = {2.99,8.77,4.88,6.44,3.45};
76
COS1512/101
<< endl;
cout << " c found at index "
<< search( Characters,5,'c')
<< " in array of characters"
<< endl;
cout << " 5 found at index "
<< search( Integers, 10, 5)
<< " in array of integers"
<< endl;
cout << " 3.45 found at index "
<< search( Doubles, 10, 3.45)
<< " in array of doubles"
<< endl;
}
Output:
C found at index 3 in array of characters
c found at index -1 in array of characters
5 found at index 3 in array of integers
3.45 found at index 4 in array of doubles
Press any key to continue . . .
Question 4
Discussion:
For this question, you had to specify a template version of a search function to determine
whether an array contains a particular value. Here only two of the parameters had to be
template type parameters, namely the array type and the target (i.e. the element being
searched). As discussed, with the previous question, it does not make sense to alter the other
parameters into template type parameters. We also included a templatized Output() function
to output an array of any type and to call the search() algorithm to further promote reuse.
Program Listing:
#include <iostream>
#include <string>
77
using namespace std;
template<class T>
bool search( T array[], int n, T target)
{
for ( int i = 0; i < n; i++ )
{
if (array[i] == target)
return true;
}
return false;
}
//Precondition: the array base type must have operator<< defined,
//&& n <= declared size of the array argument.
78
COS1512/101
int main()
{
char Characters[14] = { 'M', 'A', 'P', 'C','E' };
int Integers[14] = { 1, 4, 3, 5, 3, 6, 8, 9, 10, 7};
double Doubles[14] = {2.99,8.77,4.88,6.44,3.45};
Output:
Array Characters does contain C
Array Characters does not contain c
Array Integers does contain 5
Array Integers does not contain 15
Array Doubles does contain 3.45
Array Doubles does not contain 3.455
Press any key to continue . . .
Question 5
For this question, you had to implement the operations of a Matrix class template.
79
File name: Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
template<class Object>
class Matrix
{
public:
Matrix( int r = 0, int c = 0 );
void SetValue(Object value, int row, int col);
Object GetValue( int row, int col) const;
int GetRow() const;
int GetCol() const;
void OutPut(ostream & out) const;
private:
vector< vector<Object> > array;
int rows;
int cols;
};
80
COS1512/101
array[r].resize(col);
template<class Object>
int Matrix<Object>::GetRow() const
{
return rows;
}
template<class Object>
int Matrix<Object>::GetCol() const
{
return cols;
}
template<class Object>
Matrix<Object> operator+(const Matrix<Object> & x,
const Matrix<Object> & y)
{
int xrow = x.GetRow();
int xcol = y.GetCol();
assert( xrow == y.GetRow() && xcol == y.GetCol());
Matrix<Object> temp(xrow,xcol);
for (int r = 0; r < xrow; r++)
{
for(int c = 0; c < xcol; c++)
{
Object sum = x.GetValue(r,c) + y.GetValue(r,c);
temp.SetValue(sum, r, c);
}
}
return temp;
}
#endif
82
COS1512/101
(b) For this question you had to write a test program for your Matrix class:
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
Matrix<int> M1(2,2);
Matrix<int> M2(2,2);
M1.SetValue(1,0,0);
M1.SetValue(2,0,1);
M1.SetValue(3,1,0);
M1.SetValue(4,1,1);
M2.SetValue(5,0,0);
M2.SetValue(6,0,1);
M2.SetValue(7,1,0);
M2.SetValue(8,1,1);
Matrix<int> M3(2,2);
M3 = M1 + M2;
M1.OutPut(cout); cout<<endl;
M2.OutPut(cout);cout<<endl;
M3.OutPut(cout);cout<<endl;
return 0;
}
83
Output Produced:
1 2
3 4
5 6
7 8
6 8
10 12
Note:
As shown above - we also have the possibility to write class templates, so that a class can have
members that use template parameters as types. C++ class templates are used where we have
multiple copies of code for different data types with the same logic. If a set of functions or
classes have the same functionality for different data types, they become good candidates for
being written as templates. C++ class templates are ideal for container classes (a class of
objects that is intended to contain other objects). Examples of container classes will be the STL
classes like vector (chapter 8, Savitch), and list. Once the code is written as a C++ class
template, it can support all data types. (See section 17.2, Savitch for full explanations on Class
Templates.) For instance the Matrix template above can accommodate a Matrix of any
type, be it strings, doubles, ints, etc.
Savitch limits his discussion to popular member functions of the STL vector class, such as
push_back, size, capacity, reserve and resize. However there are other member
functions such as:
empty which returns true if the vector is empty and false otherwise
Question 6
Discussion:
In this program we use C-strings to read in the user's first and last name. Both C-strings are
then converted to lowercase before the full name is converted to pig latin.
C-strings use the '\0' character to indicate the end of a C-string. The C-string variables used to
store the first and last names for example are therefore declared as
84
COS1512/101
to allow 20 characters each for the first and last names as well as one position for the '\0'.
In function convertToLowerCase() we use the null character '\0' as sentinel in the while
loop that converts each character to its lower case.
Program listing:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char first[21], last[21], newName[41], copyFirst[21],copyLast[21];
cout << "Please enter your first name: ";
cin >> first;
cout << "Please enter your last name: ";
cin >> last;
//make a copy of the first and last name for output purposes
strcpy(copyFirst, first);
strcpy(copyLast, last);
85
//convert first and last name to lowercase
convertToLowerCase(first);
convertToLowerCase(last);
//convert first and last name to pig latin
pigLatin(first);
pigLatin(last);
//create new string with first and last name in pig latin
strcpy(newName, first);
strcat(newName, " "); //add space between first and last name
strcat(newName, last);
cout << "Dear " << copyFirst << " " << copyLast
<< " in pig latin your name is " << newName << endl;
return 0;
}
strcat(name, "way");
}
else
{
ch = name[0];
for (int i = 0; i <= strlen(name); i++)
name[i] = name[i+1];
name[strlen(name)] = ch;
strcat(name, "ay");
name[0] = toupper(name[0]);
}
}
Question 7
7. (a) Discussion:
We adapted the sorting function to sort a vector from largest to smallest as follows:
The argument was changed from an array of ints to a vector of ints, as can be seen in
the function headings:
Note that in order to return the sorted vector, it should be a reference parameter. Also note that
since the size of a vector can be determined with the member function size(), the parameter
number_used can be omitted from both functions.
We want to sort in descending order while the sorting function in Display 7.12 sorts in ascending
order. Accordingly, both function and local variable names that refer to either 'smallest' or
'min' have been changed to 'largest' or 'max'. See for example function
index_of_largest below:
87
int index_of_largest(const vector<int> v, int start_index)
{
int max = v[start_index],
index_of_max = start_index;
for (int index = start_index + 1; index < v.size(); index++)
if (v[index] > max)
{
max = v[index];
index_of_max = index;
//max is the largest of v[start_index] through v[index]
}
return index_of_max;
}
While these name changes aid in understanding the sorting order of the sorting function, it does
not change the order in which the vector is sorted from ascending to descending. The crucial
change to ensure that the sorting is done in descending order instead of ascending order, lies in
changing the comparison
if (a[index] < min)
in function index_of_largest to
if (v[index] > max)
This change is highlighted in the code section above. The comments have also been adapted
to reflect the changed sorting order.
7. (b) Discussion:
In this question you should have provided a facility to allow the user to specify whether input
should be read from a file, or from the keyboard. Note that when input is read from the
keyboard, we indicate the end of file character with CTRL Z, followed by pressing 'enter':
{ //read input from the console
cout << "Please enter list of values. Press 'enter' "
<< "after each value. Use CTRL Z to end." << endl;
read_vector(list,cin);
}
88
COS1512/101
In function read_vector() inheritance is used so that input can be done both from a file or
from the keyboard. The formal parameter corresponding either to cin (console input) or fin (file
input) therefore has to be of type istream:
void read_vector(vector<int>& v, istream& in_stream);
The distinct elements in the vector are extracted by using a boolean function found() to
determine whether or not a specific element in the original vector (list) occurs in the vector of
distinct elements (distinct). Should a specific element not occur in the vector of distinct
elements, it is added to distinct.
//extract distinct elements in list into vector distinct
vector<int> distinct;
for (unsigned int i = 0; i < list.size(); i++)
{
if (!found(list[i], distinct))
distinct.push_back(list[i]);
}
The vector of distinct elements is then sorted, and a third vector (occurrences) with the same
number of elements as distinct is declared and initialised to 0.
//sort vector distinct
sort(distinct);
A function count() is used to count the number of times each distinct element (stored in vector
distinct), occurs in the original list of elements (vector list).
//count occurrences for each element in vector distinct
for (unsigned int i = 0; i < distinct.size(); i++)
occurrences[i] = count(distinct[i], list);
Finally, the corresponding elements in vectors distinct and occurrences are output next to
each other to show the number of times each distinct element occurs in the original list. We
show output for input from the keyboard as well as for input from a file.
89
Program listing:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
90
COS1512/101
int main()
{
vector<int> list;
fstream fin;
char answer;
string filename;
int next;
cout << "Do you want to provide input via console or "
<< "using a file(c/f)?";
cin >> answer;
cout << endl;
if (answer == 'f') //read input from a file
{
cout << "Please enter filename: " << endl;
cin >> filename;
fin.open(filename.c_str());
if (fin.fail())
{
cout << "Input file opening failed. \n";
exit(1);
}
read_vector(list,fin);
fin.close();
}
else //read input from the console
{
cout << "Please enter list of values. Press 'enter' "
<< "after each value. Use CTRL Z to end." << endl;
read_vector(list,cin);
91
}
//output
cout << endl << 'N' << '\t' << "Count" << endl;
for (unsigned int i = 0; i < distinct.size(); i++)
cout << distinct[i] << '\t' << occurrences[i] << endl;
return 0;
}
92
COS1512/101
void sort(vector<int>& v)
{
int index_of_next_largest;
93
for (unsigned int index = 0; index < v.size() - 1; index++)
{//Place the correct value in v[index]:
index_of_next_largest =
index_of_largest(v, index);
swap_values(v[index], v[index_of_next_largest]);
//v[0] >= v[1] >=...>= v[index] are the largest of the original
//vector elements. The rest of the elements are in the remaining
//positions.
}
}
return index_of_max;
94
COS1512/101
Please enter list of values. Press 'enter' after each value. Use CTRL
Z to end.
-12
3
-12
4
1
1
-12
1
-1
1
2
3
4
2
3
-12
^Z
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
95
Press any key to continue . . .
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
Press any key to continue . . .
Question 8
For this question you had to write a recursive function that returns the sum of the integers
between any two integer numbers inclusive. This recursive function expects two integer
parameters and returns a double.
Program Listing:
#include <iostream>
using namespace std;
96
COS1512/101
int main()
{
cout << "The sum between 13 and 17 is:";
cout << sum (13,17) <<endl;;
cout << "The sum between 13 and 13 is:";
cout<< sum(13,13) <<endl;
cout << "The sum between 13 and 14 is:";
cout<< sum(13,14) <<endl;
return 0;
}
Output:
The sum between 13 and 17 is:75
The sum between 13 and 13 is:13
The sum between 13 and 14 is:27
Press any key to continue . . .
Discussion:
To solve this question, we need to determine the base case and the general case. The base
case is when the solution can be obtained directly.
The base case:
If m is equal to n then we can immediately determine the sum to be m. For example the sum
between 13 and 13 would be 13.
The general case on the other hand, is a little harder to deduce:
Consider the following example:
sum(13,17) = 13 + 14 + 15 + 16 + 17
Recursively:
sum(m,n) = m + sum(m+1,n)
97
The diagram below represents the recursive calls that will take place if 13 and 17 were passed
as parameters to the sum() function. That is, we want to determine the sum between 13 and
17. The diagram shows that each sum(m,n) induces calls to sum(m+1,n). For instance,
sum(13,17) sum(13,17) = 75
since m!=n
return 13 + 62 = 75
step a return 13 + sum (14,17);
sum(14,17) = 62
sum(14,17)
since m!=n
return 14 + 48 = 62
step b return 14 + sum (15,17);
sum(15,17) = 48
sum(15,17)
since m != n
return 15 + 33 = 48
step c return 15 + sum(16,17)
sum(16,17) = 33
sum(16,17)
sum(17,17)
since m = n return 17
step e
return 17
So eventually the recursive calls will stop at sum(17,17) - because an answer of 17 can be
returned.
So sum(15,17) is:
So sum(14,17) is:
98
COS1512/101
More on Recursion:
Understanding recursion is difficult. Let us consider an everyday example. Suppose you were
given a huge bag of coins and you had to determine how much money was in the bag.
As the bag is large, you prefer not to do the work by yourself. However, you have
many willing friends. You divide the bag of coins into two heaps of coins and ask
your friend "Could you please add up this one heap of coins? I've only given you
half, so there's half the work to do”. You, then give the other half to another
friend, and say the same thing. Once both are done, they will give their answer to
you, and you add their results.
Thus, you have broken down the problem into two smaller parts, and asked your friends to do
the work.
Now those friends are smart too, so they divide their heap of coins into two parts (now each has
two heaps of ¼ of the size) and each asks two of their friends to help. When their friends are
done, they return their answer, and the result is summed. Now assume that each of their friends
does the same and enlists more friends to help and this process goes on and on. Eventually,
there is a heap of only two coins, and these are divided once again and given to two more
friends, and those friends, seeing how silly the problem is now, just tell the first friend the only
value on the coin. There's no need to ask any more friends, because you're down to one coin
(this is the base case).
Thus, recursion is all about breaking a problem down, and solving that, and that smaller
problem is solved by breaking it down some more, and trying to solve that. Eventually, you
reach an easy solution (the base case), and return the solution.
99
15 APPENDIX D: STUDY GUIDE FOR COS1512
Contents
Introduction ........................................................................................................................................... ….101
Chapter 4: Procedural Abstraction and Functions That Return a Value .............................................. 106
Chapter 11: Friends, Overloaded Operators, and Arrays in Classes .................................................. 118
100
COS1512/101
Introduction
Welcome to COS1512, an introduction to objects and the object-oriented programming
environment. This study guide accompanies the prescribed book for COS1512:
Walter Savitch. Problem Solving with C++, 8th edition. Pearson Education, Inc, 2012.
We will refer to the prescribed book as Savitch.
Note that this study guide is applicable to both the 7th and 8th edition of Savitch throughout.
Tutorial Letter 101 specifies the sections in Savitch that are covered in COS1512. We repeat
them here for your convenience. Some of the work in the prescribed book is also covered by
COS1511, and therefore only certain sections and chapters are covered in COS1512. The path
that COS1512 follows through the prescribed book can be outlined broadly as follows:
Section 1.2 in chapter 1 provides a general overview over programming and problem-solving
with a brief introduction to object-oriented programming. Though most of chapters 4 and 5 have
been covered in COS1511, section 4.6 (overloading functions) and section 5.5 (the assert
macro) are included to ensure that students have the necessary background knowledge to
understand and implement object-oriented programming. Chapter 6 uses file I/O streams as an
introduction to objects and classes, and teaches students how to use pre-defined classes.
Chapter 8 builds on using pre-defined classes by introducing the standard class string.
Chapter 8 also covers C strings and provides a preview of the Standard Template Library (STL)
with the vector class. Chapter 9 further adds to the background knowledge necessary to
understand and implement classes and objects by discussing pointers and dynamic arrays. In
Chapter 10 students learn how to define their own classes in order to create an abstract data
type (ADT). Chapter 10 also covers inheritance briefly in order to make students aware of this
concept. Chapter 11 continues to teach more techniques for defining functions and operators
for classes. Chapter 12 covers separate compilation, to allow placing the interface and
implementation of an ADT in files separate from each other and separate from the programs
that use the ADT. In Chapter 14 recursion is introduced. In Chapter 15 single inheritance, i.e.
deriving one class from another is covered. In Chapter 17 function and class templates are
covered, which will allow students to understand and use the STL.
The specific learning outcomes and assessment criteria for COS1512 are provided in section 2
of Tutorial Letter 101. These specific learning outcomes translate to learning objectives for the
study material which should be achieved in order to realise the learning outcomes. This study
guide specifies the learning objectives for each section in the prescribed book.
The discussion of each chapter in Savitch given in this tutorial letter typically includes these
subsections:
• Overview
• Learning Objectives.
The Overview indicates the main issues addressed in each chapter. The Learning Objectives
of each chapter are the skills you should acquire when mastering the content of the chapter.
You will be tested on these skills in the examination. These objectives can be achieved by
doing the self-test exercises and the assignments, implementing the examples given in the
textbook and doing some additional exercises.
101
Supplementary information is given where we feel that the explanation given in the prescribed
book is inadequate. Please read these sections carefully, as you may not be able to complete
the assignments without the additional information.
102
COS1512/101
• be aware of what the process of compiling and linking a C++ program entails;
103
1.4 Programming Methodologies
The two most popular programming paradigms are structured procedural programming and
object-oriented programming.
Structured Procedural Programming
Procedural programming focuses on the processes that data undergoes from input until
meaningful output is produced. During design the processing steps are described in an
algorithm. These steps are typically sets of operations executed in sequence after each other.
Structured programming uses a technique called top-down design or step-wise refinement.
This refines or divides a problem into subproblems until eventually each subproblem is
straightforward enough to be solved easily. In the structured procedural approach each
subproblem is normally written as a separate function (or procedure) that is called by a main
controlling function or module. A structured procedural program defines variables and then
calls a series of procedures to input values for the variables, manipulate them and output the
results.
Object-Oriented Programming
In object–oriented programming applications are organised around objects rather than
processes. In the object-oriented paradigm, a system is seen as a collection of interacting
objects that models the interaction between objects necessary to achieve the desired effect. In
object-oriented programming, program components are envisioned as objects that belong to
classes and are similar to real-world objects; the programmer then manipulate the objects and
have them interrelate to achieve a desired result. With object-oriented analysis, design and
programming, the focus is on determining the objects you want to manipulate rather than the
processes or logic required to manipulate the data. Object-oriented programming involves three
fundamental concepts: encapsulation, inheritance and polymorphism.
Writing object-oriented programs involves creating classes, creating objects from those classes,
and creating applications, i.e. stand-alone executable programs, that use those objects. If they
are designed properly, classes can be reused over and over again to develop new programs.
An object generally represents a concrete entity in the real world such as a person, place or
thing. Such an object has certain characteristics or attributes and certain behaviours or actions.
An object’s characteristics represent data or facts that we know about the object. This is
sometimes called the state of the object. The object’s behaviours are actions that the object
can take. These actions are expressed in functions or methods that execute algorithms. Each
object plays a specific role in the program design. The object interacts with other objects
through messages or function calls. Attributes and behaviours, i.e. an object’s data and
algorithms, can be encapsulated, or combined together to form a class.
A class is like a template or blueprint that can be used to construct or instantiate many similar
objects. A class can also be seen as a sort of user-defined data type, which we call an abstract
data type (ADT). An object is an instance of a particular class. A class hides information by
restricting access to it. In effect the data and methods within an object are hidden from users to
prevent inadvertent changes. Users of the class simply need to understand how to use the
interface or interaction between the object and its methods.
Through inheritance it is possible to define subclasses that share some or all of the parent
class’s attributes and methods, but with more specific features. This enables reuse of code.
Polymorphism describes the feature in languages that allows us to interpret the same word
differently in different situations based on the context in which it is used. In object-oriented
104
COS1512/101
programs this means that similar objects can respond to the same function call or message in
different ways.
105
Chapter 4: Procedural Abstraction and Functions That Return a Value
Savitch: Section 4.6 Overloading Function Names
4.1 Overview
In this chapter both predefined (library) functions and programmer-defined functions are
discussed. This includes the use of value parameters, function invocation, procedural
abstraction, and local and global scope. These concepts are covered in COS1511, but in
section 4.6, overloading function names, which is not covered in COS1511, is explained.
4.2 Learning Objectives
After having worked through section 4.6, you should be able to:
• Function definitions may be given either before or after the main( ) function. However, if
a function definition is given after the main( ) function, the function prototype must be
shown before the main( ) function.
• Savitch uses the term arguments for the parameters listed in parentheses after the function
name in a function call, while in COS1511 they were called actual parameters. See the
explanation box Parameters and Arguments’ in section 5.2 in chapter 5 in Savitch for an
explanation of the different terms that have to do with parameters and arguments.
106
COS1512/101
This chapter completes the description of C++ functions by discussing void functions and
reference parameters. All of these concepts are covered in COS1511. It also discusses testing
and debugging functions. In this regard we cover the assert macro in section 5.5.
• Use the assert macro to ensure that an expected condition is true at the location of the
macro.
5.3 Notes
1. Study section 5.5.
2. Do the self-test exercises for section 5.5.
3. If necessary, work through the rest of the chapter to refresh your memory.
107
Chapter 6: I/O Streams as an Introduction to Objects and Classes
Savitch: Sections 6.1 to 6.3
6.1 Overview
This chapter explains how to accept input from a file and send output to a file by means of
streams. In the process of explaining streams, the basic ideas of what objects are and how to
use them in a program, is introduced.
The iostream header file is vitally important because it provides a consistent interface to all
input/output in C++: screen, keyboard and file. Furthermore, all input/output devices can be
accessed in C++ through streams (using operator overloading.) This input/output header file
also provides access to a set of input/output routines that can be used to output all the primitive
(i.e. built-in) C++ types, and can be overloaded by the user to output any user defined type.
(Operator overloading is covered in Chapter 11.)
6.2 Learning Objectives
After having worked through sections 6.1 to 6.3, you should be able to:
• write programs that takes input from files (reading from files);
• manage character I/O correctly using the get and put member functions;
• demonstrate that you understand the concepts of a class, an object, a member function
among stream classes;
108
COS1512/101
In Display 6.1 the input-file stream variable in_stream is declared with the statement
ifstream in_stream;
The statement:
in_stream.open("infile.dat");
connects the file infile.dat (created as described above) to the input-file stream variable by
means of the open member function. If the input file and the program file have not been saved
in the same directory, the full path name for the input file should be supplied. In this example
infile.dat is the external file name, and in_stream the stream name used as the name of the
file in the program.
Similarly, in Display 6.1 the output is written to the file outfile.dat. Output files are created by
the program. The output file outfile.dat will therefore be created by the program. We can read
the content of the file using a text editor, such as the DevC++ editor.
6.5 Classes and Objects
A class is a data type whose variables are objects, and an object is a variable that has member
functions as well as the ability to hold data values in its member variables. Objects are the
basic units of programming in object-oriented programming languages like C++. The attributes
of an object are described through the values stored in its member variables, while the member
functions are used to execute the operations that can be performed on the object (its
behaviour).
6.6 File Names as Input
Giving a file name as input is discussed in section 6.1 in Savitch. Note that the declaration for a
variable to hold the file name
char file_name[16];
in the subsection ‘File names as input’ in section 6.1 declares a C-string variable, and not a
variable of the standard string class. In COS1511 the standard string class was covered,
and both types of string variables are covered in chapter 8 in Savitch.
One can also use a variable of the standard string class to hold the name of a file given as
input, as shown below. Note that in this case when the name of the file is supplied as argument
to the member function open, the string variable must first be converted to a C-string with the
c_str()member function of the string class. The string member function c_str()
converts a standard string to a character array.
string file_name;
ifstream in_stream;
cout << "Enter the name of the file to be processed: ";
cin >> file_name;
in_stream.open(file_name.c_str())
109
6.7 Header files
For keyboard/screen input/output you need to use #include <iostream>. Similarly, for file
input/output you need to use #include <fstream>. The fstream class inherits from the
iostream class, so most header files are arranged so that to #include<fstream> is
sufficient to get the iostream header file included as well. Note, however, that this may not be
the case for all compilers.
6.8 Processing data files versus processing text files
Files from which data should be read as strings, ints, floats or a mixture of data types,
e.g. ints and chars, are called data files. Files which should be read and processed character
by character, are called text files. The way in which we read data files differ from the way in
which we read text files.
The typical way to read a data file, i.e. a file containing values that should be processed as int,
string or float, is demonstrated below. Assume we have to process a data file called
infile containing one int followed by a string on each line:
ifstream infile;
infile1.open("InputFile.dat");
if (!infile)
{
cout << "Cannot open file "
<< "InputFile.dat" << " Aborting!" << endl;
exit(1);
}
int value;
string name;
while (infile>> value >> name) //this checks for the eof marker of
// infile before reading the data from the file
{
//process value and name
}
infile.close();
Processing the input file as a text file, i.e. a file consisting of characters which should be
processed one by one, is demonstrated below:
ifstream infile;
infile1.open("InputFile.txt");
if (!infile)
{
cout << "Cannot open file "
<< "InputFile.dat" << " Aborting!" << endl;
exit(1);
}
110
COS1512/101
char ch;
infile.get(ch);
while (!infile.eof()) //explicit testing for eof of infile
{
//process ch
infile.get(ch);
}
infile.close();
We can extract an object from a file of objects in the same way that we process a data file, e.g.:
Assume the following class definition:
class Student
{
public:
Student();
string getName() const;
int getMark() const;
private:
string name;
string mark;
}
Then the following code fragment will process a file of objects of class Student
(InputFile.dat):
ifstream infile;
infile1.open("InputFile.dat");
if (!infile)
{
cout << "Cannot open file "
<< "InputFile.dat" << " Aborting!" << endl;
exit(1);
}
Student aStdt;
while (infile>> aStdt) //this checks for the eof marker
// of infile before extracting aStdt (one object)
//from the file
111
{
//process object aStdt
}
infile.close();
112
COS1512/101
The class vector is defined in the Standard Template Library (STL) for C++. The STL vector
container is a generalization of array. A vector is a container that is able to grow (and shrink)
during program execution. A container is an object that can contain other objects. The STL
vector container is implemented using a template class (templates are covered in Chapter 17).
113
type. However, they have several other notable features not associated with the basic array.
The main advantages of using vectors is that the size of a vector need not be fixed and can be
changed during program execution, which is not possible with an array. Vectors are also
important when the ability to rapidly access arbitrary elements is necessary, because, as with
an array, individual elements in a vector can be directly indexed. The elements in a vector are
assumed to be unsequenced, but the operation sort in the STL can be used to place the
elements of a vector in order.
8.6 The Standard Template Library (STL)
The Standard Template Library (STL) standardises commonly used components through a set
of containers (lists, vectors, sets, maps, etc.) that hold objects, built-in objects and class objects.
The containers keep the objects secure and define a standard interface through which they can
be manipulated. The STL also contains standard algorithms that can be applied to the objects
while they are in the container, for instance, to sort them, compare them, merge objects into
another container, find a specific object and many more operations. The STL algorithms use
iterators, which are similar to pointers, to operate on the objects in the containers. The
algorithms manipulate the objects in a standard way and the iterators ensure that the algorithms
handle all and only the elements in the containers, and no more. You do not need to be able to
use the iterator access methods for vectors, though more information on using iterators can be
found in Chapter 18.
114
COS1512/101
• demonstrate that you understand the difference between automatic (ordinary) variables and
dynamic variables;
• demonstrate that you understand the difference between static arrays and dynamic arrays.
9.3 Notes
1. Study sections 9.1 and 9.2 excluding the optional sections on Pointer Arithmetic and
Multidimensional Dynamic Arrays.
2. Do the self-test exercises for sections 9.1 and 9.2.
3. We do not cover the subsections on Pointer Arithmetic and Multidimensional Dynamic
Arrays.
115
Chapter 10: Defining Classes
Savitch: Sections 10.1 to 10.4
10.1 Overview
This chapter teaches you how to define your own classes. A class is a data type whose
variables are objects. You have already used some pre-defined data types such as int,
char, string and ifstream. The chapter first introduces structures as a first step toward
defining classes. A structure (struct) can be viewed as kind of a simplified class without
member functions.
10.2 Learning Objectives
After having worked through sections 10.1 to 10.4 you should be able to:
A const member function promises not to change any class data that it uses. A use of const
in a class object declaration promises that the function won’t call any other function with the
potential to change that object’s data members. The compiler tries to enforce these promises.
Use of the const keyword requires consistency. As shown in the BankAccount example
below, if you declare a member function const in a class definition, you must use the const
keyword in your definition of that member function. If you use a const object for an argument,
and do not declare the function with a const parameter, you should get an error or a warning
from your compiler.
116
COS1512/101
To implement this in the class definition for the BankAccount class in Display 10.5:
(a) Change the function prototypes for the accessor functions get_balance( ) in line 17
and
(b) Change the headings of the member function definitions for get_balance( ) in line 85
and get_rate( ) in line 90 to:
double BankAccount::get_balance( ) const
double BankAccount::get_rate( ) const
In this way the compiler will give an error message if any statement in the body of either
get_balance( ) or get_rate( )attempts to change a data member. The section on the
const parameter modifier in section 11.1 in chapter 11 further explains this concept.
117
Chapter 11: Friends, Overloaded Operators, and Arrays in Classes
Savitch: Sections 11.1 to 11.4
• define and use functions to overload both binary and unary operators;
• declare and manipulate arrays of classes as well as classes that contains arrays as data
members;
• write destructors;
3. Note the use of the const modifier as explained in section 11.1. We use this trick to
prevent accessor functions from modifying data members.
4. Class definitions typically include a destructor.
11.4 Overloading operators as member functions
Appendix 8 explains how to overload operators as member functions. In Appendix 8, Savitch
explains “When + is overloaded as a member operator, then in the expression cost + tax,
the variable cost is the calling object and tax is the argument to +.” This means that the
compiler translates the expression cost + tax into the expression cost.operator+(tax).
This clearly shows that the function operator+ has only one parameter, as is shown in
Appendix 8 in Savitch.
The general form of functions to overload the binary operators as member functions of a class
follows:
118
COS1512/101
where # stands for the binary operator, arithmetic (e.g. +, -, *, /) or relational (e.g. ==, !=, >, >=,
<, <=, &&, ||) to be overloaded; returnType is the type of value returned by the function; and
className is the name of the class for which the operator is being overloaded.
Function definition:
returnType className::operator#(const className& otherObject) const
{
//Algorithm to perform the overloading operation
return value;
}
Compare this with the general syntax to overload binary operators as non-member (friend)
functions:
Function prototype (to be included in the definition of the class):
friend returnType operator#(const className&, const className&) const;
where # stands for the binary operator, arithmetic or relational, to be overloaded; returnType
is the type of value returned by the function; and className is the name of the class for which
the operator is being overloaded.
Function definition:
returnType operator#(const className& firstObject,
const className& secondObject) const
{
//Algorithm to perform the overloading operation
return value;
}
Appendix 7 covers the this pointer. As an additional example we show how the this pointer
can be used in overloading the unary operators ++ and – in prefix position as member functions.
(Note that overloading ++ and – in postfix position differs from what we show here!)
The process of overloading a unary operator is similar to that of overloading binary operators.
The only difference is that in the case of binary operators, the operator has two operands. In the
case of unary operators, the operator has only one operand. So, to overload a unary operator
for a class, note the following:
1. If the operator function is a member of the class, it has no parameters.
2. If the operator function is a non-member of the class (i.e. a friend function), it has one
parameter.
119
We use the class Money from chapter 11, and overload the pre-increment operator ++. We first
show how to overload the pre-increment operator ++ as a friend function and then as a
member function.
The general syntax to overload pre-increment operator ++ as a non-member function in prefix
position can be described as follows:
Function prototype (to be included in the definition of the class):
friend className operator++(className& incObject);
Function definition:
className operator++(className& incObject)
{
//increment object by 1
return incObject;
}
Following this general syntax, we can overload the increment operator++ in prefix position as a
non-member function as follows:
Function prototype (to be included in the definition of the class):
friend Money operator++(Money & M);
Function definition:
Money operator++(Money & M)
{
++M.all_cents;
return M;
}
The general syntax to overload the pre-increment operator ++ as a member function in prefix
position can be described as follows:
Function prototype (to be included in the definition of the class):
className operator++();
Function definition:
className className:: operator++()
{
//increment object by 1
return *this;
}
Following this general syntax, we can overload the increment operator++ in prefix position as a
member function as follows:
120
COS1512/101
Function definition:
Money Money::operator++()
{
++all_cents;
return*this;
}
In C++, three special functions are provided automatically for every class: destructor, copy
constructor and operator =. In many cases the default behaviour provided by the compiler
can be accepted. Sometimes, however, this cannot be done. This is typically the situation for
classes with pointer data members. For these classes, three things are normally done:
1. Include the destructor in the class.
Specification (IntCell.h):
#ifndef INTCELL_H
#define INTCELL_H
class IntCell
{
public:
IntCell (int Value = 0); //constructor
int get_Value( ) const; //accessor
void set_Value(int x); //mutator
private:
int Value;
};
#endif
121
Implementation (IntCell.cpp):
#include "IntCell.h"
void IntCell::set_Value(int x)
{
Value = x;
}
11.6.1 Destructor
A destructor is a member function that is called automatically when an object of the class goes
out of scope at the end of a function, or is deleted explicitly by a delete statement.
Destructors can do any “housekeeping” necessary, such as to delete all dynamic variables
created by the object. Just as the compiler creates a default constructor automatically if no
constructor has been specified for the class, the compiler creates a destructor automatically if
one is not included in the class definition. A class has only one destructor with no arguments.
The name of the destructor is distinguished from the no-parameter constructor by the tilde
symbol ~.
To include a destructor for the class IntCell, we add ~IntCell as a member function to the
class definition (specification). The implementation for the destructor function ~IntCell, is as
follows:
~IntCell ( )
{ //nothing to be done since IntCell contains only an int data
// member.If IntCell contained any class objects as member
// variables their destructors would be called
};
• When an object is declared and initialised by using the value of another object, such as
IntCell B = C;
122
COS1512/101
IntCell B(C);
but not
B = C; //Assignment operator
• When, as a parameter, an object is passed using call by value (instead of by & or const
&)
The operator =
The copy assignment operator, operator =, is used to copy objects. It is called when = is
applied to two objects after both have been previously constructed. The expression lhs =
rhs is intended to copy the state (i.e. the values of the data members) of rhs into lhs. By
default the operator = is implemented by applying operator = to each data member in turn.
As mentioned in the last paragraph of chapter 11 in Savitch, to make sure that the object is not
copied to itself, an alias test is done with
123
if (this != &rhs) //standard alias test to avoid self-assignment
An additional keyword in C++, the pointer this points at the current object. We can think of the
pointer this as a homing device that, at any instant in time, tells us where we are. (Also see
Appendix 7 in Savitch for more on this). The pointer this thus refers to the address of the
current object, while the unary address operator & in combination with rhs, as in &rhs, refers to
the address in memory where rhs is stored. If these two addresses are the same, i.e (this
== &rhs), the same object appears on both sides of the assignment operator and the
operation should not be executed.
If the object is not copying to itself, the operator= is applied to each data member. IntCell
has only one data member Value, so only one statement is needed:
Value = rhs.Value;
*this is the current object, and returning *this will return a reference to the current object.
This allows assignments to be chained, as in a = b = c.
If a class consists of data members that are exclusively primitive types (such as int, double,
vector<int>, string, etc.) or objects for which the defaults make sense, the defaults can be
accepted. However, when a class contains a data member that is a pointer, the defaults are no
longer sufficient.
Suppose that the class contains a single data member that is a pointer. This pointer points at a
dynamically allocated object. If we adapt the specification for class IntCell so that its single
member is a pointer to an integer, the specification and implementation changes to what is
shown below. Note that the default destructor, copy constructor and assignment operator will
be used since we do not define our own:
Specification (IntCell.h):
#ifndef INTCELL_H
#define INTCELL_H
class IntCell
{
public:
IntCell (int Value = 0); //constructor
int get_Value( ) const; //accessor
void set_Value(int x); //mutator
private:
int *Value;
};
#endif
124
COS1512/101
Implementation (IntCell.cpp):
#include "IntCell.h"
IntCell::IntCell(int InitialValue)
{
Value = new int(InitialValue);
}
void IntCell::set_Value(int x)
{
*Value = x;
}
The output from the application below in which class IntCell is used, exposes problems with
accepting the default destructor, copy constructor and assignment operator:
#include <iostream>
#include "IntCell.h"
using namespace std;
int main() {
IntCell a(2);
IntCell b = a;
IntCell c;
c = b;
a.set_Value(4);
cout << a.get_Value() << endl << b.get_Value() << endl
<< c.get_Value()
<< endl;
return 0;
}
Output:
4
4
4
Press any key to continue . . .
125
The output produces three 4s, even though logically only a should be 4. The problem is that
the default copy constructor and the default operator= both copy not the objects being
pointed at, but simply the value of the pointer Value. Thus a.Value, b.Value and c.Value
all point at the same int value. This is called shallow copying: i.e. the pointer rather than the
contents of the address to which the pointer is pointing, is copied. Typically, we would expect a
deep copy, in which a clone of the entire object is made.
A second less obvious problem is a memory leak: the int initially allocated by a’s constructor
remains allocated and needs to be reclaimed. The int allocated by c’s constructor is no longer
referenced by any pointer variable. It also needs to be reclaimed, but we no longer have a
pointer to it. This is because the default destructor does nothing. We have to call delete
ourselves. To fix these problems, we implement the Big Three, as shown below:
Specification (IntCell.h):
#ifndef INTCELL_H
#define INTCELL_H
class IntCell
{
public:
IntCell (int Value = 0); //constructor
private:
int *Value;
};
#endif
Implementation (IntCell.cpp):
#include "IntCell.h"
IntCell::~IntCell( ) //destructor
{
delete Value;
}
Running the same application as before, now gives the correct output, as shown below:
Output:
4
2
2
Press any key to continue . . .
To summarize, when a class has pointer members and a constructor allocates memory from the
free store you need a copy constructor to guarantee correct initialization of an object of that
class type from another object of that class type. And you also need a destructor to guarantee
that free store memory pointed to by members are deallocated when the class object goes out
of scope. If you assign objects of such a class to one another, then you need to overload the
operator = to guarantee that the data pointed to by objects’ pointers are copied, and not just the
pointers.
Note that defining operator = has no effect on the behavior of the class objects during copy
construction. Similarly, defining a copy constructor does nothing for the assignment operator.
Operator = is invoked when assignment is made to a class object. The destructor is called
when an automatic object goes out of scope, and when the destructor is called explicitly.
127
Chapter 12: Separate Compilation and Namespaces
Savitch: Sections 12.1 and the first part of section 12.2 ‘Creating a namespace’
12.1 Overview
This chapter teaches you how to divide an ADT into separate specification and implementation
files. It also discusses how to implement separate compilation as well as conditional
compilation using pre-processor directives. This chapter also discusses namespaces. Creating
your own namespaces is beyond the scope of this module, but you do have to understand the
concept of a namespace.
12.2 Learning Objectives
After having worked through section 12.1 and the first part of section 12.2 you should be able
to:
• inclusion of other files in the file being compiled (by means of file inclusion directives),
128
COS1512/101
ll preprocessor directives begin with #, and only whitespace characters may appear before a
preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do
not end in a semicolon (;).
The #include preprocessor directive causes a copy of a specified file to be included in place
of the directive. The two forms of the file inclusion directive (#include <filename> and
#include "filename") indicate where (which directory) the preprocessor should find the file
to be included.
The < and > characters tells the compiler to look where the system stores predefined header
files.
The " and " characters usually cause the compiler to look in the current directory for the
header file. If the filename is given in terms of an absolute file system pathname, then the file is
taken from that absolute location. For example, the following directive indicates that the file
sample.h can be found in the directory \example\source on the C: drive.
#include "C:\example\source\sample.h"
Once the preprocessing has been done, the compiler translates the source code into object
code, stored in a .o file. The linker then links the object code with the code from libraries for
functions that are included (with #include) to create the executable code, stored in a .exe
file.
129
Chapter 14: Recursion
Savitch: Sections 14.1 to 14.2
14.1 Overview
This chapter introduces recursion. Recursion is a difficult topic to grasp. However, it’s very
easy to apply once you understand it. Recursion means allowing a function or procedure to call
itself. It keeps calling itself until some limit is reached.
14.2 Learning Objectives
After having worked through sections 14.1 to 14.2 you should be able to:
n n-1
∑i = ∑i + n
i=1 i=1
130
COS1512/101
answer becomes 6.
Recursion works backward until a given point is reached at which an answer is defined (the
base case), and then works forward with that definition, solving the other definitions which rely
upon that one.
All recursive procedures/functions should have some sort of test to stop the recursion. Under
one condition, called the base condition, the recursion should stop. Under all other conditions,
the recursion should go deeper. In the example above, the base condition is if (num == 1).
If you don’t build in a base condition, the recursion will either not take place at all, or become
infinite.
131
Chapter 15: Inheritance
Savitch: Section 15.1
15.1 Overview
This chapter introduces the concept of inheritance, a key feature of object-oriented
programming. Inheritance is the ability to define new classes from existing classes. The new
classes that we create from the existing classes are called derived classes; the existing classes
derived are called the base classes. The derived classes inherit the properties of the base
classes. So rather than create completely new classes from scratch, we take advantage of
inheritance and reduce software complexity. We only cover section 15.1: Inheritance Basics.
Polymorphism is not covered in this module.
15.2 Learning Objectives
After having worked through section 15.1 you should be able to:
• understand the effect of the access-specifier labels (i.e private, public and protected) in
terms of public inheritance.
• demonstrate that you understand the difference between redefining a member function in a
class and overloading a member function.
15.3 Notes
1. Study section 15.1.
2. You can safely ignore all references to namespaces other than std. In Display 15.1, for
example, lines 9 and 28 can be omitted, and the same applies to the other displays in this
chapter.
3. Do the self-test exercises for section 15.1.
15.3 Inheritance
15.3.1 The Purpose of Inheritance
The primary purpose of inheritance is to reuse code by exploiting the ‘is-a’ relationship between
objects. Software reusability saves time in program development. It encourages the reuse of
proven and debugged high-quality software, thus reducing problems after a system becomes
functional. Significant overlap between two classes indicates a potential case for inheritance.
132
COS1512/101
We see that inheritance creates a new class, called the derived class, that is an extension,
specialization or modification of one or more existing classes, called the base classes. In the
situation where there is only one base class, we have single inheritance. Where there is more
than one base class, multiple inheritance occurs. In this, example, we could illustrate multiple
inheritance by defining
class Emergency {/* . . . */};
and have had a class PoliceCar inherit from both class Car and class Emergency. Or we
could have a class Ambulance inherit from both class Truck and class Emergency. The
PoliceCar and Ambulance would inherit features from Car and the derived class adds its
distinguishing features. Since multiple inheritance is beyond the scope of this module, we will
not discuss it further.
Notice that the ‘is-a’ relationship is transitive (that is, a TractorTrailer is a Truck, a Truck
is a Vehicle, and therefore a TractorTrailer is a Vehicle), but it is not reflexive (that is,
not all Vehicles are TractorTrailers). Another way of expressing the ‘is-a’ relationship is
to say that a TractorTrailer is a kind of Truck and that an Truck is a kind of Vehicle.
See also the text box in section 15.1 in Savitch on ‘An Object of a Derived Class Has More
Than One Type’.
133
In addition to the ‘is-a’ relationship that represents inheritance, two other relationships between
abstractions are commonly used on object-oriented design, namely the ‘has-a’ and ‘uses-a’
relationships. The ‘has-a’ relationship says that some object is part of another. The ‘has-a’
relationship implies containment. For example, a car has an engine. The ‘uses-a’ relationship
says that one object uses another object in some way. This usually realized by one object
communicating with another via member functions. For example, suppose the operating system
has a clock object that maintains the current date and time. The clock object has member
functions that return the current date and time. Other objects that need the date or time, use
the clock object by calling the appropriate member functions to fetch the current date or time.
15.4 Public Inheritance
Public inheritance should be used when a new class (the derived class) describes some set of
objects that is a subset of the objects being described by the base class. This relationship is
known as the ‘is-a’ relationship.
So what does public inheritance mean? Consider the classes Base and Derived:
class Base
{
public:
int x;
protected:
int y;
private:
int z;
};
Public members of the Base class are public members of the Derived class too. The
Derived class inherits all data members and all member functions from the Base class, but it
can access only those that are not private. That is, a derived class can access public and
protected members of its base class. In the example, class Derived can access x and y
directly but not z, because z is private in class Base. If we want to access z which is a private
data member in the Base class, then we can access z in one of two ways:
134
COS1512/101
(2) or we could make z protected (more about this in section 15.1 in Savitch).
Now consider the following client program that instantiates objects of type Base and Derived:
int main( )
{
Base b;
Derived d;
cout << b.x;
cout << d.x;
//cout << b.y;
//cout << d.y;
//cout << b.z;
//cout << d.z;
}
Since x is public in class Base, it is also public in class Derived through public inheritance.
Therefore we can access x in the client program. But we cannot access y because it is a
protected member of both Base and Derived. And of course we cannot access z because it is
private.
Note, that the access specifier for class inheritance defaults to private. If class Derived has
base class Base:
class Base {/* . . .*/};
class Derived : access-specifier Base
{ /* . . . */};
//where access-specifier is one of public, protected, private
then the access that is granted to member functions of class Derived depends on the access-
specifier selected. If the access-specifier is omitted, access to all members of the base class
automatically defaults to private. This would mean that Derived has no access to the member
functions and data members of Base.
135
Chapter 17: Templates
Savitch: Sections 17.1 to 17.2
17.1 Overview
This chapter discusses C++ templates. C++ templates provide a way to reuse code by defining
functions and classes that have parameters for type names. Templates are very useful when
implementing generic constructs like vectors, stacks, lists and queues, which can be used with
any arbitrary type.
17.2 Learning Objectives
After having worked through sections 17.1 to 17.2 you should be able to:
2. Once again, you can safely ignore all references to namespaces other than std, such as
in lines 12 and 49 of Display 17.4.
3. The MinGW compiler does not allow separate compilation of templates. Therefore you
need to include the template definition (i.e. its declaration and the implementation thereof)
in the same (header) file. Note that you can place the template definition in a separate file
and use a #include directive to include the template definition when you need to use it.
4. Note that the use of the keyword class in a template prefix does not mean that you can
write only a class name there. You can use any type name already defined in the
language such as, int, float, double, or any identifier that is a user defined type (such
as a class, structure or enum).
5. Do the self-test exercises for sections 17.1 and 17.2.
136
COS1512/101
16 APPENDIX E: GLOSSARY
The following link provides a glossary for English/Afrikaans IT terminology:
http://www.coetzee.org/woordelys/
©
Unisa 2013
137