C# .NET - Rational numbers overloaded operator - error

Asked By John Thomas
08-Dec-09 04:58 PM

Hi,

I need to add 2 rational numbers by overloading the + operator. I have the rational class that has a constructor that takes in the integral portion, numerator and denominator portion.  

I am getting an error Rational does not contain a constructor that takes in 2 arguments. How can i resolve it.

The ouput should be in the form of a fraction. Can someone tell me what is the mistake that i am doing and how to fix it please.

Class Rational

{

#region Private Data Members

private int _integral;

private int _num;

private int _den;

#endregion

#region Public Properties

public int Integral

{get{return _integral;}

set{_integral = value;}}

private int Num

{get {return _num;}

set {_num = value;}}

public int Den

{get {return _den;}

set {_den = value;}}

#endregion

#region Constructor

public Rational(int integ, int num, int den)

{

this.Integral = integ;

this.Num = (den * integ)+ num;

this.Den = den;

}

#endregion

//overload opertor +

public static Rational operator +(Rational a, Rational b)

{

return new Rational(a.Num * b.Den + b.Num * a.Den, a.Den * b.Den);

}

}

MAIN PROGRAM

static void Main(string[] args)

{

Rational a = new Rational(1, 1, 2);

Rational b = new Rational(1, 1, 2);

Console.WriteLine(a + b);

Console.ReadKey();

}

Response  Response

08-Dec-09 08:56 PM

The problem is on the following line in your code:

return new Rational(a.Num * b.Den + b.Num * a.Den, a.Den * b.Den);

You are only passing 2 parameters to the constructor.  I believe this should be as follows:

return new Rational(a.Integral + b.Integral, a.Num * b.Den + b.Num * a.Den, a.Den * b.Den);

Regards,
http://www.sql-server-helper.com/sql-server-2008/sqlconnection-connection-string.aspx

Check This  Check This

08-Dec-09 11:57 PM
In that class :
Use simple Oprator Overloading method.

Ex:
a Rational Number 3 (5/7) can be written as ((7*3)+5)/7  = 26/7

Now, Operation 5 + 26/7 =    ((7*5) + 26)/7 = (35 + 26)/7

( 5 + 3.71)  = 8.71                          or      (61/7) = 8.71
Create New Account
help
sql server stop working SQL Server my sql2000sp4 on winsbs2k3 stoped working due to error access is denied (error 5). It worked well during two years until yesterday. any ideas? sql service manager starts with domain / administrator. SQL Server Discussions SQL Server 2000 (1) Stored procedure (1) Errorlog (1) Backup (1) Class (1) Lock (1) Date
Writing to SQL Server Logs from a C# SMO Program?? SQL Server SQL Server 2005 .NET 2.0 Hi, Is there an SMO class available for me to write an event into a SQL Server log? I see the documentation for the LogFile and LogFileCollection classes but I can
What's the go with sql server SQL Server I was just saying today that sql server is full of silly limitations and I seem to hit on a new one every features with more ways to manipulate things than you can poke a stick at. In sql server we don't even have a full range of date functions (a pretty basic feature
migration from SQL Server 2000 to SQL Server 2008 SQL Server I would like to migrate my database SQL Server 2000 to SQL Server 2008. My fronend application is MS Access 2002. I just wanted
New to SQL Server SQL Server Hello all I am new to SQL Server (2005), and I have to migrate a large programme a = nd = Access 2000 database to SQL Server 2005 My question is very basic. In my VB6 programme, I have functions that = Update