Part D - Inheritance
Derived Classes
Workshop 8
In this workshop, you code a hierarchy consisting of two classes,
where one class contains the structure of the other class.
Learning Outcomes
Upon successful completion of this workshop, you will have
demonstrated the abilities to
- derive one class from another
- call a base class constructor from the derived class
constructor
- access a base class function that is shadowed by a
derived class function
Telephony - All Groups
Your hierarchy consists of two classes:
Phone Class
Design and code a class named Phone that holds a telephone
number. Upon instantiation, a PhoneNumber object receives either nothing
or two integers: one identifying the area code and the other
identifying the local telephone number. If the object
receives nothing or an invalid number, the object adopts a
safe empty state. A valid area code is between 100 and 999
inclusive. A valid local number is between 1,000,000 and
9,999,999 inclusive.
Your Phone class includes the
following member functions:
- void display() const - a query
that displays the telephone number in AAA-LLL-LLLL format, where AAA denotes area code and LLL-LLLL denotes local number separated
into two parts by a dash.
- bool isValid() const - a query that
returns true if the number is valid,
false if the object is in a safe
empty state.
Place your class definition in a header file
named Phone.h and your function
definitions in an implementation file named Phone.cpp
Client Module
The implementation file for the main module that uses your
class is listed on the left, while the results are shown on the
right:
// Workshop 8 - Derived Classes
// w8.cpp
#include <iostream>
#include "Phone.h"
const int MAX_NO = 20;
int main() {
int n = 0, code, local;
Phone phone[MAX_NO];
std::cout << "Telephone List\n";
std::cout << "==============\n";
do {
std::cout << "Area Code: ";
std::cin >> code;
if (code != 0) {
std::cout << "Local No.: ";
std::cin >> local;
Phone temp(code, local);
if (temp.isValid())
phone[n++] = temp;
}
} while (code != 0 && n < MAX_NO);
std::cout << std::endl;
for (int i = 0; i < n; i++) {
phone[i].display();
std::cout << std::endl;
}
}
|
Telephone List
==============
Area Code : 416
Local No. : 5551212
Area Code : 141
Local No. : 4441111
Area Code : 228
Local No. : 6661234
Area Code : 0
416-555-1212
141-444-1111
228-666-1234
|
IntlPhone Class
Design and code a class named IntlPhone, derived from Phone that holds an international
telephone number. Upon instantiation, an IntlPhone object receives either nothing or
three integers: one identifying the country or region code, one
identifying the area code within that country or region and one
identifying the local telephone number within that area
code. If the object receives nothing or an invalid number,
the object adopts a safe empty state. A valid country or
region code is between 1 and 999 inclusive. A valid area
code is between 100 and 999 inclusive. A valid local
number is between 1,000,000 and 9,999,999 inclusive.
Your IntlPhone class includes
the following member functions:
- void display() const - a query
that displays the phone number in CCC-AAA-LLL-LLLL format, where CCC denotes country code, AAA denotes area code within a country or region,
and LLL-LLLL denotes local
number.
- bool isValid() const - a query that
returns true if the number is valid,
false if the current object is in a
safe empty state.
Add your class definition to the header file
named Phone.h and your function
definitions to the implementation file named Phone.cpp
Client Module
The implementation file for the main module that uses your
class is listed on the left, while the results are shown on the
right:
// Workshop 8 - Derived Classes
// w8.cpp
#include <iostream>
#include "Phone.h"
const int MAX_NO = 20;
int main() {
int n = 0, country, code, local;
IntlPhone phone[MAX_NO];
std::cout << "Telephone List\n";
std::cout << "==============\n";
do {
std::cout << "Country : ";
std::cin >> country;
if (country != 0) {
std::cout << "Area Code : ";
std::cin >> code;
std::cout << "Local No. : ";
std::cin >> local;
IntlPhone temp(country, code, local);
if (temp.isValid())
phone[n++] = temp;
}
} while (country != 0 && n < MAX_NO);
std::cout << std::endl;
for (int i = 0; i < n; i++) {
phone[i].display();
std::cout << std::endl;
}
}
|
Telephone List
==============
Country : 1
Area Code : 416
Local No. : 5551212
Country : 44
Area Code : 141
Local No. : 4441111
Country : 49
Area Code : 228
Local No. : 6661234
Country : 0
1-416-555-1212
44-141-444-1111
49-228-666-1234
|
Upgrade - Group Y
Design and code the Phone and
IntlPhone classes described above.
Add input and output functionality to your design by overloading
the following helper operators:
- std::istream& operator>>(std::istream&, Phone& p)
- extracts data from standard input and stores it in p
- std::istream& operator>>(std::istream&, IntlPhone& p)
- extracts data from standard input and stores it in p
- std::ostream& operator<<(std::ostream&, const Phone& p)
- inserts data from p into standard output
- std::ostream& operator<<(std::ostream&, const IntlPhone& p)
- inserts data from p into standard output
Add your prototypes to the header file
named Phone.h and your function
definitions to the implementation file named Phone.cpp
Client Module
The implementation file for the main module that uses your
class is listed on the left, while the results are shown on the
right:
// Workshop 8 - Derived Classes
// w8.cpp
#include <iostream>
#include "Phone.h"
const int MAX_NO = 20;
int main() {
int n = 0, country, code, local;
IntlPhone temp, phone[MAX_NO];
std::cout << "Telephone List\n";
std::cout << "==============\n";
do {
std::cin >> temp;
if (temp.isValid())
phone[n++] = temp;
} while (temp.isValid() && n < MAX_NO);
std::cout << std::endl;
for (int i = 0; i < n; i++)
std::cout << phone[i] << std::endl;
}
|
Telephone List
==============
Country : 1
Area Code : 416
Local No. : 5551212
Country : 44
Area Code : 141
Local No. : 4441111
Country : 49
Area Code : 228
Local No. : 6661234
Country : 0
1-416-555-1212
44-141-444-1111
49-228-666-1234
|
SUBMISSION
Typescript
Create a typescript of your complete solution
using the following commands:
+ At the prompt, type: script w8.txt
+ At the prompt, type: whoami
+ At the prompt, type: cat Phone.h Phone.cpp
+ At the prompt, type: g++ -o w8 w8.cpp Phone.cpp
+ At the prompt, type: w8
+ At the prompt type: exit
|
These commands will produce a file named w8.txt. Download this file to your local
computer.
Moodle
Upload your typescript file to Moodle:
- Login to Moodle
- Select your course code
- Select W8 under Assignments and Tasks
- Upload w8.txt
- Write a short note to your instructor
- press "Edit"
- enter your conclusions about this workshop in the notes textbox
- press "Save Changes"
- When ready to submit, press "Send for Marking"
Other Submissions
Submit your typescript file as your instructor has specified.
|