Question: // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include #include using namespace std; #define MAXLENGTH 12 #define NAME_CHARS abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-' namespace Errors { struct InvalidName { }; }
// Name.h
#ifndef __NAME_H__
#define __NAME_H__
#include
#include
using namespace std;
#define MAXLENGTH 12
#define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'"
namespace Errors
{
struct InvalidName { };
}
class Name
{
public:
Name ( string first_name = "", string last_name = "");
string first() const;
string last() const;
void set_first( string fname );
void set_last( string lname);
friend ostream& operator<< ( ostream & os, Name name );
friend bool operator== (Name name1, Name name2 );
friend bool operator< (Name name1, Name name2 );
friend bool operator> (Name name1, Name name2 );
private:
string fname;
string lname;
};
#endif /* __NAME_H__ */
// Name.cpp
#include "name.h"
#include
Name::Name ( string first_name, string last_name):
fname(first_name), lname(last_name) {}
ostream& operator<< ( ostream& os, Name name )
{
os << setw(MAXLENGTH) << left;
os << name.fname << setw(MAXLENGTH) << left << name.lname;
return os;
}
string Name::first() const
{
return fname;
}
string Name::last() const
{
return lname;
}
void Name::set_first( string first )
{
if ( first.length() <= MAXLENGTH &&
first.npos == first.find_first_not_of(NAME_CHARS) )
fname = first;
else {
fname = "";
throw(Errors::InvalidName() );
}
}
void Name::set_last( string last )
{
if ( last.length() <= MAXLENGTH &&
string::npos == last.find_first_not_of(NAME_CHARS) )
lname = last;
else {
lname = "";
throw(Errors::InvalidName() );
}
}
bool operator== (Name name1, Name name2 )
{
if ( name1.lname == name2.lname &&
name1.fname == name2.fname
)
return true;
else
return false;
}
bool operator< (Name name1, Name name2 )
{
if ( ( name1.lname < name2.lname ) ||
( name1.lname == name2.lname &&
name1.fname < name2.fname )
)
return true;
else
return false;
}
bool operator> (Name name1, Name name2 )
{
if ( ( name1.lname > name2.lname ) ||
( name1.lname == name2.lname &&
name1.fname > name2.fname )
)
return true;
else
return false;
}
---------------------------------------------------------------------------------------------
//contant.h
#ifndef _CONTACT_H
#define _CONTACT_H
#include "name.h"
using namespace std;
class Contact
{
public:
Contact ();
Contact (Name person, string tel_num ="", string email_addr ="");
int set (string fname, string lname, string tel_num, string email_addr);
int set ( char* csv_string);
void get_name (Name & fullname);
void get_tel (string & tel_num);
void get_email (string & email_addr);
void set_name (Name fullname);
void set_tel (string tel_num);
void set_email (string email_addr);
string convert2csv ();
friend bool operator> (Contact contact1, Contact contact2);
friend bool operator< (Contact contact1, Contact contact2);
friend bool operator== (Contact contact1, Contact contact2);
friend ostream& operator<< (ostream &, Contact );
friend bool match (Contact contact1, Contact contact2);
private:
Name name;
string telephone;
string email;
bool is_valid_telephone(string tel) const;
};
#endif /* _CONTACT_H */
//contact.cpp
#include
#include
#include "contact.h"
#include "name.h"
#include
#define DIGITS "0123456789"
Contact::Contact (): name("",""), telephone(""), email("") {}
Contact::Contact (Name person, string tel_num, string email_addr):
name(person), telephone(tel_num), email(email_addr) {}
int Contact::set (string fname,
string lname,
string tel_num,
string email_addr)
{
name.set_first(fname);
name.set_last(lname);
telephone = tel_num;
email = email_addr;
return 1;
}
int Contact::set ( char* csv_string)
{
char lname[33];
char fname[33];
char tel_num[11];
char email_addr[128];
char user[128];
char domain[128];
int count;
count = sscanf(csv_string,
"%32[a-zA-Z'-],%32[a-zA-Z'-],%10[0-9],%127s",
fname, lname, tel_num, email_addr );
if ( count < 4 ) {
return 0;
}
else if ( strlen(tel_num) < 10 )
return 0;
else {
name.set_first(fname);
name.set_last(lname);
telephone = tel_num;
count = sscanf(email_addr,"%[^@]@%[a-zA-Z0-9.]",user, domain);
if ( count < 2 )
return 0;
else {
email = email_addr;
return 1;
}
}
}
void Contact::get_name (Name & fullname)
{
fullname.set_first(name.first());
fullname.set_last(name.last());
}
void Contact::get_tel (string & tel_num)
{
tel_num = telephone;
}
void Contact::get_email (string & email_addr)
{
email_addr = email;
}
void Contact::set_name (Name fullname)
{
name.set_first(fullname.first());
name.set_last(fullname.last());
}
void Contact::set_tel (string tel_num)
{
telephone = tel_num;
}
void Contact::set_email (string email_addr)
{
email = email_addr;
}
bool operator> (Contact contact1, Contact contact2)
{
return ( contact1.name > contact2.name );
}
bool operator< (Contact contact1, Contact contact2)
{
return ( contact1.name < contact2.name );
}
bool operator== (Contact contact1, Contact contact2)
{
return ( contact1.name == contact2.name );
}
ostream& operator<< (ostream & out, Contact contact )
{
out << contact.name << setw(12)
<< contact.telephone << setw(127)
<< contact.email << " ";
return out;
}
string Contact::convert2csv ()
{
char csv[512];
sprintf(csv, "%s,%s,%s,%s", name.first().c_str(), name.last().c_str(),
telephone.c_str(), email.c_str());
return csv;
}
bool match (Contact contact1, Contact contact2)
{
if ( contact1.name.last() != "" && contact2.name.last() != "" ) {
if ( contact1.name.last() != contact2.name.last() )
return false;
}
if (contact1.name.first() != "" && contact2.name.first() != "") {
if ( contact1.name.first() != contact2.name.first() )
return false;
}
if (contact1.telephone != "" && contact2.telephone != "") {
if ( contact1.telephone != contact2.telephone )
return false;
}
if (contact1.email != "" && contact2.email != "") {
if ( contact1.email != contact2.email )
return false;
}
return true;
}
bool Contact::is_valid_telephone(string tel) const
{
if ( tel.npos == tel.find_first_not_of(DIGITS) )
return true;
else
return false;
}
---------------------------------------------------------------------------
The questions are from 5 to 8:
---------------------------------------------------------------------------
5. Given the declaration
Contact new_contact;
which of the following instructions will execute without error? On the answersheet for question 5, put the letters of the ones that execute without error on the answersheet for this question.
(a) new_contact.set("Xavier,Francis,8009367777,fx32@abc.cuny.65");
(b) new_contact.set("donald,duck,7771234567,dduckatdisney");
(c) new_contact.set("Dr.,Strangelove,2223331888,drstrangelove@hollywoodandvine");
(d) new_contact.set("root,user,1111111111,root@192.168.1.0");
(e) new_contact.set("Willy-Nelson,O'Flanagan,6461118888,willi@theHem@music.org");
(f) new_contact.set("Willy-Nilly,O'Keefe,6461118000,willinilli2@music.org");
6. The contact.cpp _le has the following include directives:
(a) #include
(b) #include
(c) #include
(d) #include "contact.h"
(e) #include "name.h"
Are each of these directives necessary for this file to be compiled without error? On the answersheet,
for question 6, write the letters of the include directives above that cannot be removed without introducing a compiler-error .
Answer questions 7 and 8 based on the following declarations
Name security_guard("Jane");
Name office_person("Joe");
Name strange_person("Jane", "Doe");
Name unknown;
7. Which of the following are error-free? List the letters of the ones that are error-free on the
answersheet for question 7.
(a) Contact c1( unknown);
(b) Contact c2( security_guard );
(c) Contact c3( strange_person, "8886661234");
(d) Contact c4( "Jane", "Doe", "", "");
8. What is output by the code below? Enter a 0 or a 1 for question 8.
Contact c5("Jane", "", ""), c6(security_guard, "", "");
if ( c5 == c6 ) cout << 0 ; else cout << 1 ;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
