Moving a method

closed account (GL1Rko23)
I am trying to move this method from one cpp file to another, but when I do I am getting all kinds of errors, could someone show me the proper way to move the method "amountFor". This is not homework, I am doing it from a book but the book is in Java.

Customer.cpp

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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cout;
using std::istringstream;
using std::string;
using std::vector;

void Customer::amountFor(Rental & aRental, double & thisAmount)
{
    switch (aRental.getMovie().getPriceCode()){
        case Movie::REGULAR:
            thisAmount += 2;
            if(aRental.getDaysRented() > 2)
                thisAmount += (aRental.getDaysRented() - 2) * 1.5;

            break;
        case Movie::NEW_RELEASE:
            thisAmount += aRental.getDaysRented() * 3;
            break;
        case Movie::CHILDRENS:
            thisAmount += 1.5;
            if(aRental.getDaysRented() > 3)
                thisAmount += (aRental.getDaysRented() - 3) * 1.5;

            break;
    }
}

string Customer::statement()
{
  double totalAmount          = 0;
  int    frequentRenterPoints = 0;

  // Init result string
  string result("Rental Record for ");
  result += getName();
  result += "\n";

  for (vector<Rental>::iterator it = _rentals.begin();
                                it != _rentals.end(); ++it)
  {
      double thisAmount = 0;
      Rental& each = *it;
      amountFor(each, thisAmount);
      // Add frequent renter's points
        ++frequentRenterPoints;

        // Add bonus for new release rental
        if ( each.getMovie().getPriceCode() == Movie::NEW_RELEASE &&
             each.getDaysRented() > 1 )
            ++frequentRenterPoints;

        // Include in result
        result += "\t";
        result += each.getMovie().getTitle();
        result += "\t";

        // Convert int to a string.
        // Output the int object into the output string stream.
        std::ostringstream out_str_stream;
        out_str_stream << thisAmount;
        result += out_str_stream.str();
        result += "\n";
        totalAmount += thisAmount;
    }
    result += "Amount owed is: ";

     // Convert int to a string.
     // Output the int object into the output string stream.
    std::ostringstream out_str_stream;
    out_str_stream << totalAmount;
    result += out_str_stream.str();
    result += "\n";
    result += "You earned: ";
    std::ostringstream out_str_stream2;
    out_str_stream2 << frequentRenterPoints;
    result += out_str_stream2.str();
    result += " frequent renter points\n";

    return result;
}


And I want to move the "amountFor" method into here:

1
2
3
4
5
/*  Rental implementation
 */

#include "rental.h"



Here is the customer .h:

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
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include "rental.h"

#include <string>
#include <vector>

class Customer
{
  public:
    // Constructors
    Customer(std::string name)
      : _name(name)
    { }

    // Accessors
    std::string getName() { return _name; }

    // Mutators
    void addRental(Rental rental)
      { _rentals.push_back(rental); }

   // Other
   std::string statement();

  private:
    std::string         _name;
    std::vector<Rental> _rentals;
    void amountFor(Rental & aRental, double & thisAmount);



};

#endif 




and the rental.h


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
#ifndef RENTAL_H
#define RENTAL_H

#include "movie.h"

class Rental
{
  public:
    // Comstructors
    Rental(Movie movie, int days_rented)
      : _movie(movie), _days_rented(days_rented)
    { }

    // Accessors
    int   getDaysRented() { return _days_rented; }
    Movie getMovie()      { return _movie;       }

    // Mutators

  private:
    Movie _movie;
    int   _days_rented;
};

#endif 
Last edited on
If you post because you're getting error messages, it's usually helpful to post the error messages.

You'll need to include Customer.h in rental.cpp if you move the function there.
Topic archived. No new replies allowed.