Practice

SIN Card


ID Card

Design and code a class named IDCard that holds identity information about a card holder.  Place your class declaration in a header file named IDCard.h and your function definitions in an implementation file named IDCard.cpp.  Include in your coding all of the statements necessary for your code to compile under a standard C++ compiler.

Upon instantiation, an IDCard object may receive no parameters or a null-terminated C-style string holding the name of the card holder.  If the object receives a non-empty string, the object accepts the string as the card holder's name.  Otherwise, the object assumes a safe empty state.  The string holding the card holder's name may be of any length. 

Since the name of the card holder may be of any length, your design must include a copy constructor, an assignment operator and a destructor. 

Your class also includes the following member functions:

  • const char* name() const - a query that returns the address of the card holder's name if the data stored is valid and the NULL address if there is no valid data stored; and
  • int valid() const - a query that returns 1 if the data stored is valid and 0 otherwise.

Finally, your design overloads the insertion operator for an ostream object as the left operand and an IDCard as the right operand.  The operator displays on the left operand the name of the card holder stored in the right operand.  If the right operand does not contain valid data, this operator displays the string "unknown" on the left operand.  Design this operator so that it handles cascading.

For example, consider the following program that uses your class


 #include <iostream>
 using namespace std;
 #include "IDCard.h"

 int main ( ) {
     IDCard person("Jane Doe");
     IDCard clone = person;
     IDCard forgery;

     cout << forgery.valid() << clone.valid() << endl;
     cout << person << endl;
     cout << clone << endl;
     cout << forgery << endl;

     forgery = person;

     cout << forgery.valid() << clone.valid() << endl;
     cout << person << endl;
     cout << clone << endl;
     cout << forgery << endl;

     return 0;
 }

This program produces the following output


  01
  Jane Doe
  Jane Doe
  unknown
  11
  Jane Doe
  Jane Doe
  Jane Doe


SIN Card

Derive from an IDCard class a new class named SINCard that holds social insurance information about a card holder.  Place your class declaration in a header file named SINCard.h and your function definitions in an implementation file named SINCard.cpp.  Include in your coding all of the statements necessary for your code to compile under a standard C++ compiler.

Upon instantiation, an SINCard object may receive

  • no parameters
  • a null-terminated C-style string holding the name of the card holder and an integer holding the social insurance number of the cardholder. 

If the object receives two arguments, the object accepts the string as the card holder's name and the number as the card holder's social insurance number.  If the name supplied is a valid name and if the number supplied is a positive number, the object stores the number supplied as the card holder's social insurance number.  Otherwise, the object stores a safe empty state. 

Your derived class includes the following member functions:

  • int valid() const - a query that returns 1 if the object holds a valid name and a valid social insurance number.  Otherwise, this function returns 0. 
  • int sin() const - a query that returns the social insurance number of the object if it holds a valid name and a valid number.  Otherwise, this function returns 0. 
  • SINCard& operator=(int n) - a modifier that receives an integer n and, if n is valid, stores the integer as the card holder's social insurance number.  If n is invalid or the object is not holding valid name data, this function does nothing. 

Finally, your design overloads the insertion operator for an ostream object as the left operand and an SINCard as the right operand.  The operator displays on the left operand the social insurance number of the card holder in a field of 9 with zero fill followed by the name of the card holder.  If the right operand does not contain valid data, this operator displays a number filled with 0's followed by the string "unknown".  Design this operator so that it handles cascading.

For example, consider the following program that uses your class


 #include <iostream>
 using namespace std;
 #include "SINCard.h"

 int main ( ) {
     SINCard person("Jane Doe", 66666666), unknown;

     cout << person.valid() << unknown.valid() << endl;
     cout << person << endl;
     cout << unkonwn << endl;

     person = 123456789;

     cout << person << endl;

     return 0;
 }

This program produces the following output


  10
  066666666 Jane Doe
  000000000 unknown
  123456789 Jane Doe







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License