Skip to main content
deleted 135 characters in body
Source Link
chris360
  • 725
  • 6
  • 16
//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);
    //get name
    std::string& get_name(Person& p);
    //comparison operator
    bool operator<(const Person&) const;
private:
    std::string name;
    std::string address;
};

#endif 
#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;
}

std::string& Person::get_name(Person& p) {
    return p.name;
}

bool Person::operator<(const Person& other) const {
    return name < other.name;
}
//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);
    //get name
    std::string& get_name(Person& p);
    //comparison operator
    bool operator<(const Person&) const;
private:
    std::string name;
    std::string address;
};

#endif 
#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;
}

std::string& Person::get_name(Person& p) {
    return p.name;
}

bool Person::operator<(const Person& other) const {
    return name < other.name;
}
//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 
#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;
}
added 22 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Simple address book in C++ (Revised)- follow-up

Parent Class:(Person)Parent class (Person):

Person Member Functions:Person member functions:

Child Class:(Address Book)Child class (Address Book):

Address Book Member Functions:Address book member functions:

Main:Main:

Simple address book C++ (Revised)

Parent Class:(Person)

Person Member Functions:

Child Class:(Address Book)

Address Book Member Functions:

Main:

Simple address book in C++ - follow-up

Parent class (Person):

Person member functions:

Child class (Address Book):

Address book member functions:

Main:

deleted 13 characters in body
Source Link
chris360
  • 725
  • 6
  • 16
#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, int entries) {
    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;
}
#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, int entries) {
    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;
}
#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;
}
Source Link
chris360
  • 725
  • 6
  • 16
Loading