In: Computer Science
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "Contact.h"
using namespace std;
class Contact
{
public:
Contact(string init_name = "", string init_phone =
"000-000-0000");
void setName(string name);
void setPhone(string phone);
string getName()const;
string getPhone()const;
friend ostream& operator << (ostream& os, const
Contact& c);
friend bool operator == (const Contact& c1, const Contact&
c2);
friend bool operator != (const Contact& c1, const Contact&
c2);
private:
string name, phone;
};
Contact::Contact(string init_name, string init_phone)
{
name = init_name;
phone = init_phone;
}
void Contact::setName(string name)
{
this->name = name;
}
void Contact::setPhone(string phone)
{
this->phone = phone;
}
string Contact::getName() const
{
return name;
}
string Contact::getPhone() const
{
return phone;
}
ostream& operator << (ostream& os, const Contact&
c)
{
os << "Name: " << c.name << endl;
os << "Phone: " << c.phone << endl;
return os;
}
bool operator == (const Contact& c1, const Contact&
c2)
{
return c1.name == c2.name;
}
bool operator != (const Contact& c1, const Contact&
c2)
{
return c1.name != c2.name;
}
int main()
{
}
==================================================================================
sample run
Name: John Casey Phone: 831-234-2323 Name: Amy Doe Phone: 831-534-2890 Name: Julie Anderson Phone: 731-234-6543 Name: Nebula Walker Phone: 831-565-1010 Name: Paul Rod Phone: 831-654-0090
Display the contacts from first to last and then in reverse order. Ask the user for a full name and display the Contact information for that person.
Sample Driver Program Run
List of contacts: Name: John Casey Phone: 831-234-2323 Name: Amy Doe Phone: 831-534-2890 Name: Julie Anderson Phone: 731-234-6543 Name: Nebula Walker Phone: 831-565-1010 Name: Paul Rod Phone: 831-654-0090 Contacts printed backwards: Name: Paul Rod Phone: 831-654-0090 Name: Nebula Walker Phone: 831-565-1010 Name: Julie Anderson Phone: 731-234-6543 Name: Amy Doe Phone: 831-534-2890 Name: John Casey Phone: 831-234-2323 Enter a full name: Amy Doe Their contact info is: Name: Amy Doe Phone: 831-534-2890
Program Code [C++]
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
class Contact
{
public:
Contact(string init_name = "", string init_phone = "000-000-0000");
void setName(string name);
void setPhone(string phone);
string getName()const;
string getPhone()const;
friend ostream& operator << (ostream& os, const Contact& c);
friend bool operator == (const Contact& c1, const Contact& c2);
friend bool operator != (const Contact& c1, const Contact& c2);
private:
string name, phone;
};
Contact::Contact(string init_name, string init_phone)
{
name = init_name;
phone = init_phone;
}
void Contact::setName(string name)
{
this->name = name;
}
void Contact::setPhone(string phone)
{
this->phone = phone;
}
string Contact::getName() const
{
return name;
}
string Contact::getPhone() const
{
return phone;
}
ostream& operator << (ostream& os, const Contact& c)
{
os << "Name: " << c.name << endl;
os << "Phone: " << c.phone << endl;
return os;
}
bool operator == (const Contact& c1, const Contact& c2)
{
return c1.name == c2.name;
}
bool operator != (const Contact& c1, const Contact& c2)
{
return c1.name != c2.name;
}
int main() {
// Creating an array of size 5 of Contacts
Contact contacts[5];
// Asking user for info as given in sample run
for (int i=0; i<5; i++) {
string name, phone;
cout << "Name: ";
getline(cin >> ws, name);
cout << "Phone: ";
getline(cin >> ws, phone);
cout << endl;
contacts[i] = Contact(name, phone);
}
// Now Showing list of contacts
cout << "List of contacts:" << endl;
for (int i=0; i<5; i++)
cout << contacts[i] << endl;
// Now printing list in reverse order
cout << "Contacts printed backwards: " << endl;
for (int i=4; i>=0; i--)
cout << contacts[i] << endl;
// Ask user for a name and displays its information
string n;
cout << "Enter a full name: " << endl;
getline(cin >> ws, n);
// Searchinng for name in array and if found, printing info
int index = -1;
for (int i=0; i<5; i++) {
if(contacts[i].getName() == n)
index = i;
}
if(index == -1)
cout << "Information not found";
else {
cout << "Their contact info is:" << endl;
cout << contacts[index] << endl;
}
return 0;
}
Sample Output:-
-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!