About This Document
1. All questions come with code
a. Please scroll to the last page of this document, to get the download link
2. Question 8, required you to do the question yourself, don’t forget about it.
3. If you are having any issues, kindly email studywellnotes@gmail.com
P a g e 1 | 30
, COS1512_ASSIGNMENT3_2022
Question 1
Discussion:
For this question, you had to convert the struct Address into a class. There is essentially only a slight
difference between a struct and a class. A class is the same as a struct (i.e. a struct may also
contain both member variables and member functions just like a class, even though the examples
in Savitch do not show that) except that, by default, all members are inaccessible to the general user of
the class. This means that all members of a struct are by default public, and all members of a class
are by default private. Therefore, we have to specify that the member functions are public.
(As an exercise, omit the public keyword from the class and recompile it.) While there are situations
where a struct is more suitable, as a rule, you should use classes rather than structs. In
general, it is preferable to use classes, as classes offer better protection.
An object encapsulates or combines data and operations on that data into a single unit. In C++,
the mechanism that allows you to combine data and the operations on that data in a single unit is
called a class.
A class can be seen as a user-defined data type. We use a class to declare or instantiate an object.
When an object of a specific class is instantiated, the constructor for the class is called by default. The
purpose of the constructor is to initialise the member variables of the object automatically. This means that
a constructor should not include instructions to obtain values for the member variables (cin statements).
It also means that the member variables should not be re-declared inside the constructor, since
these variables will then be local to the function, and will prevent the constructor function from
accessing the member variables to initialise them. Study the constructor for class Address as shown
below to see how these concepts are implemented:
As streetName, streetNr, city, and postalCode are private member variables (see page 549
of Savitch 6th edition/ page 581 of Savitch 7th edition/ page 579 of Savitch 8th edition) of class
Address, they cannot be accessed directly in the main() function. As a result, public member
functions getStName(), getStNr(), getCity() and getPCode() are used to access these member
variables in order to determine their values. These functions are known as accessor functions, while
setStName(), setStNr(), setCity() and setPCode() are mutator functions. (Study pages 553
– 554 of Savitch, 6th edition/ pages 585 -586 of Savitch 7th edition/ pages 581 - 582 of Savitch 8th
edition).
Note the prototypes for member
functions: string
getStName() const; int
getStNr()const; string
getCity()const; string
getPCode()const;
P a g e 2 | 30
, COS1512_ASSIGNMENT3_2022
These member function are accessors - hence the const keyword at the end of the function
definition. Note that a member function that does not have the const keyword at the end of it may
possibly mutate (change or modify) the state of the object.
Note that the purpose of an accessor function (a function whose name starts with get, and
sometimes called a ‘get function’) is not to obtain an input value for the member variable from a
user, BUT to return the current value of the member variable. Accessor functions need to
indicate a return type corresponding to the member variable for which it returns a value. An
accessor function has no parameters. Since an accessor function is a member function of the class,
it refers directly to the member variable which value it should return in the body of the function, i.e.
there is no need to declare (and return) a local variable inside an accessor function. In fact, if you
do declare a local variable inside an accessor function, and return the value of the local variable, it
will NOT return the current value of the member variable!
Study the accessor function getStName() to see how these concepts are implemented:
//Accessors to retrieve (get) data from each of member variables
string Address::getStName() const
{
return streetName;
}
Mutator functions (functions whose names start with set, and sometimes called ‘set functions’) are used
to change or modify member variables of an object. The parameter of the mutator function typically
indicates the value according to which the member variable should be changed. For example, the
mutator function setStName()below modifies member variable streetName to s:
void Address::setStName(string s)
{
streetName = s;
}
// method to check if ticket is sold
bool ShowTicket::getSold() const {
return bSold;
}
P a g e 6 | 30
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller StudyWellNotes. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.60. You're not tied to anything after your purchase.