Results 1 to 5 of 5

Thread: plus(+) operator

  1. #1

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    plus(+) operator

    I have a class

    Code:
    class Student {
    
    	public:
       	string StudentId;
          float feespaid;
    
          //plus operator here
    };
    that i want to implement a plus operator such that it increases the fees paid variable by an amount specified in the parameter.

    I tried research but everything is just confusing, can someone show me how i can implement the operator to do what i want...thx u...

    i tried
    Code:
    operator+(feespaid a);
    .. but dont seem to do anything.
    FOX Designs

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: plus(+) operator

    I haven't written an C or C++ for a long time but I'm guessing that the rules for operators are basically the same as for C#. In C#, operators are not instance members because they are not accessed via an instance. They are declared static because they are passed an instance of the class. In C#, your class would look like this:
    csharp Code:
    1. class Student
    2. {
    3.     public string StudentId { get; set; }
    4.     public float FeesPaid { get; set; }
    5.  
    6.     public static Student operator +(Student student, float feesPaid)
    7.     {
    8.         student.FeesPaid += feesPaid;
    9.  
    10.         return student;
    11.     }
    12.  
    13.     public static Student operator +(float feesPaid, Student student)
    14.     {
    15.         student.FeesPaid += feesPaid;
    16.  
    17.         return student;
    18.     }
    19. }
    Note that the operator is defined twice for the two different orders of the arguments. You only need to do that if you want to be able to use the operator with arguments in both orders rather than just the object before the value.

    I'm not sure that it will be 100% the same in C/C++ but I expect that it will be quite similar.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: plus(+) operator

    OK, just had a bit of a look at MSDN and apparently the rules are a bit different for C++. It looks like operators can be instance members. Again, I don't know if the C++/CLI rules are different standard C++ but it looks like you could do something similar to what you did originally, but you still have to actually provide an implementation. Think of an operator as a special method. It's up to you to implement the logic of a method and operators are no different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: plus(+) operator

    Operator overloading should follow the following syntax:

    Code:
    	 MyClass operator+(const SomeOtherClass &value)
    	 {
    		 MyClass result;
                     // calculate new values for result based on the addition of this instance and 'value'
                     return result;
    	 }
    It sounds to me like you are wanting to modify the instance itself inside the + operator overload. I would strongly advice against that. It would mean that this addition:
    Code:
    Student myStudent;
    myStudent.feespaid = 0;
    myStudent + 5.0;
    Would actually alter the "myStudent" instance. An addition between two operands should not modify the operands, that is, they should stay the same after the addition has taken place.
    So, if anything, what you really want to overload is the += operator:

    Code:
    	 Student& operator+=(const float value)
    	 {
    		 this->feespaid += value;
    		 return *this;
    	 }
    Altough I feel that an addition between a Student and a float is abit illogical, atleast now it doesnt violate the "rules" of addition
    Last edited by Atheist; Nov 4th, 2012 at 06:42 AM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    New Member
    Join Date
    May 2012
    Posts
    1

    Re: plus(+) operator

    You should not go throw this much deep, as your question was to know how to increase the amount.
    Add one var which is count as object and put it in a loop which will run according to your criteria.
    And then put give the maximum condition about finding the values.
    You can try this in c,c++ by using ++ operator before the var.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width