After posting my original address book program I took the responses and edited my code. I have used every suggestion given, the code works great, and now I am looking for further review and suggestions from the Code Review community.
Parent class (Person
):
//parent class
#ifndef PERSON
#define PERSON
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Person
{
public:
//input person variable
std::istream& read(std::istream&, Person &p1);
//output person variable
std::ostream& print(std::ostream& os, const Person &p1);
//comparison operator
bool operator<(const Person&) const;
private:
std::string name;
std::string address;
};
#endif
Person
member functions:
#include "Person.h"
//input for address book person element
std::istream& Person::read(std::istream& in, Person &p1) {
std::cin >> p1.name;
std::cin.ignore();
std::getline(std::cin, p1.address);
return in;
}
//operator for address book person element
std::ostream& Person::print(std::ostream& os, const Person &p1) {
os << "Name: " << p1.name << "\nAddress: " << p1.address << "\n\n";
return os;
}
bool Person::operator<(const Person& other) const {
return name < other.name;
}
Child class (Address Book):
//child class
#ifndef ADDRESS_BOOK
#define ADDRESS_BOOK
#pragma once
#include "Person.h"
class Address_book : public Person
{
friend std::istream& operator>>(std::istream&, Address_book&);
friend std::ostream& operator<<(std::ostream&, Address_book&);
public:
//set vector size to match appropriate number of entries
void num_of_ent(int, Address_book&);
void Address_book::sort(Address_book&,int);
private:
std::vector<Person> add_book;
};
#endif
Address book member functions:
#include "Address_book.h"
//sets vector size, creates person elements of vector
void Address_book::num_of_ent(int entries, Address_book& abook) {
for (int i = 0; i < entries; ++i) {
Person p;
abook.add_book.push_back(p);
}
}
//fills address book
std::istream& operator>>(std::istream& in, Address_book& abook) {
for (int i = 0; i < abook.add_book.size(); ++i) {
abook.add_book[i].read(in, abook.add_book[i]);
}
return in;
}
//sort address book
void Address_book::sort(Address_book& abook) {
std::sort(abook.add_book.begin(), abook.add_book.end());
}
//prints contents of address book
std::ostream& operator<<(std::ostream& os, Address_book& abook) {
for (int i = 0; i < abook.add_book.size(); ++i) {
abook.add_book[i].print(os, abook.add_book[i]);
}
return os;
}
Main:
#include <iostream>
#include <vector>
#include <string>
#include "Address_book.h"
#include "Person.h"
void pause();
int main() {
int entries = 0;
std::cout << "Enter number of entries to go in addressbook: ";
std::cin >> entries;
Address_book abook1;
abook1.num_of_ent(entries, abook1);
//input into addressbook
std::cout << "Enter the name, followed by the address:\n";
std::cin >> abook1;
//sort addressbook
abook1.sort(abook1, entries);
//output addressbook
std::cout << "\n";
std::cout << abook1;
pause();
}
void pause() {
std::string pause;
std::cout << "Press any key followed by enter to continue...";
std::cin >> pause;
}