Facebook
From Mature Goat, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 52
  1. #include <iostream>
  2. #include <fstream>
  3. #include "PhoneCall.h"
  4. using namespace std;
  5.  
  6. PhoneCall::PhoneCall() { //default constructor
  7.         number = " ";
  8.         length = 0;
  9.         rate = 0;
  10. }
  11.  
  12. PhoneCall::PhoneCall(string new_number) { //overloaded constructor
  13.         number = new_number;
  14.         length = 0;
  15.         rate = 0;
  16. }
  17.  
  18. PhoneCall::~PhoneCall() {  //destructor
  19.         ;
  20. }
  21.  
  22. string PhoneCall::get_number() const {
  23.         return number;
  24. }
  25.  
  26. int PhoneCall::get_length() const {
  27.         return length;
  28. }
  29.  
  30. float PhoneCall::get_rate() const {
  31.         return rate;
  32. }
  33.  
  34. float PhoneCall::calcCharge() { ///datermine cost of call
  35.         return length * rate;
  36. }
  37.  
  38. bool operator==(const PhoneCall& call1, const PhoneCall& call2) {
  39.         return(call1.number == call2.number);
  40. }
  41.  
  42. istream& operator >> (istream& ins, PhoneCall& the_call) {
  43.         ins >> the_call.number >> the_call.length >> the_call.rate;
  44.         return ins;
  45. }
  46.  
  47. ostream& operator << (ostream& outs, const PhoneCall& the_call) {
  48.         outs << the_call.number << endl << the_call.length << endl << the_call.rate;
  49.         return outs;
  50. }
  51.  
  52. #pragma once
  53. #ifndef PHONECALL_H
  54. #define PHONECALL_H
  55. #include <iostream>
  56. #include <fstream>
  57. #include <cstdlib>
  58. #include <iomanip>
  59. using namespace std;
  60.  
  61. class PhoneCall {
  62. public:
  63. //the difference between the overloaded friend functions for the stream extraction operator >> and the stream insertion
  64. //operation. The overloaded stream insertion operator << uses the const keyword to ensure that the object which is
  65. //sent to the output stream will not be modified. In the case of the overloaded stream extraction operator >>,
  66. //the object retrieved from the input stream, must change. The overloaded stream insertion operator << modifies the
  67. //output stream and the overloaded stream extraction operator >> modifies the input stream, therefore both the
  68. //ostream and the istream must be reference parameters
  69.         friend istream& operator >> (istream& ins, PhoneCall& the_call);
  70.         friend ostream& operator << (ostream& outs, const PhoneCall& the_call);
  71. //the const keyword is used to guaratee that this function will not modify the PhoneCall objects.
  72. //When we compared two PhoneCall objects with ==, we do not want to change or modify the objects we
  73. //compare. A friend function may manipulate the underlying structure of the class but the const
  74. //keyword ensures that these manipulations do not modify the object in any possible way
  75.         friend bool operator == (const PhoneCall& call1, const PhoneCall& call2);
  76.         PhoneCall();  //default constructor
  77.         PhoneCall(string new_number); //overloaded constructor
  78.         ~PhoneCall(); //destructor
  79.         string get_number() const; //accessor
  80.         int get_length() const; //accessor
  81.         float get_rate() const; //accessor
  82.         float calcCharge(); //calculate amount charged for this call
  83. private:
  84.         string number;  //member variables
  85.         int length;
  86.         float rate;
  87. };
  88.  
  89. #endif PHONECALL_H