注册 登录
编程论坛 VC.NET论坛

[原创]非托管类中的运算符重载

zhangzujin 发布于 2006-01-11 21:48, 1018 次点击

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

using namespace std;

class Complex
{
public:
int real;
int imaginary;

Complex( )
{
real=0;
imaginary=0;
}

Complex(int a,int b)
{
real=a;
imaginary=b;
}

Complex operator+(Complex A)
{
real=real+A.real;
imaginary=imaginary+A.imaginary;

return Complex(real,imaginary);
}

void ShowComplex( )
{
cout<<"the real part is:"<<real<<endl;
cout<<"the imaginary part is:"<<imaginary<<endl;
}
};

int _tmain()
{
Complex A(2,3),B(4,5),C;

C=A+B;
C.ShowComplex( );

return 0;
}

1 回复
#2
冰镇柠檬汁儿2006-01-13 08:28

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

using namespace std;

public __gc class Complex
{
private:
int real;
int imaginary;

public:
Complex()
{}

Complex(int a,int b)
{
real=a;
imaginary=b;
}

Complex operator+(Complex A)
{
real=real+A.real;
imaginary=imaginary+A.imaginary;

return Complex(real,imaginary);
}

void ShowComplex( )
{
Concle::Writeline(S"the real part is:", real);
Concle::Writeline(S"the imaginary part is:", imaginary);
}
};

int _tmain()
{
Complex A(2,3),B(4,5),C;

C=A+B;
C.ShowComplex( );

return 0;
}
我觉得楼主的程序这么改一下会好一点

1