Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// 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;

}

-----------------------------------------

Questions:

----------------------------------------

1. Consider this code snippet: Name name1("Cassandra", "O'Hara"); Name name2("Smith"); if ( name1 < name2 ) cout << 0 ; else cout << 1 ; On the answersheet, for question 1, list the letters of the statements below that are true. It is possible that none are true or several are true. (a) One or more of these statements results in a compiler error. (b) The code compiles correctly but will have a run-time error. (c) The code compiles correctly and outputs 0. (d) The code compiles correctly and outputs 1.

2. True or False. The declaration below results in an error. Name name3("Gandalf", "The Gray"); On the answersheet, for question 2, write either True or False.

3. True or False. The code below sets the value of the private member fname of name to "Ben Hur". Name name; name.set_first("Ben Hur"); On the answersheet, for question 3, write either True or False.

4. True or False. The statement #include can be replaced by #include "iomanip" without causing any change to the compiled code. On the answersheet, for question 4, write either True or False.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions