Multiple objects calling a function

Hi all, in my code below, I have 2 objects, each with "age" member data. I have a method that is supposed to add those ages and convert them to another number, but I don't know how to get the method to run. My full program has 4 objects that have to run this method on every other one, like:

the " + " being the method determinAge()
man1 + man1
man1 + man2
man1 + man3
man1 + man4
man2 + man2
etc

Here's what I have now:

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
#include <iostream>
#include <sstream>

using namespace std;

class test
{
public:
   double age;
   static const double MAX_VAL;
   static const double SCALE_FACTOR;

   double determineAge(test guy1, test guy2);
};

double const test::MAX_VAL = 10;
double const test::SCALE_FACTOR = 1;

int main()
{
   test man1, man2;
   
   man1.age = 3;
   man2.age = 7;
   
   cout << man1.determineAge(man1, man2) << endl;
}

double test::determineAge(test guy1, test guy2)
{
   double diff, fit;

   diff = this->age - age;
   fit = MAX_VAL - SCALE_FACTOR - abs(diff);
   fit = fit / ((double)(MAX_VAL - SCALE_FACTOR));

   return fit;
}
Last edited on
Ok,
Basically you are not using the passed tests in the test::determineAge() function.
Maybe you want to use:
On line 33: diff =guy1.age - guy2.age;
Thank you!
Last edited on
Magically, incredibly, that worked! But, I think I'm close, but still way off. I had the this-> in there because we were instructed to use this-> specifically. Having the objects in the test::determineAge() function might not be correct... Any idea on how to get test::determineAge() function to work using this-> ?
Definitely!
Here's how:
on line 13 write: double determineAge(test* guy1, test* guy2);
On line 26 write: cout << man1.determineAge(&man1, &man2) << endl;
Now, on line 33 you can use the -> operator like this:
line 33: diff =guy1->age - guy2->age;
Does this solve your problem?
To give you a decent answer we really need a clearer explanation of the problem.

To start with, the determineAge() function doesn't appear to determine an age. Only once it's clear what it's supposed to to will it possible to decide whether it is better as a member function or otherwise.

Andy

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
#include <iostream>
using namespace std;

class Person {
private:
   static const double SHIFT_FACTOR;
   static const double SCALE_FACTOR;

public:
   double m_age;

   Person(double age) : m_age(age) {}

   // http://www.urbandictionary.com/define.php?term=half-your-age-plus-seven
   static bool closeEnoughInAgeToMarry(const Person& person1, const Person& person2);
};

const double Person::SHIFT_FACTOR = 7.0;
const double Person::SCALE_FACTOR = 2.0;

int main() {
    std::cout << std::boolalpha;

    Person person1(42), person2(28), person3(21);

    cout << Person::closeEnoughInAgeToMarry(person1, person2) << endl;
    cout << Person::closeEnoughInAgeToMarry(person2, person3) << endl;
    cout << Person::closeEnoughInAgeToMarry(person3, person1) << endl;

    return 0;
}

bool Person::closeEnoughInAgeToMarry(const Person& person1, const Person& person2) {
   double min_age1 = person1.m_age / SCALE_FACTOR + SHIFT_FACTOR;
   double min_age2 = person2.m_age / SCALE_FACTOR + SHIFT_FACTOR;

   double max_age1 = (person1.m_age - SHIFT_FACTOR) * SCALE_FACTOR;
   double max_age2 = (person2.m_age - SHIFT_FACTOR) * SCALE_FACTOR;

   return    (person2.m_age >= min_age1) && (person2.m_age <= max_age1)
          && (person1.m_age >= min_age2) && (person1.m_age <= max_age2);
}
Both of you have been a huge help! You've answered the questions you thought I was asking and have answered questions I didn't know I had. You guys did a fantastic job of deciphering the question I didn't have the words to ask.
No problem!
Topic archived. No new replies allowed.