Not sure what these functions are supposed to do.

I am not entirely sure what im supposed to make this program do. I do know that on the main cpp file, it compares to inputs of feet and inches

Enter feet: 4
Enter inches: 2

Enter second length of feet: 5
Enter second length of inches: 4:



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
// Chapter 14, Programming Challenge 9: FeetInches Modification
#include <iostream>
#include "FeetInches.h"
using namespace std;

int main()
{
   // Create three FeetInches objects.
   FeetInches one, two;

   // Get a distance for the first object.
   cout << "Enter a distance in feet and inches: " << endl;
   cin >> one;
   
   // Get a distance for the second object.
   cout << "Enter another distance in feet and inches: " << endl;
   cin >> two;

   // Test the != operator.
   if (one != two)
      cout << "The two are not equal.\n";

   // Test the >= operator.
   if (one >= two)
      cout << one << " is >= " << two << endl;
   
   // Test the <= operator.
   if (one <= two)
      cout << one << " is <= " << two << endl;
   
   return 0;
}

// Specification file for the FeetInches class
#ifndef FEETINCHES_H
#define FEETINCHES_H

#include <iostream>
using namespace std;

class FeetInches; // Forward Declaration

// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const FeetInches &);
istream &operator >> (istream &, FeetInches &);

// The FeetInches class holds distances or measurements 
// expressed in feet and inches.

class FeetInches
{
private:
   int feet;        // To hold a number of feet
   int inches;      // To hold a number of inches
   void simplify(); // Defined in FeetInches.cpp
public:
   // Constructor
   FeetInches(int f = 0, int i = 0)
      { feet = f;
        inches = i;
        simplify(); }

   // Mutator functions
   void setFeet(int f)
      { feet = f; }

   void setInches(int i)
      { inches = i;
        simplify(); }

   // Accessor functions
   int getFeet() const
      { return feet; }

   int getInches() const
      { return inches; }

   // Overloaded operator functions
   FeetInches operator + (const FeetInches &);
   FeetInches operator - (const FeetInches &);
   FeetInches operator ++ ();    // Prefix ++
   FeetInches operator ++ (int); // Postfix ++
   bool operator > (const FeetInches &);
   bool operator < (const FeetInches &);
   bool operator == (const FeetInches &);

   // Conversion functions
   operator double();
   operator int();
   
   // Friends
   friend ostream &operator << (ostream &, const FeetInches &);
   friend istream &operator >> (istream &, FeetInches &);
   
   // New operators
   // >= 
   bool operator >=(const FeetInches &);
   // <=
   bool operator <=(const FeetInches &);
   // != 
   bool operator !=(const FeetInches &);
};

#endif



simplify() - adjusts number entered into as inches that are equal or higher than 12 or below 0 i.e 5 ft 13 inches == 6 ft 1


I get what the acessors and setters do as well as the constructors.
Overloading the >, <, == compares the two, and overloading istream and ostream is obvious. But what do the others do, im unsure. Why would the >=, <=, matter?
Last edited on
It does look like you have to implement the "new" member functions.

Think this way: the FeetInches is a number just like int. How do ints behave with relational operators? Make FeetInches behave similarly.

One does not actually need more than the relational operators < and == but if one must, one can implement the others with those two. For example, greater-or-equal should be true when less is not true.
Yes this is one of a few assignments I have to complete. This one is just confusing me. I must write the code for these new functions.

I get for the most part what this program is trying to achieve but the relational operator functions are confusing me. A FeetInches length cannot be > and >=, right?

Also what would the overloaded operator functions even do? +,-, ++
Last edited on
A FeetInches length cannot be > and >=, right?


Why not? 5 is > and >= to 4 isn't it? Apply the same logic to FeetInches.

Also what would the overloaded operator functions even do? +,-, ++

Think about how you would use these operators normally.

+ should add two FeetInches (example: 3 ft. 4 inches + 7 ft. 2 inches)
- should do something similar accept with subtraction
++ should increment a the value, most like by inches (3 ft. 2 inches becomes 3 ft. 3 inches)
Thanks, this one wasn't obvious to me. That does seem like the only thing you could do with these operator functions.

It just doesn't make sense that you would need to write both > and >=. But I guess im thinking way to much into this.

Another question:

If I want to see if a string that the user inputs is in an array of strings, how would I do this. A link in the right direction is good enough.
Last edited on
Topic archived. No new replies allowed.