The attaced files are the starter code that presents some of the concepts needed
ID: 3740736 • Letter: T
Question
The attaced files are the starter code that presents some of the concepts needed to complete Lab. Pay particular attention to M.I.L. - Member Initialization List
__________________________letter.h___________________________________
#ifndef LETTER_H
#define LETTER_H
#include "address.h"
#include <string>
#include <fstream>
using namespace::std;
class letter
{
public:
letter(void);
letter(const letter &);
letter(const address & sentBy, const address & sentTo, double theWeight);
letter(const string & senderName, const string & senderZip,
const string & toName, const string & toZip,
double theWeight);
~letter(void);
address getSender(void) const;
address getTo(void) const;
double getWeight(void) const;
string getSenderName(void) const;
string getSenderZip(void) const;
string getToName(void) const;
string getToZip(void) const;
letter & setSender(const address &);
letter & setTo(const address &);
letter & setWeight(double);
const letter & operator = (const letter & right);
double getPostageCost(void) const; // assume NO ERROR CONDITIONS
/*
Weight Not Over Rates
(ounces)
1 0.44
2 0.61
3 0.78
3.5 0.95
*/
void print(ostream &) const;
// output will be of the form:
// From:
// John Wilson
// 66210
// To:
// Mary Jones
// 66212
// Cost: $0.95
private:
address from;
address to;
double weight; // weight in ounces
};
#endif
__________________________________address.h________________________
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
#include <iostream>
using namespace::std;
class address
{
public:
address(void);
address(const address &);
address(const string & theName, const string & theZip);
~address(void);
string getName(void) const;
string getZip(void) const;
address & setName(const string & theName);
address & setZip(const string & theZip);
const address & operator=(const address & right);
void print(ostream &) const;
// output will be of the form:
// John Wilson
// 66210
private:
string name;
string zip;
};
#endif
Explanation / Answer
//main.cpp
#include "address.h"
#include "letter.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace::std;
int main()
{
// include code to test your member functions as you go along
address sample("JCCC","66210");
sample.print(cout);
// after you have tested your functions, execute the following
ofstream creditChargeFile;
creditChargeFile.open("./charges.txt");
letter saleNotice("The Bookstore","66210","Bob Marley","64114", 3.1);
saleNotice.print(cout,"5401 8211 4567 9912",creditChargeFile);
creditChargeFile.close();
return 0;
}
----------------------------------------------------------------------------------------
//address.cpp
#include "address.h"
address::address()
{
this->name = "John Doe";
this->zip = "66203";
}
address::address(const address& other)
{
this->name = other.name;
this->zip = other.zip;
}
address::address(const string& theName, const string& theZip)
{
this->name = theName;
this->zip = theZip;
}
address::~address()
{
// *whistles*
// nothin' to do 'ere.
}
address& address::setName(const string& theName)
{
this->name = theName;
return *this;
}
address& address::setZip(const string& theZip)
{
this->zip = theZip;
return *this;
}
string address::getName() const
{
return this->name;
}
string address::getZip() const
{
return this->zip;
}
const address& address::operator=(const address& right)
{
this->name = right.name;
this->zip = right.zip;
return *this;
}
void address::print(ostream& output) const
{
output << this->name << endl << this->zip << endl;
}
--------------------------------------------------------------------------
//address.h
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
#include <iostream>
using namespace::std;
class address
{
public:
address(void);
address(const address &);
address(const string & theName, const string & theZip);
~address(void);
string getName(void) const;
string getZip(void) const;
address & setName(const string & theName);
address & setZip(const string & theZip);
const address & operator=(const address & right);
void print(ostream &) const;
// output will be of the form:
// John Wilson
// 66210
private:
string name;
string zip;
};
#endif
-------------------------------------------------------------------------------
//letter.cpp
#include "letter.h"
letter::letter()
{
this->weight = 0.0;
this->to = address();
this->from = address();
}
letter::letter(const letter& other)
{
this->weight = other.weight;
this->to = other.to;
this->from = other.from;
}
letter::letter(const address& sentBy, const address& sentTo, double theWeight)
{
this->weight = theWeight;
this->to = sentTo;
this->from = sentBy;
}
letter::letter(const string& senderName, const string& senderZip, const string& toName, const string& toZip, double theWeight)
{
this->weight = theWeight;
this->to = address(toName, toZip);
this->from = address(senderName, senderZip);
}
letter::~letter()
{
// nothing to do here, yet.
}
address letter::getSender() const
{
return this->from;
}
address letter::getTo() const
{
return this->to;
}
string letter::getSenderName() const
{
return this->from.getName();
}
string letter::getToName() const
{
return this->to.getName();
}
string letter::getSenderZip() const
{
return this->from.getZip();
}
string letter::getToZip() const
{
return this->to.getZip();
}
letter& letter::setSender(const address& sender)
{
this->from = sender;
return *this;
}
letter& letter::setTo(const address& to)
{
this->to = to;
return *this;
}
letter& letter::setWeight(double weight)
{
this->weight = weight;
return *this;
}
const letter& letter::operator= (const letter& right)
{
this->from = right.from;
this->to = right.to;
this->weight = right.weight;
return *this;
}
double letter::getPostageCost() const
{
if (weight < 1.0)
return 0.44;
else if (weight < 2.0)
return 0.61;
else if (weight < 3.0)
return 0.78;
else
return 0.95;
}
void letter::print(ostream& output) const
{
output << "FROM:" << endl;
this->from.print(output);
output << endl << "TO:" << endl;
this->to.print(output);
output << endl;
}
void letter::print(ostream& output, const string& creditCardNumber, ofstream& chargeFile) const
{
this->print(output);
chargeFile << creditCardNumber << ' ' << "TOTAL CHARGE: " << (this->getPostageCost() * 0.90) << endl;
}
-----------------------------------------------------------------------------------------
//letter.h
#ifndef LETTER_H
#define LETTER_H
#include "address.h"
#include <string>
#include <fstream>
using namespace::std;
class letter
{
public:
letter(void);
letter(const letter &);
letter(const address & sentBy, const address & sentTo, double theWeight);
letter(const string & senderName, const string & senderZip,
const string & toName, const string & toZip,
double theWeight);
~letter(void);
address getSender(void) const;
address getTo(void) const;
double getWeight(void) const;
string getSenderName(void) const;
string getSenderZip(void) const;
string getToName(void) const;
string getToZip(void) const;
letter & setSender(const address &);
letter & setTo(const address &);
letter & setWeight(double);
const letter & operator = (const letter & right);
double getPostageCost(void) const; // assume NO ERROR CONDITIONS
void print(ostream &) const;
private:
address from;
address to;
double weight; // weight in ounces
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.