Question

Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This cla

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

// C++ code to create Address, Contact and AddressBook classes

// Address.h

#ifndef ADDRESS_H_
#define ADDRESS_H_

#include <iostream>
using namespace std;

class Address
{
private:
   string m_city;
   string m_state;

public:
   Address(string city="", string state="");
   void setCity(const string &city);
   void setState(const string &state);
   const string& getCity() const;
   const string& getState() const;
   friend ostream& operator<<(ostream &out, const Address &address);
   bool operator==(const Address &other) const;
};

#endif /* ADDRESS_H_ */
//end of Address.h

// Address.cpp

# include "Address.h"

Address::Address(string city, string state) : m_city(city), m_state(state)
{}

void Address::setCity(const string &city)
{
   m_city = city;
}

void Address::setState(const string &state)
{
   m_state = state;
}

const string& Address::getCity() const
{
   return m_city;
}

const string& Address::getState() const
{
   return m_state;
}

ostream& operator<<(ostream &out, const Address &address)
{
   out<<"City : "<<address.getCity()<<" State : "<<address.getState();
   return out;
}

bool Address::operator==(const Address &other) const
{
   return((m_city== other.getCity()) && (m_state == other.getState()));
}

//end of Address.cpp

// Contact.h

#ifndef CONTACT_H_
#define CONTACT_H_

#include "Address.h"

class Contact
{
private:
   string m_firstName;
   string m_lastName;
   Address m_address;

public:
   Contact(string firstName="", string lastName ="", string m_city="", string m_state="");
   void setFirstName(const string &firstName);
   void setLastName(const string &lastName);
   void setAddress(const Address &address);
   const string& getFirstName() const;
   const string& getLastName() const;
   const Address& getAddress() const;
   friend ostream& operator<<(ostream &out, const Contact &other);
   bool operator==(const Contact &other) const;
};

#endif /* CONTACT_H_ */
//end of Contact.h

// Contact.cpp

#include "Contact.h"

Contact::Contact(string firstName, string lastName, string m_city, string m_state) : m_firstName(firstName), m_lastName(lastName), m_address(m_city,m_state)
{}

void Contact:: setFirstName(const string &firstName)
{
   m_firstName = firstName;
}

void Contact:: setLastName(const string &lastName)
{
   m_lastName = lastName;
}

void Contact:: setAddress(const Address &address)
{
   m_address = address;
}

const string& Contact:: getFirstName() const
{
   return m_firstName;
}

const string& Contact:: getLastName() const
{
   return m_lastName;
}

const Address& Contact:: getAddress() const
{
   return m_address;
}

ostream& operator<<(ostream &out, const Contact &other)
{
   out<<"FirstName : "<<other.getFirstName()<<" LastName : "<<other.getLastName()<<endl;
   cout<<"Address : "<<other.getAddress();
   return out;
}

bool Contact:: operator==(const Contact &other) const
{
   return((m_firstName == other.getFirstName()) && (m_lastName == other.getLastName()) && (m_address == other.getAddress()));
}

//end of Contact.cpp

// AddressBook.h

#ifndef ADDRESSBOOK_H_
#define ADDRESSBOOK_H_

#include "Contact.h"
#include <vector>

class AddressBook
{
private:
   vector<Contact> m_contacts;

public:
   AddressBook();
   void printAllContacts() const;
   size_t size() const;
   void addContact(const Contact &contact);
   void deleteAllContacts();
   void searchByAddress(const Address &address);
};

#endif /* ADDRESSBOOK_H_ */
//end of AddressBook.h

// AddressBook.cpp

#include "AddressBook.h"

AddressBook::   AddressBook()
{}

void AddressBook:: printAllContacts() const
{
   for(size_t i=0;i<m_contacts.size();i++)
   {
       cout<<m_contacts[i]<<endl;
   }
}

size_t AddressBook:: size() const
{
   return m_contacts.size();
}

void AddressBook::addContact(const Contact &contact)
{
   m_contacts.push_back(contact);
}

void AddressBook:: deleteAllContacts()
{
   m_contacts.clear();
}

void AddressBook:: searchByAddress(const Address &address)
{
   bool found = false;

   for(size_t i=0;i<m_contacts.size();i++)
   {
       if(m_contacts[i].getAddress() == address)
       {
           found = true;
           cout<<m_contacts[i]<<endl;
       }
   }

   if(!found)
       cout<<"No contact found for address : "<<address<<endl;
}

//end of AddressBook.cpp

Add a comment
Know the answer?
Add Answer to:
Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...
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
  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Create a C++ project with 2 classes, Person and Birthdate. The description for each class is...

    Create a C++ project with 2 classes, Person and Birthdate. The description for each class is shown below: Birthdate class: private members: year, month and day public members: copy constructor, 3 arguments constructor, destructor, setters, getters and age (this function should return the age) Person class: private members: firstName, lastName, dateOfBirth, SSN public members: 4 arguments constructor (firstName, lastName, datOfBirth and SNN), destructor and printout Implementation: - use the separated files approach - implement all the methods for the 2...

  • C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1:...

    C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1: default constructor that initialized the attributes with proper default data of your choice. 2: destructor() method, that releases all the dynamically allocated memory. 3: copy constructor() method: That properly manages the dynamic array when the object is passed by value to a function as a parameter. 4: overload the assignment operator: To properly handle copying the dynamic array when one object is assigned to...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names...

    C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient. The header file for this class is given below. #ifndef LETTER_H #define LETTER_H #include #include using namespace std; class Letter { public:    //constructor    Letter(string from = "John", string to = "Ana");    void add_line(string line);    string get_text() const; private:    string sender;    string recipient;    vector lines; }; #endif -Implement the constructor to initialize...

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
ADVERTISEMENT