New to board problems with overloading

This is a discussion on New to board problems with overloading within the C++ Programming forums, part of the General Programming Boards category; Hey whats up everybody. Im new to the board and Im trying to understand this overloading thing but I dont ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    8

    New to board problems with overloading

    Hey whats up everybody. Im new to the board and Im trying to understand this overloading thing but I dont have a clue. Anyways I have to write a program that adds 2 rational numbers and I have to overload + - * / . I also have to overload the relational and equality operators. Im not sure what that is. Any help will be appreciated

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Its basically a easier better way to write statements, heres a quick cheat sheet.

    Code:
    replace variable = variable + variable;
    
    with variable += variable; //Adds variable to itself
    
    replace variable = variable * variable;
    
    with variable *= variable; // * variable by variable
    
    and so on...

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Sorry man I dont understand that. I need to do something like this. Im not sure what to put inside the function. How can I add 2 rational numbers if I only pass in one. Im not sure but I think you can only pass in one parameter when you overload something. I also have numerator and denominator in my private section.


    Code:
    Rational Rational::Operator +(Rational &rat)
    {
    
    }

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by Ride -or- Die
    Its basically a easier better way to write statements, heres a quick cheat sheet.

    That's not operator overloading.


    Anyway operator overloading is just like writing other member functions.

    I think you want something like

    Rational Rational::operator+(const Rational & rat1 , const Rational & rat2){

    //put you code to add here

    //and return the result

    }
    Last edited by beege31337; 12-13-2002 at 07:46 PM.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    class Rational
    {
        private:
              int _numerator;
              int _denominator;
        public:
              Rational() : _numerator(0),_denominator(1) {}
              Rational(int num,int denom) : _numerator(num),_denominator((denom!=0) ? denom:1) {}
              Rational(const Rational& rhs) : _denominator(rhs._denominator),_numerator(rhs._numerator){}
              virtual ~Rational() {}
              Rational operator + (const Rational& rhs)
                {
                    Rational result(*this);
                    result._denominator+=rhs._denominator;
                    result._numerator+=rhs._numerator;
                    return result;
                 }
    };
    Basic rational numbers class with construction,destruction and operator+ to add 2 rationals together defined.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    nm i read it too quickly, sorry dude.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    BTW Thats not how rational numbers are added. code is illustration purposes only.You will have to write a correct operator+.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    overloading means using the same function name (or operator symbol) and pass it two different sets of paramenters and still get it to work.

    Example:

    start with the following function:

    void add(int, int);
    //add takes two ints and does something with them

    void add(int, double);
    //now add is overloaded to take one int and one double and will do something with them. the function add is overloaded

    void add(double, int);
    //yes, it is important which sequence you use parameters as this function is different than the one above to the compiler.

    void add(double, double);
    //another overloaded version of add

    void add(double, float);
    //yet another overloaded version of add

    need I go further? The compiler can determine which version of add to call based on which set of parameters is passed. Note that polymorphism is a related, but different concept, having to do with object methods wherein methods of the same name perform differently based on the type of the object, not the parameters passed to the function/method.

    You can do the same thing (overloading) with operators as operators are just shorthand versions of functions.

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Ok this is what I have for my addition function. Please tell me whats wrong here I dont have a clue.
    Code:
    #include <iomanip.h>
    #include <stdio.h>
    //******************************************************************************
    class Rational{
    	friend ostream &operator<<( ostream &, Rational &);
    public:
    	Rational(){ numerator = 0; denominator = 1;}; //constructor function
    	Rational( int n, int d) {numerator = n; denominator = d (d==0) ? 1:d;}
    	Rational operator +(Rational);
    	Rational operator -(Rational);
    	Rational operator *(Rational);
    	Rational operator /(Rational);
    	int gcd (int n, int d);
    private:
    	int numerator;
    	int denominator;
    
    };
    
    //********************************gcd*******************************************
    //function accepts 2 integers through call by value mechanism
    //function computes the greatest common divisor and returns the GCD
    int Rational :: gcd (int n, int d)
    {
    		if (d == 0)
    			return n;
    		else
    			return (gcd(d, n % d));
    }
    
    //******************************************************************************
    ostream& operator <<(ostream& out, rational& rat)
    {
    	out << "The Rational value is: " << rat.numerator << " / " << rat.denominator << endl;
    	return out;
    }
    //*******************************add********************************************
    //function accepts 2 rational types through call by value mechanism
    //function adds two rational numbers
    Rational Rational::operator +(Rational& rat)
    {
    	Rational value;
    
    	value.numerator = rat.numerator * rat.denominator + rat.numerator * rat.denominator;
    	value.denominator = rat.denominator * rat.denominator;
    	int i = gcd(numerator, denominator);
    	if(i != 0 && i != 1){
    		numerator /= i;
    		denominator /= i;
    	}
    	return value;
    
    }

  10. #10
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You will need for many thing for example if you have a class of HugeInteger( a data type that can store very big numbers ), and you want to add two of them, without overloading you'll do:
    Code:
    hugeInt h1, h2;
    //initialize
    h1.add(h2)
    where add is a member function in class hugeInt.
    but the other way is to overload the addition operator, so you can write:
    Code:
    hugeInt h2, h2;
    //initialize
    h1 + h2;
    which makes more sense...
    But remember you can only overload existing operators for user defined datatypes, and you cannot overload all operators.
    none...

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Yeah i know thats for the main function but I wanted to know if my addition function is correct

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Wouldn't you want to return a rational reference (&Rational) instead of a Rational?

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    No. operator + should always return an object and not a reference. Remember this rule. It will serve you well.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Thanks for the help guys but I jsut cant understand it.

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    heres a link to a simple rational numbers class. Please dont use the code as it is but instead rewrite it yourself. This will just show you how to do the operator overloads and describe some helpful functions such as gcd() and samedenominator() and lcm().
    If you are still stuck then post your attempt and ill help.
    http://www.gpc.peachnet.edu/~jbenson...ary/Rational.h
    http://www.gpc.peachnet.edu/~jbenson...y/Rational.cpp
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# vs ASM board
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-28-2009, 05:18 AM
  2. Network Programming Board
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2003, 01:04 PM
  3. rotating the board
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2003, 10:09 AM
  4. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21