what is operator overloading?
operator overloading like a function overloading ,a Programmer can overload the operator .A programmer can create his or her own operator in a class using by overloading the built in operator to perform some specific computation when the operator is used on the object of that class.
some operator we can not overload like scope resolution operator (::),member selection(.) and pointer member selection (.*).
#include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{
int img,real;
public:
Complex(){img=0;real=0;}
Complex(int r,int i){img=i;real=r;}
Complex operator+(Complex );
void show(){cout<<real<<"+ i"<<img<<endl;}
};
Complex Complex::operator+(Complex com)
{
Complex value;
value.real=real+com.real;
value.img =img+com.img;
return value;
}
int main()
{
Complex copx(10,20);
Complex comx(30,40);
copx=copx+comx;
copx.show();
return 0;
}
i use == operator overloding in my code to compare the two mapimessage should not be equal
BOOL CMAPIMessage::operator==(CMAPIMessage& message)
{
if(!m_entryID.cb || !message.m_entryID.cb || m_entryID.cb!=message.m_entryID.cb) return FALSE;
if(memcmp(m_entryID.lpb,message.m_entryID.lpb, m_entryID.cb)) return FALSE;
return (!m_strSubject.Compare(message.m_strSubject));
}
operator overloading like a function overloading ,a Programmer can overload the operator .A programmer can create his or her own operator in a class using by overloading the built in operator to perform some specific computation when the operator is used on the object of that class.
some operator we can not overload like scope resolution operator (::),member selection(.) and pointer member selection (.*).
#include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{
int img,real;
public:
Complex(){img=0;real=0;}
Complex(int r,int i){img=i;real=r;}
Complex operator+(Complex );
void show(){cout<<real<<"+ i"<<img<<endl;}
};
Complex Complex::operator+(Complex com)
{
Complex value;
value.real=real+com.real;
value.img =img+com.img;
return value;
}
int main()
{
Complex copx(10,20);
Complex comx(30,40);
copx=copx+comx;
copx.show();
return 0;
}
i use == operator overloding in my code to compare the two mapimessage should not be equal
BOOL CMAPIMessage::operator==(CMAPIMessage& message)
{
if(!m_entryID.cb || !message.m_entryID.cb || m_entryID.cb!=message.m_entryID.cb) return FALSE;
if(memcmp(m_entryID.lpb,message.m_entryID.lpb, m_entryID.cb)) return FALSE;
return (!m_strSubject.Compare(message.m_strSubject));
}
Comments
Post a Comment