Overloaded assignment operators based on C + +

Source: Internet
Author: User
Tags strlen

To solve the problem above, we should write a special assignment operator function to deal with this kind of problem. Operator functions can be overloaded when you need to assign values to each of the two objects of the same class. This method can resolve the assignment of the class and the release of the pointer.

In the following program, the assignment function in the class assigns a different pointer from the heap with the new operator, which takes the corresponding value in the assignment object and copies it to the object that accepts the assignment.

Overloaded assignment operators in a class are formatted as follows:

void operator = (const date&) We are going to refine it later. Currently, the return type of the overloaded operator function is void. It is the general member function of the class, which in this program is red, is the member function of the date class. Its function name is always operator =, and the parameter is always a reference to the object of the same class. Parameter represents the source object, which is the provider of the assignment data. The operators of overloaded functions are used as member functions of the target object.

#include "iostream.h"

#include "string.h"

Class Date

{

int mo,da,yr;

Char *month;

Public

Date (int m=0, int d=0, int y=0);

~date ();

void operator= (const date&);

void display () const; };

Date::D ate (int m, int d, int y)

{

static char *mos[] =

{

"January", "February", "March", "April", "may", "June",

"July", "August", "September", "October", "November", "December"};

mo = m; da = D; yr = y;

if (M!= 0)

{month = new Char[strlen (mos[m-1]) +1];

strcpy (month, mos[m-1]);

}

Else month = 0;

}

Date::~date ()

{

delete [] month;

}

void Date::d isplay ()

Const

{

if (month!=0) cout "Month" "Da" "," "Yr Endl;

}

void date::operator= (const date& DT)

{

if (this!= &dt)

{

mo = Dt.mo;

da = Dt.da;

yr = Dt.yr;

delete [] month;

if (dt.month!= 0)

{

month = new Char

[Std::strlen (Dt.month) +1];

std::strcpy (month, dt.month);

}

Else month = 0;

}

}

int main ()

{

Date birthday (8,11,1979);

Birthday.display ();

Date NewDay (12,29,2003);

Newday.display ();

NewDay = Birthday;

Newday.display ();

return 0;

}

In addition to adding an overloaded operator function to the date class, this program is the same as one of the above programs. The assignment operator function first obtains the required data, and then uses the delete to return the memory occupied by the original month pointer to the heap. Then, if the source object's month pointer has already been initialized, the new operator is used to reallocate the memory to the object, and the month string of the source object is copied to the receiver.

The first statement of the overloaded Date class assignment operator function compares the address and the this pointer of the source object. This operation is guaranteed that the object does not assign itself to its own value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.