+1 (315) 557-6473 

Program To Create Various Classes Using Concepts of Inheritance in C++ Assignment Solution.


Instructions

Objective
Write a program to create various classes using concepts of inheritance in C++ language.

Requirements and Specifications

Description
This goal of this project is to build an information manager similar to a rolodex (see http://en.wikipedia.org/wiki/Rolodex). A physical rolodex is made up of cards arranged in alphabetical order by [Last Name, First Name]. Each card contains some information, usually name, address, phone number and type of business (why the person is in the rolodex). The physical mechanism of using a rolodex is turning the group of cards and flipping through them. When the last card is read, the rolodex is then at the start of the list since a rolodex is built on a circular track.
Design Notes
For this project you'll implement a Card class (in Card.h and Card.cpp), a Rolodex class (in Rolodex.h and Rolodex.cpp), and a main() function (in main.cpp). The code in main() reads interactive commands that perform actions on its Rolodex object (like add a card, search for a card, list the rolodex cards, etc).
The Card class is used to represent a single Rolodex card. This class has std::string data members for First name, Last name, Occupation, Address (entire address can be in one string), and Phone number. It has get/set member functions to allow getting and setting of the member data values (e.g. first name), and a show(ostream& os) function that knows how to display the card on the supplied ostream parameter. This class does not know about Rolodex functionality, it just encapsulates a single card's information.
Source Code
CARD
#include "Card.h"
using namespace std;
Card::Card()
{
    firstName = '\0';
    lastName = '\0';
    Address = '\0';
    Occupation = '\0';
    mPhone = '\0';
}
Card::Card( string first, string last, string occ, string add, string phone )
{
    firstName = first;
    lastName = last;
    Occupation = occ;
    Address = add;
    mPhone = phone;
}
void Card::setLast(const string last)
{
    lastName = last;
}
string Card::getLast()
{
    return lastName;
}
void Card::setFirst(const string first)
{
    firstName = first;
}
string Card::getFirst()
{
    return firstName;
}
void Card::setOcc(const string occup)
{
    Occupation = occup;
}
string Card::getOcc()
{
    return Occupation;
}
void Card::setAddress(const string add)
{
    Address = add;
}
string Card::getAddress()
{
    return Address;
}
void Card::setPhone(const string phone)
{
    mPhone = phone;
}
string Card::getPhone()
{
    return mPhone;
}
void Card::show(ostream& os)
{
 os << "\nName: " << getLast() << ", " << getFirst() << "\nOccupation: " << getOcc()
        << "\nAddress: " << getAddress() << "\nPhone Number: " << getPhone() << endl;
}
RODEX
#include "Rolodex.h"
#include "Card.h"
#include <cctype>
#include <algorithm>
#include <sstream>
using namespace std;
bool compare( Card first, Card second )
{
  if (first.getLast() != second.getLast()) {
  return first.getLast() < second.getLast();
 }
 return first.getFirst() <= second.getFirst();
}
bool matches(string who, Card card) {
 stringstream s(who);
 string s1;
 string s2;
 s >> s1;
 if (s >> s2) {
  // cout << "!" << s1 << "!" << s2 << "!" << endl;
  // cout << "!" << card.getFirst() << "!" << card.getLast() << "!" << endl;
  return card.getLast() == s2 && card.getFirst() == s1;
 }
 else {
  return card.getLast() == s1;
 }
}
void Rolodex::cardAdd(Card obj)
{
 int n = rolo.size();
 for (int i = 0;i<n; i++) {
  Card front = rolo.front();
  Card back = rolo.back();
  if (compare(front, back)) {
   break;
  }
  rolo.pop_front();
  rolo.push_back(front);
 }
 for (int i = 0;i<n; i++) {
  Card front = rolo.front();
  if (compare(obj, front)) {
   break;
  }
  rolo.pop_front();
  rolo.push_back(front);
 }
 rolo.push_front(obj);
}
Card Rolodex::cardRemove()
{
    Card temp = rolo.front();
  rolo.pop_front();
    return temp;
}
Card Rolodex::getCurrentCard()
{
    return rolo.front();
}
Card Rolodex::flip()
{
 Card front = rolo.front();
 rolo.pop_front();
 rolo.push_back(front);
 return rolo.front();
}
bool Rolodex::search(string who)
{
 int n = rolo.size();
 for (int i = 0; i<n; i++) {
  Card front = rolo.front();
  if (matches(who, front)) {
   return true;
  }
  rolo.pop_front();
  rolo.push_back(front);
 }
 cout << "Name not found: " << who << endl << endl;
 return false;
 // list< Card >::iterator tempit = roloit;
  // roloit = rolo.begin();
  // while ( roloit != rolo.end() )
  // {
  // if ( (roloit->getLast()).compare(who) )
  // return true;
  // else
  // roloit++;
  // }
 // for ( roloit = rolo.begin(); roloit != rolo.end(); roloit++ )
 // for ( i = 0; tolower(roloit->getLast()[0]) > tolower(who[0]); i++ )
 // if ( tolower(roloit->getLast()[i]) < tolower(who[i]))
 // break;
 // else if (roloit->getLast()[i] == who[i]);
 // else if (roloit->getLast()[i] > who[i] && !roloit->getLast()[0] )
 // return true;
 // cout << "Name not found: " << who << endl << endl;
 // return false;
}
void Rolodex::show(ostream& os)
{
 int n = rolo.size();
 for (list< Card >::iterator it = rolo.begin(); it != rolo.end(); it++) {
  it->show(os);
 }
}
MAIN
#include "Card.h"
#include "Rolodex.h"
#include <iostream>
#include <cstring>
using namespace std;
void search(Rolodex& temp)
{
 string who;
 cout << "\nEnter the last name of the person you are looking for: ";
 getline(cin, who);
 if (temp.search(who)) {
  temp.getCurrentCard().show(cout);
 }
}
void add(Rolodex& temp)
{
 string first;
 cout << "Please enter First Name: ";
 getline(cin, first);
 string last;
 cout << "Please enter Last Name: ";
 getline(cin, last);
 string occ;
 cout << "Please enter Occupation: ";
 getline(cin, occ);
 string add;
 cout << "Please enter Address: ";
 getline(cin, add);
 string phone;
 cout << "Please enter Phone: ";
 getline(cin, phone);
 temp.cardAdd(Card(first, last, occ, add, phone));
}
char showMenu() {
 string line;
 while (true) {
  cout << "Please, choose on of the options:" << endl;
  cout << "List (L)" << endl;
  cout << "View (V)" << endl;
  cout << "Flip (F)" << endl;
  cout << "Add (A)" << endl;
  cout << "Remove (R)" << endl;
  cout << "Search (S)" << endl;
  cout << "Quit the program (Q)" << endl;
  getline(cin, line);
  char choice = toupper(line[0]);
  if (choice == 'L') {
   return 'L';
  }
  else if (choice == 'V') {
   return 'V';
  }
  else if (choice == 'F') {
   return 'F';
  }
  else if (choice == 'A') {
   return 'A';
  }
  else if (choice == 'R') {
   return 'R';
  }
  else if (choice == 'S') {
   return 'S';
  }
  else if (choice == 'Q') {
   return 'Q';
  }
  else {
   cout << "Invalid input. Please, try again" << endl;
   cout << endl;
  }
 }
}
int main(void)
{
  Rolodex mainrolo;
  int i;
  Card card[10];
 card[0].setFirst("Tony");
 card[0].setLast("Hansen");
 card[0].setAddress("12 E. St.\nNY, NY 33333");
 card[0].setOcc("Writer");
 card[0].setPhone("555-9999");
 card[1].setFirst("Jon");
 card[1].setLast("Smyth");
 card[1].setAddress("CMU Computer Services Pittsburg, PA");
 card[1].setOcc("Computer Hardware");
 card[1].setPhone("555-1324");
 card[2].setFirst("Alonza");
 card[2].setLast("Heard");
 card[2].setAddress("123 Anyplace Ave\nMalden, MA");
 card[2].setOcc("Mechanic");
 card[2].setPhone("555-5678");
 card[3].setFirst("Jen");
 card[3].setLast("Reyes");
 card[3].setAddress("325 Oak Rd\nWilmington, MA");
 card[3].setOcc("Graphic Artist");
 card[3].setPhone("555-4950");
 card[4].setFirst("Alan");
 card[4].setLast("Lupine");
 card[4].setAddress("1 Bigelow Ave.\nLawrence, MA");
 card[4].setOcc("Vet");
 card[4].setPhone("555-7654");
 card[5].setFirst("Jewel");
 card[5].setLast("Proverb");
 card[5].setAddress("34 Washington St.\nWaltham, MA");
 card[5].setOcc("Landscaper");
 card[5].setPhone("555-3333");
 card[6].setFirst("Paul");
 card[6].setLast("Revere");
 card[6].setAddress("45 Commonwealth Ave.\nBoston, MA");
 card[6].setOcc("Radical Revolutionary");
 card[6].setPhone("555-1776");
 card[7].setFirst("Adolf");
 card[7].setLast("Coors");
 card[7].setAddress("Boston, MA");
 card[7].setOcc("Beer Manufacturer");
 card[7].setPhone("555-2337");
 card[8].setFirst("Seymour");
 card[8].setLast("Papert");
 card[8].setAddress("MIT");
 card[8].setOcc("Lego Professor");
 card[8].setPhone("555-1111");
 card[9].setFirst("Fred");
 card[9].setLast("Milton");
 card[9].setAddress("12 Freedom Way\nNashua, NH");
 card[9].setOcc("Sales");
 card[9].setPhone("555-9981");
 for ( i = 0; i < 10; i++ )
  mainrolo.cardAdd(card[i]);
 bool isOver = false;
 while (!isOver) {
  switch(showMenu()) {
   case 'L':
    mainrolo.show(cout);
    break;
   case 'V':
    mainrolo.getCurrentCard().show(cout);
    break;
   case 'F':
    mainrolo.flip().show(cout);
    break;
   case 'A':
    add(mainrolo);
    break;
   case 'R':
    mainrolo.cardRemove();
    break;
   case 'S':
    search(mainrolo);
    break;
   case 'Q':
    isOver = true;
    break;
  }
  cout << endl;
 }
 cout << "Good Bye!" << endl;
 return 0;
}