text: :
  Most operators can actually be overloaded either way, however there are a few exception cases:)!
  Cents Cents::operator-();
  Cents operator-(const Cents *this).
  friend operator+(int, cCents). We’ll be covering most of these in upcoming lessons.
  Thanks Alex, this is starting to make cents.
  Hi
  I wonder which is the right way of expressing in running text,the implicit pointer argument that gets passed to member functions.
  *this or this
  I prefer the latter.
  I don’t understand what you are asking.
  In operator overloading,using friend function with unary operator only that members are accessed object referenceor ordinary object with data members.in which one is possible?any possible for accessing members using friend function with unary operator?
  should be
  right? because u have not defined the member functions to be constants?
  br,
  a typo notice :
  “The negative operator is a unary operator”
  should be
  “an unary”
  Hi Alex,
  Can you please give me a main function to check out the operator+(), using member function.
  In
  However, when dealing with operands that modify the class itself (eg. operators =, +=, -=, ++, –, etc…)
  it looks as though the last operator is a single minus sign. Shouldn’t that be two? as in --
  hmm, looks as though I may have fallen into the same trap. -- -
  i want to know if i can have an overloaded unary operator. (don’t know if that even makes sense.)
  i don’t see how it is possible. The problem i was thinking about was-
  suppose obj1 and obj2 are two objects of the same class.
  i want to do:
  -M1; // the operator -- function returns no value, just changes the values of the data members of M1
  M2=-M1 // the operator -- function returns an object of the class.
  i was thinking of using both member function and friend function together would allow me to do this, haven’t tried it yet though.
  Thanks,
  madhukar
  also, when operator -- is called in
  M2=-M1;
  data members of M1 remain unchanged.
  PS: im not trolling, was really wondering if its possible :S
  PPS: i do realize i can achieve what i want using a 3rd object and such. My question is purely academic.
  Hi Alex, about your overall point of when to use friend function or member function, I guess you were saying that “when we can use member function, use member function, other than that go by friend function” Am I right?
  when dealing with operands that modify the class itself should probably read as
  when dealing with operators that modify the operands themselves
  The main function for operator overloaded using the member function method;
  Cents c(6);
  c=c+2;
  cout<<c.GetCents();
  Output: 8
  My previous reply for bishwadeepsg not Alex
  I tried overloading binary addition (+) operator using a member function as shown below.
  ------------------------------------------------------------------------------
  using namespace std;
  class Cents
  {
  private:
  int m_nCents;
  public:
  Cents(int nCents = 0) { m_nCents = nCents; }
  // Overload for Cents + Cents
  Cents operator+(const Cents &cTemp);
  int GetCents() { return m_nCents; }
  };
  // note: this function is a member function!
  Cents Cents::operator+(const Cents &cTemp)
  {
  return Cents(m_nCents + cTemp.m_nCents);
  }
  int main()
  {
  Cents cAdd;
  Cents c1(4);
  Cents c2(6);
  cAdd = c1 + c2;
  cout << "I have " << cAdd.GetCents() << " cents." << endl;
  return 0;
  }
  The code is compiling without errors and giving me the desired output. As per my understanding, when I do c1+c2 , the call gets converted into Cents operator+(const Cents *this, const Cents &cTemp). How could “this pointer” which points to object c1 can access private member m_nCents of object c2(cTemp) even if its not a friend function. This is a bit confusing for me.
  it is a member function,
  this is because of the line…
  Cents Cents::operator+(const Cents &cTemp)
  hi, Priyali. Plz Help me,write a programe how to add two object & 1 object + 1int with in single programe.
  Sorting refers to ordering data in an increasing or decreasing fashion according to some linear
  relationship among the data items. Write a class Sorting() that has one member variable of
  integer array sArray of size 5. The class will have 2 constructor functions; one default
  and one parametrized. The default constructor initializes the sArray members with all zeros
  whereas the parametrized constructor initializes sArray members with the members of array
  ssArray passed to this constructor function while calling it from main program. The Sorting
  class will have another member function setArrayValue which will input (unsorted) values
  from the user and assign them to sArray when the Sorting object will be created with the
  default constructor. The Sorting class will also have a member function Sort which when
  called will sort the array sArray using any sorting algorithm and display the sorted data.
  Write a main() program that creates an object S1 of class Sorting using default constructor,
  assigns value to sArray, sorts them and displays the result. Create another object S2 of the same
  class using the parametrized constructor and pass to it the (unsorted) array ssArray. The values
  of ssArray can be either obtained from the user or assigned to it while declaring this array. Sort
  the array ssArray and display the sorted data.
  ANY ONE can HELP ???????????? TO WRITE A PROGRAME
  Cents Cents::operator-(); converts to Cents operator-(const Cents *this);
  On your Chapter 8.7 The hidden “this” pointer
  void SetID(int nID) {} converts to void SetID(Simple* const this, int nID) {}
  Based on Chapter 9.6, “this” is a pointer to a constant class.
  Based on Chapter 8.7, “this” is a constant pointer to a class.
  Please help. Thanks!
  Hello,
  In 9.6 for overloading the operator+, it says that the friend function’s left most operand has to be of type cents for it to be overloaded via member function. What if someone were to explicitly declare the *this pointer as the second operand in a member function and an int as the first operand. Would this work?
  the member function would look like…
  Cents operator+(int nCents, const cents *this )
  In section 8.7 you said
  “void SetID(int nID) { m_nID = nID; }” becommes “void SetID(Simple* const this, int nID) { this->m_nID = nID; }”
  But you mentioned here is "the compiler internally converts this to Cents operator-(const Cents *this)"
  In my point of view, correct one should be section 8.7.
  Hi Alex,
  For this statement "when the operator does not modify it’s operands, it’s best to implement the overloaded operator as a friend function of the class.". I don’t quite get this since the friend function can also modify the objects.
  ok… I got the answer when I finish reading…
  "using friend function to modify object members violates encapsulation"
  Regarding the following statement:
  “However, when dealing with operands that modify the class itself (eg. operators =, +=, -=, ++, --, etc…) the member function method is typically used because C++ programmers are used to writing member functions (such as access functions) to modify private member variables.”
  Would it be correct to say that C++ programmers mainly use friend functions to set and modify private member variables, and member functions to get and compare private member variables?
  Hi Alex,
  You have mentioned about ‘this’ pointer in this topic that:
  "…Remember that when C++ sees the function prototype
  the compiler internally converts this to
  which you will note is almost identical to our friend version
  This means that, you are saying ‘this’ pointer is equivalent to ‘const Cents *this’.
  But shouldn’t it be ‘Cent* const this’ ???
  Name (required)
  Website
  Please enter an answer in digits:
  Current ye@r *
  Leave this field empty