100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary COS1512 Assignment 3 Solutions 2022 R150,00   Add to cart

Summary

Summary COS1512 Assignment 3 Solutions 2022

 60 views  1 purchase

Code Solutions to Assignment 3 - COS 1512. With download link for all questions.

Preview 7 out of 31  pages

  • No
  • Unknown
  • August 16, 2022
  • 31
  • 2022/2023
  • Summary
book image

Book Title:

Author(s):

  • Edition:
  • ISBN:
  • Edition:
All documents for this subject (51)
avatar-seller
StudyWellNotes
COS 1512
ASSIGNMENT 3
SOLUTIONS




StudyWellNotes
8-16-2022

, COS1512_ASSIGNMENT3_2022




Contents
Question 1............................................................................................................................................................. 2
Program Code ................................................................................................................................................... 3
Question 2............................................................................................................................................................. 6
Question 3............................................................................................................................................................. 8
Question 4........................................................................................................................................................... 11
Question 5........................................................................................................................................................... 14
PROGRAM CODE ............................................................................................................................................. 17
Question 6 a ........................................................................................................................................................ 20
Question 6b......................................................................................................................................................... 23
Question 7........................................................................................................................................................... 26
Program Code ................................................................................................................................................. 26
Question 8........................................................................................................................................................... 30




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:

//implementation
Address::Address()
//constructor
{
streetName = "
"; streetNr =
0; city = " ";
postalCode = "0000";
}


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;
}


Program Code

#include <iostream>
#include <string>
using namespace std;
class Address {
public:
Address();
void setStreetName(string s);
void setStreetNumber(int n);
void setCity(string c);
void setPostalCode(string p);
string getStreetName()const;
int getStreetNumber()const;
string getCity()const;
string getPostalCode()const;
private:
string streetName;
int streetNr;
string city;
string postalCode;
};




P a g e 3 | 30

, COS1512_ASSIGNMENT3_2022




//implementation
Address::Address() { //constructor
streetName = " ";
streetNr = 0;
city = " ";
postalCode = "0000";
}
//Mutators to change (set) the value of each member variable
void Address::setStreetName(string s) {
streetName = s;
}
void Address::setStreetNumber(int n) {
streetNr = n;
}
void Address::setCity(string c) {
city = c;
}
void Address::setPostalCode(string p) {
postalCode = p;
}
//Accessors to retrieve (get) data from each of member variables
string Address::getStreetName() const {
return streetName;
}
int Address::getStreetNumber() const {
return streetNr;
}
string Address::getCity() const {
return city;
}
string Address::getPostalCode() const {
return postalCode;
}
//main application
int main() {
Address myAddress;
string sName;
int sNr;
string cCity;
string PostalCode;
cout << "Please enter your address:" << endl;
cout << "Street name: ";
getline(cin, sName, '\n');
myAddress.setStreetName(sName);
cout << "Street number: ";
cin >> sNr;
myAddress.setStreetNumber(sNr);
cout << "City: ";
cin >> cCity;
myAddress.setCity(cCity);
cout << "Postal code: ";
cin >> PostalCode;
myAddress.setPostalCode(PostalCode);
cout << endl << "My address is: " << endl;
cout << myAddress.getStreetNumber() << " " << myAddress.getStreetName() <<



P a g e 4 | 30

, COS1512_ASSIGNMENT3_2022




endl;
cout << myAddress.getCity() << endl;
cout << myAddress.getPostalCode() << endl;
return 0;
}

/*
Please enter your address:
Street name: Preller St
Street number: 543
City: Muckleneuk
Postal code: 0002

My address is:
543 Preller St
Muckleneuk
0002

--------------------------------
Press any key to continue . . .*/




P a g e 5 | 30

, COS1512_ASSIGNMENT3_2022




Question 2

#include <iostream>
using namespace std;

class ShowTicket {
private:
int iRow,iSeatNumber;
bool bSold;

public:
ShowTicket();
ShowTicket(int row, int seat, bool sold);
bool getSold() const;
void setSold(bool sold);
void print() const;
~ShowTicket();
};

// default constructor
ShowTicket::ShowTicket(){
iRow = 0;
iSeatNumber = 0;
bSold = false;
}

// overloaded constructor
ShowTicket::ShowTicket(int row, int seat, bool sold) {
iRow = row;
iSeatNumber = seat;
bSold = sold;
}

// method to print
void ShowTicket::print() const{
cout << "Row:" << iRow << endl;
cout << "Seat Number: " << iSeatNumber << endl;

if (bSold == true) {
cout << "Sold Status: " << "Ticket Sold" << endl;
} else {
cout << "Sold Status: " << "Ticket Not Sold" << endl;
}

cout << endl;
}

// 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

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

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

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 this summary 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 this summary for R150,00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

75323 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R150,00  1x  sold
  • (0)
  Buy now