1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <iostream>
#include <map>
#include <string>
#include "AddressBook.hpp"
int main(int argc, const char * argv[]) {
std::map<AddressBook, int> info;
info[AddressBook("James","Farrow","Acacia Grove", 11)] = 47;
info[AddressBook("Ethan","Farrow","Acacia Grove", 11)] = 8;
info[AddressBook("Ruby","Farrow","Acacia Grove", 11)] = 6;
info[AddressBook("Sarah","Holdcroft","Acacia Grove", 11)] = 37;
//if(info.find("James") != info.end()){
//
//}
for(std::map<AddressBook, int>::iterator it = info.begin(); it != info.end();){
it->first.print();
std::cout << "Age: " << it->second << std::endl;
it++;
}
return 0;
}
#ifndef AddressBook_hpp
#define AddressBook_hpp
#include <string>
#include <iostream>
class AddressBook {
public:
AddressBook();//deafualt constructor for map
AddressBook(const AddressBook & other);// copy constructor
AddressBook(std::string, std::string, std::string, int);// constructor
void setFirstName();
std::string getFirstName() const;
void setSecondName();
std::string getSecondName() const;
void setAddress();
std::string getAddress()const;
void setHouseNuber();
int getHouseNumber() const;
void print() const;
//overload assignment operator for map to order objects
//overload assignment operator for map to order objects
bool operator<(const AddressBook &other) const {
if( secondName == other.secondName){
return firstName < other.firstName;
}
else{
return firstName < other.firstName;
}
}
private:
int houseNumber;
std::string firstName;
std::string secondName;
std::string address;
};
#endif /* AddressBook_hpp */
//this constructor is required for map to be able to construct objects
//if not present program will not compile
AddressBook::AddressBook(): firstName(""), secondName(""), address(""), houseNumber(0){
}
//copy constructor
AddressBook::AddressBook(const AddressBook &other){
//std::cout << "copy" << std::endl;
firstName = other.firstName;
secondName = other.secondName;
address = other.address;
houseNumber = other.houseNumber;
}
//standard constructor
AddressBook::AddressBook(std::string first, std::string second, std::string addr, int num)
:firstName(first), secondName(second), address(addr), houseNumber(num){
}
//print the information;
void AddressBook::print()const{
std::cout << firstName << " " << secondName << " "
<< houseNumber << " " << address << " ";
}
//set the firstname of object
void AddressBook::setFirstName(){
std::cout << "First name: ";
std::getline(std::cin, firstName, '\n');
}
std::string AddressBook::getFirstName() const{
return firstName;
}
void AddressBook::setSecondName(){
std::cout << "Second name: ";
std::getline(std::cin, secondName, '\n');
}
std::string AddressBook::getSecondName() const{
return secondName;
}
void AddressBook::setAddress(){
std::cout << "Address: ";
std::getline(std::cin, address, '\n');
}
std::string AddressBook::getAddress() const{
return address;
}
void AddressBook::setHouseNuber(){
std::cout << "house Number: ";
std::cin >> houseNumber;
}
int AddressBook::getHouseNumber()const {
return houseNumber;
}
|