Skip to content

Instantly share code, notes, and snippets.

/Address.cpp Secret

Created September 5, 2014 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/08ff6c5284c179c9a323 to your computer and use it in GitHub Desktop.
Save anonymous/08ff6c5284c179c9a323 to your computer and use it in GitHub Desktop.
#include "Address.h"
using namespace std;
Address::Address(const string street, const string city, const string state, const string zipcode) : street(street), city(city), state(state), zipcode(zipcode)
{}
string Address::getStreet() const{
return street;
}
string Address::getCity() const{
return city;
}
string Address::getState() const{
return state;
}
string Address::getZipcode() const{
return zipcode;
}
void Address::setStreet(const string street){
this->street = street;
}
void Address::setCity(const string city){
this->city = city;
}
void Address::setState(const string state){
this->state = state;
}
void Address::setZipcode(const string zipcode){
this->zipcode = zipcode;
}
void Address::setAddress(const string street, const string city, const string state, const string zipcode) {
setStreet(street);
setCity(city);
setState(state);
setZipcode(zipcode);
}
#ifndef __hw02__Address__
#define __hw02__Address__
#include <string>
using namespace std;
class Address{
private:
string street;
string city;
string state;
string zipcode;
public:
Address(const string street = "default", const string city = "default", const string state = "default", const string zipcode = "default");
string getStreet() const;
string getCity() const;
string getState() const;
string getZipcode() const;
void setStreet(const string street);
void setCity(const string city);
void setState(const string state);
void setZipcode(const string zipcode);
void setAddress(const string street, const string city, const string state, const string zipcode);
};
#endif /* defined(__hw02__Address__) */
#include "Employee.h"
using namespace std;
Employee::Employee(const int eid, Address homeAddress, Person name) :eid(eid), homeAddress(homeAddress),
name(name) {}
string Employee::getAddress() const {
string temp = homeAddress.getStreet() + " " + homeAddress.getCity() + ", "
+ homeAddress.getState() + " " + homeAddress.getZipcode();
return temp;
}
string Employee::getName() const {
string temp = name.getFirst() + " " + name.getLast();
return temp;
}
int Employee::getEid() const {
return eid;
}
void Employee::setAddress(const string& street, const string& city, const string& state, const string& zipcode) {
Employee::homeAddress.setAddress(street, city, state, zipcode);
}
void Employee::setName(const string& first, const string& last) {
Employee::name.setName(first, last);
}
void Employee::setEid(const int eid) {
this->eid = eid;
}
#ifndef __hw02__Employee__
#define __hw02__Employee__
#include "Address.h"
#include "Person.h"
#include <string>
class Employee {
private:
Address homeAddress;
Person name;
int eid;
public:
Employee(int eid = 0, Address homeAddress = Address(), Person name = Person());
string getAddress() const;
string getName() const;
int getEid() const;
void setAddress(const string& street, const string& city, const string& state, const string& zipcode);
void setName(const string& first, const string& last);
void setEid(const int eid);
};
#endif /* defined(__hw02__Employee__) */
#include <iostream>
#include <ostream>
#include <sstream>
#include <fstream>
#include <string>
#include "Employee.h"
using namespace std;
bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);
int readFromFile(ifstream& in, Employee empArray[]);
void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);
int main(int argc, const char * argv[])
{
ifstream fin;
ofstream fout;
if(!openFileForReading(fin, "employeesIn.txt")) {
cerr << "Error opening employeesIn.txt for reading." << endl;
exit(1);
}
if(!openFileForWriting(fout, "employeesOut.txt")) {
cerr << "Error opeing employeesOut.txt for writing." << endl;
exit(1);
}
Employee employeeArray[50];
int employeeCount = readFromFile(fin, employeeArray);
fin.close();
writeToFile(fout, employeeArray, employeeCount);
fout.close();
cout << "Program successful." << endl << endl;
return 0;
}
bool openFileForReading(ifstream& fin, const string& filename) {
fin.open("employeesIn.txt");
return (fin.is_open());
}
bool openFileForWriting(ofstream& fout, const string& filename) {
fout.open("employeesOut.txt");
return (fout.is_open());
}
int readFromFile(ifstream& in, Employee empArray[]) {
int temp = 0;
string eidText;
string first;
string last;
string street;
string city;
string state;
string zipcode;
while(!in.eof()) {
getline(in, eidText, ',');
getline(in, first, ',');
getline(in, last, ',');
getline(in, street, ',');
getline(in, city, ',');
getline(in, state, ',');
getline(in, zipcode, ',');
empArray[temp].setEid(stoi(eidText));
empArray[temp].setName(first, last);
empArray[temp].setAddress(street, city, state, zipcode);
temp++;
}
return temp;
}
void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {
for (int i = 0; i < numberOfEmployees; i++){
out << "Employee Record: " << empArray[i].getEid()
<< endl
<< "Name: " << empArray[i].getName()
<< endl
<< "Home Address: " << empArray[i].getAddress()
<< endl
<< endl;
}
}
#include <iostream>
#include <sstream>
#include "Person.h"
using namespace std;
Person::Person(const string first, const string last) : first(first), last(last)
{}
string Person::getFirst() const{
return first;
}
string Person::getLast() const{
return last;
}
void Person::setFirst(const string first) {
this->first = first;
}
void Person::setLast(const string last) {
this->last = last;
}
void Person::setName(const string first, const string last) {
setFirst(first);
setLast(last);
}
#ifndef __hw02__Person__
#define __hw02__Person__
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
// declarations
string first;
string last;
public:
//default constructor for Person
Person(const string first = "default", const string last = "default");
string getFirst() const;
string getLast() const;
// sets the first name of Person
void setFirst(const string first);
// sets the last name of person
void setLast(const string last);
// sets the name, using both first and last
void setName(const string first, const string last);
};
#endif /* defined(__hw02__Person__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment