Question

// Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...

// Name.h

#ifndef __NAME_H__

#define __NAME_H__

#include <iostream>

#include <string>

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 <iomanip>

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 <stdio.h>

#include <string.h>

#include "contact.h"

#include "name.h"

#include <iomanip>

#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 << "\n";

    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 <iomanip>
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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) (d) The code compiles correctly and outputs 1.

2)  False, it does not result in error

3) False, Results in ERROR because empty space is NOT Accepted.

4) True, it can be replaced


See Image for more clarity



Thanks, PLEASE COMMENT



Add a comment
Know the answer?
Add Answer to:
// Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream;...

    lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT