1

let's say i have a class of type human and i want to create a method function called jim.punch(billy); i created jim and i created billy. how do i refer to jim when i'm writing the function? let's say whatever returns is based on their weight. so if billy is bigger, something will happen and something else will happen if jim is bigger. i just don't know how to use jim in the function

#include <iostream>
using namespace std;

  class dog
 {
public:
     int age;
     int weight;
     int height;

     int punch(int);
  };

   int jim::punch(x)
   {
      if (jim > x) <------------------
      {
          return //something
      }  
      else
     {
         return something
    }

 int main()
 {
 human jim;
 jim.age = 30";
 jim.weight = 175;
 jim.height = 6;
 human billy; //etc 

 jim.punch(billy);

return 0;
}
Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
JosephK
  • 59
  • 1
  • 3
  • 8
  • 1
    Is `jim` a class or an instance? Your text suggests the latter, your code the former. Can you try posting an [SSCCE](http://sscce.org/)? Still, for basics such as this, a [good book](http://stackoverflow.com/q/388242/1782465) is probably a better resource than Stack Overflow. – Angew is no longer proud of SO Jan 27 '14 at 08:52
  • human jim; //human is the class. i'm new to this sorry if this isn't legible. and i have a book but i couldn't find a suitable example. jim.age = "30"; jim.weight = 175; jim.height = 6; – JosephK Jan 27 '14 at 09:00

1 Answers1

0

You should really follow a good book (or at least a good online tutorial); your question shows confusion about very basic C++ concepts.

Nevertheless, here's an answer to your particular situation (omitting loads of details and important-but-not-for-this-particular-case concepts). human is a class. Classes can have data members and member functions. Many instances (or obejcts) of a class type can exist; each of these has its own values of data members.

Each member function can access the members (data and functions) of the instance on which it was invoked. It can do so explicitly (using the keyword this which represents a pointer to the instance), or implicitly (just naming the data member).

So here's how you might express your situation in code:

class human
{
  //place data members such as age, height, weight here

public:
  int punch(const human &target);
};

int human::punch(const human &target)
{
  std::cout << "PUNCH!";

  if (weight > target.weight) //equivalent to this->weight > target.weight
  {
    return /* something which represents this object winning */
  }
  else
  {
    return /* something which represents target object winning */
  }
  //Note: you should handle == case as well
}


int main()
{
  human jim, billy;
  jim.punch(billy);
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Thank you for your help and your timely response. Your advice is very much appreciated. – JosephK Jan 27 '14 at 09:39
  • @user3239608 If an answer solves your problem, you should consider marking it as accepted (using the green tick-mark next to it, max one accepted answer per question). That's how [SO works](http://stackoverflow.com/help/someone-answers). As a "where now" note to your code, you could look into constructors next (to initialise `jim` and `billy` properly, instead of having their attributes public and assigning them). – Angew is no longer proud of SO Jan 27 '14 at 09:47