PolymorphismLab CPP
PolymorphismLab CPP
Lab 1
This lab will focus on using polymorphism while interacting with a contact
list. There will be a main class Contacts that controls the “view” the user
sees and responds to user input. The contact information (personal and
work) will be an instance of the Information class.
choice - This string attribute represents input from the user and is used
to change view.
index - This integer attribute keeps track of the particular contact whose
information is to be displayed.
length - This integer attribute keeps track of the length of the above
vectors.
To put polymorphism into practice, we are going to create the abstract class
Information. This will have the pure abstract functions DisplayInfo and
AddInfo. The Contacts class inherits from Information, and therefore it
must override the DisplayInfo and AddInfo functions.
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void AddInfo() {
}
};
Add the attributes and constructor for the Contacts class. For testing
purposes, set view to "quit". We will change this to a more appropriate
value later on. The other attributes do not need a value when instantiating
the object. Instantiate each vector attribute, set choice to an empty string,
and set index and length to 0. Additionally, add the Display function, which
we will go in more detail later on.
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
void AddInfo() {
void Display() {
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
};
void DisplayInfo() {
void AddInfo() {
void ShowList() {
Contacts contacts;
contacts.Display();
Run your program. Because the view attribute is "quit", the script should
immediately display a message and then stop. Your output should look
something like this.
.guides/img/Polymorphism/output_lab1_updated
Code
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void AddInfo() {
void ShowList() {
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout<< " "<<endl;
AddInfo();
}
else if (view==("quit")) {
cout<< ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
};
int main(){
//add code below this line
Contacts contacts;
contacts.Display();
return 0;
}
Lab 2
Adding a Contact
The first thing we need to do is to change the default view when a Contacts
object is instantiated. The list view should be the first view shown to the
user. Change the value for view from "quit" to "list" in the constructor.
The rest of the constructor should not be changed.
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
}
void AddInfo() {
void ShowList() {
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout<< " "<<endl;
AddInfo();
}
else if (view==("quit")) {
cout << ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
};
int main(){
Contacts contacts;
contacts.Display();
//add code above this line
return 0;
public:
Contacts() {
view = "list";
// rest of the constructor remains unchanged
}
Next we want to modify the ShowList function to show the list of people in
the contact list. There are two possible states for the list view: the list is
empty or there are contacts in the list. When the list is empty, the user will
be provided with the choice to add a contact or quit the program. Use a
conditional to represent these two states. For now, set view to "quit" in the
else branch.
The print statement is to add a blank line for legibility. We also need a cin
object to collect input from the user. If length is 0, then present the user
with a choice. Store their input in choice. The .tolower() function will
convert the user choice to a lowercase letter. This will make comparisons
easier. Remember, C++ is case sensitive; q and Q are not the same. By
forcing all input to lowercase, we only need to test for the lowercase letter.
The ShowList function ends by calling another function to handle the user’s
choice.
void ShowList() {
cout << endl;
char sc;
if (length == 0) {
cout << ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
choice = putchar(tolower(sc));
}
else {
view = "quit";
}
HandleChoice();
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
}
Adding a Contact
When the user enters "a", we need to create a new contact. To do this, we
are going to modify the AddInfo function. Use cout and the getline function
to ask the user to enter the name, personal phone number, personal email,
work title, work phone number, and work email, and then temporarily
store that information. Each piece of information should go into the
corresponding vector attribute after. Notice that we include cin >> ws
within getline because by doing so, we are able to consume the whitespace
ws. Once the information has been added, increase the length attribute and
revert back to the list view.
void AddInfo() {
cout << endl;
string sc2;
cout << ("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
Contacts contacts;
contacts.Display();
cout<< contacts.GetLength()<< endl;
Run the program and enter a when prompted, then add the following
contact:
Rachel Kim
555 123-4567
[email protected]
Senior Software Engineer
555 890-1234
[email protected]
Code
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
void AddInfo() {
cout << endl;
string sc2;
cout << ("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
int GetLength() {
return length;
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
}
void ShowList() {
cout << endl;
char sc;
if (length == 0) {
cout << ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
choice = putchar(tolower(sc));
}
else {
view = "quit";
}
HandleChoice();
}
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout << endl;
AddInfo();
}
else if (view==("quit")) {
cout << ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
};
int main(){
Contacts contacts;
contacts.Display();
cout << contacts.GetLength() << endl;
return 0;
}
Lab 3
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
void AddInfo() {
cout << endl;
string sc2;
cout << ("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
int GetLength() {
return length;
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
}
void ShowList() {
cout << endl;
char sc;
if (length == 0) {
cout << ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
choice = putchar(tolower(sc));
}
else {
view = "quit";
}
HandleChoice();
}
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout << endl;
AddInfo();
}
else if (view==("quit")) {
cout << ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
};
Contacts contacts;
contacts.Display();
cout << contacts.GetLength() << endl;
return 0;
But before doing that, we need to remove the print statement at the end of
the script. The final two lines of code should look like this:
Contacts contacts;
contacts.Display();
We are going to iterate through the list of contacts and print the name. To
help users select a contact, a number will appear before each name. This
way, the user types the number, and the contact’s information appears.
Previously, the cin object stored the user input as a char, but since we want
the user to be able to input numbers now, we have to change the input sc
to a string instead. Then we create a loop in which sc gets iterated and each
character will be converted into lowercase and added to the string choice.
In the else branch function, create a for loop to go from 0 to length. We
want a number followed by the name. The numbers should start at 1, so
print the loop index plus 1 followed by the element from the names vector.
After displaying the list of names, ask the user to make a selection. Entering
a number will show all of the information about a contact. After
HandleChoice is called, be sure to set choice back to an empty string;
otherwise, the user’s input will continue to be stored in choice and
negatively affect the acceptable input requirement.
void ShowList() {
cout << endl;
string sc;
if (length == 0) {
cout << ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
else {
for (int i = 0; i < length; i++) {
cout << (i + 1 ) << ") " << names.at(i) << endl;
}
cout << ("\n(#) Select a name \n(A)dd a new
contact\n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
HandleChoice();
choice = "";
}
We need to create the boolean helper function IsNumeric which takes the
user input and determines if they entered a number. First see if the user
input (a string) is empty. Return false if either condition is true. Then try to
convert the string to an integer. If this works, return true. If C++ throws an
exception because the string cannot be converted to an integer, then return
false. Since IsNumeric is a helper function, you should encapsulate it as
private.
bool IsNumeric(string s) {
int value;
if (s == "") {
return false;
}
try {
value = stoi(s);
return true;
}
catch (runtime_error& e) {
return false;
}
}
.guides/img/Polymorphism/lab3_pic1
Thomas Hobbes
555 666-7777
[email protected]
Philosopher
555 888-9999
[email protected]
.guides/img/Polymorphism/lab3_pic2
Code
#include <iostream>
#include <vector>
using namespace std;
//add class definitions below this line
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
void AddInfo() {
cout << endl;
string sc2;
cout << ("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
int GetLength() {
return length;
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
else if (IsNumeric(choice) && view==("list")) {
int num = (stoi(choice) - 1);
if (num >= 0 && num < length) {
index = num;
view = "info";
}
}
}
void ShowList() {
cout << endl;
string sc;
if (length == 0) {
cout << ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
else {
for (int i = 0; i < length; i++) {
cout << (i + 1 ) << ") " << names.at(i) << endl;
}
cout << ("\n(#) Select a name \n(A)dd a new
contact\n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
HandleChoice();
choice = "";
}
void Display() {
while (true) {
if (view=="list") {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout << endl;
AddInfo();
}
else if (view==("quit")) {
cout << ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
bool IsNumeric(string s) {
int value;
if (s == "") {
return false;
}
try {
value = stoi(s);
return true;
}
catch (runtime_error& e) {
return false;
}
}
};
int main(){
Contacts contacts;
contacts.Display();
return 0;
}
Lab 4
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
void AddInfo() {
cout<< endl;
string sc2;
cout<<("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
int GetLength() {
return length;
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
else if (IsNumeric(choice) && view==("list")) {
int num = (stoi(choice) - 1);
if (num >= 0 && num < length) {
index = num;
view = "info";
}
}
}
void ShowList() {
cout<< endl;
string sc;
if (length == 0) {
cout<< ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
else {
for (int i = 0; i < length; i++) {
cout<< (i + 1 ) << ") " << names.at(i)<<endl;
}
cout<< ("\n(#) Select a name \n(A)dd a new
contact\n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
HandleChoice();
choice = "";
}
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout<< endl;
AddInfo();
}
else if (view==("quit")) {
cout<< ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
bool IsNumeric(string s) {
int value;
if (s == "") {
return false;
}
try {
value = stoi(s);
return true;
}
catch (runtime_error& e) {
return false;
}
}
};
int main(){
Contacts contacts;
contacts.Display();
return 0;
}
On the previous page, we already modified HandleChoice to deal with
numeric input. So let’s update the DisplayInfo function to display the
information. Print the elements from each vector using the attribute index.
After the information is displayed, change view back to "list" and then call
ShowList.
void DisplayInfo() {
cout<< endl;
cout<<names.at(index)<<endl;
cout<<"Personal email address: " << personalEmails.at(index)
<<endl;
cout<<"Personal phone number: " <<
personalPhoneNumbers.at(index)<<endl;
cout<<"Work title: " << titles.at(index)<<endl;
cout<<"Work email address: " << workEmails.at(index)<<endl;
cout<<"Work phone number: " << workPhoneNumbers.at(index)
<<endl;
view = "list";
ShowList();
}
Code
#include <iostream>
#include <vector>
using namespace std;
class Information {
public:
virtual void DisplayInfo() = 0;
virtual void AddInfo() = 0;
};
void DisplayInfo() {
cout<< endl;
cout<<names.at(index)<<endl;
cout<<"Personal email address: " <<
personalEmails.at(index)<<endl;
cout<<"Personal phone number: " <<
personalPhoneNumbers.at(index)<<endl;
cout<<"Work title: " << titles.at(index)<<endl;
cout<<"Work email address: " << workEmails.at(index)
<<endl;
cout<<"Work phone number: " << workPhoneNumbers.at(index)
<<endl;
view = "list";
ShowList();
}
void AddInfo() {
cout<< endl;
string sc2;
cout<<("Enter their name: ");
getline(cin >> ws, sc2);
string name = sc2;
names.push_back(name);
int GetLength() {
return length;
}
void HandleChoice() {
if (choice==("q")) {
view = "quit";
}
else if (choice==("a") && view==("list")) {
view = "add";
}
else if (IsNumeric(choice) && view==("list")) {
int num = (stoi(choice) - 1);
if (num >= 0 && num < length) {
index = num;
view = "info";
}
}
}
void ShowList() {
cout<< endl;
string sc;
if (length == 0) {
cout<< ("(A)dd a new contact \n(Q)uit \n> ");
cin >> sc;
for (char c : sc) {
choice += (tolower(c));
}
}
else {
for (int i = 0; i < length; i++) {
cout<< (i + 1 ) << ") " << names.at(i)<<endl;
}
void Display() {
while (true) {
if (view==("list")) {
ShowList();
}
else if (view==("info")) {
DisplayInfo();
}
else if (view==("add")) {
cout<< endl;
AddInfo();
}
else if (view==("quit")) {
cout<< ("\nClosing the contact list...\n");
break;
}
}
}
private:
string view;
vector<string> names;
vector<string> titles;
vector<string> workPhoneNumbers;
vector<string> workEmails;
vector<string> personalPhoneNumbers;
vector<string> personalEmails;
string choice;
int index;
int length;
bool IsNumeric(string s) {
int value;
if (s == "") {
return false;
}
try {
value = stoi(s);
return true;
}
catch (runtime_error& e) {
return false;
}
}
};
int main(){
Contacts contacts;
contacts.Display();
return 0;
}
Lab Challenge
Problem
In the IDE to the left, the class Chef is already defined, as is the Display
function. However, it does not have a constructor. Create three
constructors that take one, two, and three parameters respectively. Note
that the attributes name and cuisine are set to "null" while michelinStars is
set to 0 by default.
Given Code
#include <iostream>
using namespace std;
class Chef {
public:
string GetName() {
return name;
}
string GetCuisine() {
return cuisine;
}
int GetStars() {
return michelinStars;
}
void Display() {
cout << GetName() << " is known for " << GetCuisine() << "
cuisine and has " << GetStars() << " Michelin stars." <<
endl;
}
private:
string name = "null";
string cuisine = "null";
int michelinStars = 0;
};
int main() {
c1.Display();
c2.Display();
c3.Display();
return 0;
Expected Output
Three Chef objects are instantiated, each one using a different constructor.
Calling the Display function for each object should return the following
text: