MUST USE GETLINE IF YOU`RE USING IF YOU NEED TO GET SOMETHING. First I have writ
ID: 3913222 • Letter: M
Question
MUST USE GETLINE IF YOU`RE USING IF YOU NEED TO GET SOMETHING.
First I have written a header file for you for a class called Friend. (Note that we can only call this class Friend if we use capitalization, since the word friend is reserved.) This class has have two private variables, one for the friend’s name and the other for their birthday, the later being of type Date, another class that I have given you. You are to write the implementation of this class, including overloaded insertion and extraction operators as well operators for = = and !=. Two friends are “equal” only if they have the same name and the same birthday. (Doctors’ offices do this because of the low probability that two people will have both the same name and the same birthday.)
Test this class by writing a main of your own that declares a two friends, lets you put the information into both, outputs them to the screen and compares them for equality.
Now, in the main that I have given you, you will find that the application allows:
The addition of a new friend to the beginning of the list
The viewing of all the friends in the list
The ability to walk through the list, viewing one friend at a time
The ability to remove a friend from the list (de-friending someone)
The ability to insert a new friend at some spot in the middle of the list, which includes at the back end
The ability to sort all the friends by their birthdays
The ability to find a friend using their name
There is also a file backup mechanism. In this case it operates using the person’s username for the name of the file. {OVER}
I have given you the header file for the container class that makes all of this possible, fbfriends.h. You are to write the implementation of this as well. The private variables for this class consist of a pointer of type Friend and variables for capacity, used, and current_index. The constructor will begin by allocating a dynamic array of Friends capable of holding only five friends. (This will save on memory space for those users with few friends.) When the action of adding an additional friend to the list happens you should check if used == capacity, and if it does do a resize operation, that increases the size of the array by five.
This container also has an internal iterator, as the author illustrated in section 3.2 of the text, which will require that you write the functions
start
advance
is_item
current
remove_current
insert
attach
and you will find that these are implemented in the same way as they are in the text. You will also need to implement show_all – used mostly for testing purposes, bday_sort, find_friend, and is_friend.
Because this is a dynamic array you will need to write a resize function, and the Big 3 (destructor, copy constructor, and assignment operator). And because we’re providing file backup we will also have functions for load and save.
date.h*****************************************
friend.h**************************************************
fbfriends.h***********************************************
main.cc***********************************************************
rplan.txt*************************************
Explanation / Answer
here is your file : ------------>>>>>>>>>>
Friend.cpp : --------->>>>>>
#include "friend.h"
#include<sstream>
Friend::Friend(){
name = "";
bday = Date();
}
std::string Friend::get_name()const{
return name;
}
Date Friend::get_bday()const{
return bday;
}
bool Friend::operator==(const Friend &f)const{
return name == f.get_name() && bday == f.get_bday();
}
bool Friend::operator !=(const Friend &f)const{
return !(*this == f);
}
void Friend::input(std::istream &ins){
ins.ignore();
getline(ins,name);
ins.ignore();
int d,m,y;
string dt;
getline(ins,dt);
stringstream ss;
for(int i = 0;i<dt.length();i++){
if(dt[i] == '/'){
ss<<" ";
}else{
ss<<dt[i];
}
}
ss>>d>>m>>y;
ss.clear();
bday = Date(d,m,y);
}
void Friend::output(std::ostream &ous)const{
ous<<name<<" "<<bday;
}
std::ostream& operator<<(std::ostream &ous,const Friend &f){
f.output(ous);
return ous;
}
std::istream& operator>>(std::istream &ins,Friend &f){
f.input(ins);
return ins;
}
FBFriends.cpp : ---------->>>>>>>
#include "fbfriends.h"
FBFriends::FBFriends(){
data = new Friend[5];
used = 0;
capacity = 5;
current_index = -1;
}
FBFriends::~FBFriends(){
delete[] data;
}
FBFriends::FBFriends(const FBFriends &other){
*this = other;
}
void FBFriends::operator=(const FBFriends &other){
if(this == &other){
return;
}
delete[] data;
capacity = other.capacity;
data = new Friend[capacity];
used = other.used;
for(int i = 0;i<used;i++){
data[i] = other.data[i];
}
}
void FBFriends::start(){
current_index = 0;
}
void FBFriends::advance(){
if(current_index < used-1)
current_index++;
}
Friend FBFriends::current(){
return data[current_index];
}
bool FBFriends::is_item(){
return current_index > 0 && current_index < used;
}
void FBFriends::remove_current(){
if(is_item()){
for(int i = current_index;i<used-1;i++){
data[i] = data[i+1];
}
used--;
}
}
void FBFriends::insert(const Friend &f){
if(!is_item()){
return;
}
if(used >= capacity){
resize();
}
for(int i = used;i>current_index;i--){
data[i] = data[i-1];
}
data[current_index] = f;
}
void FBFriends::attach(const Friend &f){
if(used >= capacity){
resize();
}
data[used++] = f;
}
void FBFriends::show_all(std::ostream &ous){
for(int i = 0;i<used;i++){
ous<<data[i]<<endl;
}
}
Friend FBFriends::find_friend(const std::string &name)const{
for(int i = 0;i<used;i++){
if(data[i].get_name() == name){
return data[i];
}
}
}
bool FBFriends::is_friend(const Friend &f){
for(int i = 0;i<used;i++){
if(f == data[i]){
return true;
}
}
return false;
}
void FBFriends::load(std::istream &ins){
Friend temp;
while(!ins.eof()){
ins>>temp;
attach(temp);
}
}
void FBFriends::save(std::ostream &ous){
for(int i = 0;i<used;i++){
ous<<data[i]<<endl;
}
}
void FBFriends::bday_sort(){
Friend temp;
for(int i = 0;i<used;i++){
for(int j = i+1;j<used;j++){
if(data[i].get_bday() > data[j].get_bday()){
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
void FBFriends::resize(){
capacity = capacity + 5;
Friend *temp = new Friend[capacity];
for(int i = 0;i<used;i++){
temp[i] = data[i];
}
delete[] data;
data = temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.