(高分)C++.net中如何重载运算符

WeiWY 2006-05-09 02:54:39
如定义一个复数,使用托管的方式对+,-,*,/进行重载,请问该如何做?
原来的方式已经实现,如下:
#include "stdafx.h"
using namespace std;

class complex
{
public:
complex()
{
real=0;
image=0;
}
complex(double r, double i)
{
real = r;
image = i;
}
complex complex::operator-();
complex operator +(const complex &c);
complex operator -(const complex &c);
complex operator *(const complex &c);
complex operator /(const complex &c);
private:
double real, image;
};

complex complex::operator-()
{
return complex(-real,-image);
}
complex complex::operator +(const complex &c)
{
return complex(real + c.real, image + c.image);
}

complex complex::operator -(const complex &c)
{
return complex(real - c.real, image - c.image);
}

complex complex::operator *(const complex &c)
{
return complex(real * c.real - image * c.image, real * c.image + image * c.real);
}

complex complex::operator /(const complex &c)
{
return complex((real * c.real + image + c.image) / (c.real * c.real + c.image * c.image),
(image * c.real - real * c.image) / (c.real * c.real + c.image * c.image));
}


int _tmain(int argc, _TCHAR* argv[])
{
complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
.........
}
...全文
206 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
njbfy 2006-05-13
  • 打赏
  • 举报
回复
还有一部分:

// ComplexNumber.cpp
// Method definitions for class ComplexNumber.

#include "stdafx.h"
#include "ComplexNumber.h"

// default constructor
ComplexNumber::ComplexNumber() {}

// constructor
ComplexNumber::ComplexNumber( int a, int b )
{
Real = a;
Imaginary = b;
}

// return string representation of ComplexNumber
String *ComplexNumber::ToString()
{
return String::Concat( S"( ", real.ToString(),
( imaginary < 0 ? S" - " : S" + " ),
( imaginary < 0 ? ( imaginary * -1 ).ToString() :
imaginary.ToString() ), S"i )" );
}

// overload the addition operator
ComplexNumber ComplexNumber::op_Addition( ComplexNumber x,
ComplexNumber y )
{
return ComplexNumber( x.Real + y.Real,
x.Imaginary + y.Imaginary );
}

// overload the subtraction operator
ComplexNumber ComplexNumber::op_Subtraction( ComplexNumber x,
ComplexNumber y )
{
return ComplexNumber( x.Real - y.Real,
x.Imaginary - y.Imaginary );
}

// overload the multiplication operator
ComplexNumber ComplexNumber::op_Multiply( ComplexNumber x,
ComplexNumber y )
{
return ComplexNumber(
x.Real * y.Real - x.Imaginary * y.Imaginary,
x.Real * y.Imaginary + y.Real * x.Imaginary );
}
njbfy 2006-05-13
  • 打赏
  • 举报
回复
有个例子,实现复数+、-、*, 分别用了op_Addition、op_Subtraction、op_Multiply

// ComplexNumber.h
// Class that overloads operators for adding, subtracting
// and multiplying complex numbers.

#pragma once

#using <mscorlib.dll>

using namespace System;

public __value class ComplexNumber
{
public:
ComplexNumber(); // default constructor
ComplexNumber( int, int ); // constructor
String *ToString();

// property Real
__property int get_Real()
{
return real;
}

__property void set_Real( int value )
{
real = value;
}

// property Imaginary
__property int get_Imaginary()
{
return imaginary;
}

__property void set_Imaginary ( int value )
{
imaginary = value;
}

// overload the addition operator
static ComplexNumber op_Addition( ComplexNumber,
ComplexNumber );

// overload the subtraction operator
static ComplexNumber op_Subtraction( ComplexNumber,
ComplexNumber );

// overload the multiplication operator
static ComplexNumber op_Multiply( ComplexNumber,
ComplexNumber );

private:
int real;
int imaginary;
}; // end class ComplexNumber


// ComplexNumberTest.cpp
// Example that uses operator overloading.

#include "stdafx.h"
#include "ComplexNumber.h"

#using <mscorlib.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::Windows::Forms;

int _tmain()
{
// create two ComplexNumbers
ComplexNumber x = ComplexNumber( 1, 2 );
String *output = String::Concat(
S"First Complex Number is: ", x.ToString() );

ComplexNumber y = ComplexNumber( 5, 9 );
output = String::Concat( output,
S"\nSecond Complex Number is: ", y.ToString() );

// perform addition
output = String::Concat( output, S"\n\n",
x.ToString(), S" + ", y.ToString(), S" = ", ( x + y ) );

// perform subtraction
output = String::Concat( output, S"\n",
x.ToString(), S" - ", y.ToString(), S" = ", ( x - y ) );

// perform multiplication
output = String::Concat( output, S"\n",
x.ToString(), S" * ", y.ToString(), S" = ", ( x * y ) );

MessageBox::Show( output, S"Operator Overloading" );

return 0;
} // end _tmain
真相重于对错 2006-05-13
  • 打赏
  • 举报
回复
__nogc class complex//加上__nogc
{
//其余不是你已经写了?

}
WeiWY 2006-05-13
  • 打赏
  • 举报
回复
那非托管类如何实现,只需要给一个运算符重载的例子,谢谢
WeiWY 2006-05-13
  • 打赏
  • 举报
回复
谢了!
真相重于对错 2006-05-11
  • 打赏
  • 举报
回复
托管类肯定不行,要用非托管类。
WeiWY 2006-05-11
  • 打赏
  • 举报
回复
是改成托管类,楼上帮忙,我看了帮助,老是调试通不过。
真相重于对错 2006-05-09
  • 打赏
  • 举报
回复
笔误
__gc ==> __nogc
真相重于对错 2006-05-09
  • 打赏
  • 举报
回复
如果不是非要改成托管类直接
__gc class complex
{
......

};
omen_outam 2006-05-09
  • 打赏
  • 举报
回复
我以前作业有过这道题,很抱歉当时用英文回答的,粘贴如下:

Operator overloading makes it possible for some special classes to implement some particular functions, i.e. Operator overloading provides operators multiple functions, which enhances the extension ability of C++.

Operator overloading allows you to provide the user a visual interface, which is an alternative syntax of function-calling turning the user's programing language from machine-oriented to problem-oriented.

Here are some codes from the internet, which shows how the "=" operator is overloaded.

#include <iostream>
using namespace std;

class test
{
public:
test(char *name,char *url)
{
test::name = new char[strlen(name)+1];
test::url = new char[strlen(url)+1];
if(name)
{
strcpy(test::name,name);
}
if(url)
{
strcpy(test::url,url);
}
}
test(test &temp)
{
test::name=new char[strlen(temp.name)+1];
test::url=new char[strlen(temp.url)+1];
if(name)
{
strcpy(test::name,temp.name);
}
if(url)
{
strcpy(test::url,temp.url);
}
}
~test()
{
delete[] name;
delete[] url;
}
test& operator =(test &temp)//Operator Overload
{
delete[] this->name;
delete[] this->url;
this->name = new char[strlen(temp.name)+1];
this->url = new char[strlen(temp.url)+1];
if(this->name)
{
strcpy(this->name,temp.name);
}
if(this->url)
{
strcpy(this->url,temp.url);
}
return *this;
}
public:
char *name;
char *url;
};
int main()
{
test a("lab","www.lab.com");
test b = a;//Object b doesn't exist, call the constructor.
cout<<b.name<<endl<<b.url<<endl;
test c("AOL","www.aol.com");
b = c;//Object b do exist, call the operator overloading function.
cout<<b.name<<endl<<b.url<<endl;
system("pause");
}

"test& operator=(test &temp)" in the above codes is the definition of the operator overloading. The internal pointers which needs to be deleted are where we do the deep copy. As b has been constructed, the pointers of "name" and "url" have been evaluated, thus, the stack has to be cleaned before copying the new values.

If the object does not exist, the process of evaluation is just the constructor-calling process, if the object do exist, that would be the process of operator overloading function.

希望对你有帮助.
zhoujijunnt 2006-05-09
  • 打赏
  • 举报
回复
???
顶。

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧