I need some help with this C++ assignment... I am not even sure where to begin.
ID: 3844527 • Letter: I
Question
I need some help with this C++ assignment... I am not even sure where to begin.
C++(not 11, preferably)
Write a program that will act as a sort of record keeper that can store Name, Phone Number, Address, E-Mail Address, Website Address etc. of many people to a file. The records can be accessed again and can be searched to find a specified person. Use the Standard Template Library (STL) to accomplish this project.
NOTE! - This project will need to utilize good design, such as separation of interface from the implementation (ie. classes separated out into separate files, with .h files).
There should be minimal processing in main().
Explanation / Answer
Hi,
There are three files main.cpp, Person.h and Person.cpp.
main.cpp
====
//============================================================================
// Name : person_to_file.cpp
// Author : Ramachandra jr
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <vector>
#include "Person.h"
using namespace std;
int find(vector<Person>, string);
int main() {
// Container for holding all Person objects.
vector<Person> people;
// Struct for holding persons details.
PersonDetails pd1;
pd1.name = "Rj";
pd1.phone = "0101";
pd1.address = "Some street";
pd1.email = "rj@allday.com";
pd1.website = "rjthehero:p.com";
// New person with the above details.
Person p1(pd1);
// Add to the vector.
people.push_back(p1);
// Find a person by name.
cout << find(people, "Rj") << endl;
return 0;
}
// Finds a person and returns it index.
int find(vector<Person> pv1, string name)
{
for (unsigned int i = 0; i < pv1.size(); ++i)
{
if (pv1.at(i).get_name() == name)
{
return i;
}
}
return -1;
}
Person.h
====
/*
* Person.h
*
* Created on: 28-May-2017
* Author: ajay
*/
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
using namespace std;
struct PersonDetails
{
string name, phone, address, email, website;
};
class Person
{
private:
string _name;
string _phone;
string _address;
string _email;
string _website;
public:
Person();
Person(PersonDetails p1);
virtual ~Person();
string get_name();
friend ostream & operator << (ostream &, Person &);
};
#endif /* PERSON_H_ */
Person.cpp
====
/*
* Person.cpp
*
* Created on: 28-May-2017
* Author: ajay
*/
#include "Person.h"
#include <iostream>
Person::Person()
{
// Init all
_name = "";
_phone = "";
_address = "";
_email = "";
_website = "";
}
Person::Person(PersonDetails pd1)
{
_name = pd1.name;
_phone = pd1.phone;
_address = pd1.address;
_email = pd1.email;
_website = pd1.website;
}
Person::~Person()
{}
string Person::get_name()
{
return this->_name;
}
ostream & operator << (ostream &os, Person &p1)
{
os << p1._name;
return os;
}
You can improve the find function to get you pieces of object / whole object . Please comment incase you have any issues with the above code.