Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-01-2009
Member
 
Join Date: Oct 2008
Posts: 20
Overloading Assignment Operator and Copy constructor in C#

The operator = () function in our program returns a value by creating a temporary rect object and initialising it using the two arguments constructor. The value returned is a copy of the object of which the overloaded = operator is a member. Cop constructor takes one argument,an object of type rect,passed by reference. < /FONT >



Code:
#include< iostream.h>
#include< conio.h>
class rect
{
int area;
float len,width;
public:
rect()
{
}
rect(float ll,float wd)
{
len=ll;
width=wd;
area=ll*wd;
}
//Overloading the = operator
rect operator =(rect &r)
{
cout<<endl<<"Assignment operator invoked";
> area=r.area;
len=r.len;
width=r.width;
return rect(len,width);
}
//copy constructor
rect(rect &r)
{
cout<<endl<<"Copy constructor invoked";
> len=r.len;
width=r.width;
area=r.len*r.width;
}
void display()
{
cout<< endl<<endl<<"Area= "<< area;
>cout<< endl<< endl<<"Length= "<< len;
> cout<< endl<<endl<<"Width= "<< width;
}
};
void main()
{
clrscr();
rect r1(4.5,10);
rect r2;
r2=r1;
//Copy constructor invoked
rect r3=r1;
r1.display();
r2.display();
r3.display();
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Overloading Assignment Operator and Copy constructor in C#"
Thread Thread Starter Forum Replies Last Post
How to compare various overloading Operator Plokstar Software Development 4 17-01-2011 10:07 PM
What is constructor overloading in c++ Mast Maula Software Development 4 08-01-2011 10:34 AM
Operator overloading in C sharp Bottlenecked Software Development 5 23-01-2010 09:52 AM
Difference between copy constructor and overloaded assignment operator Solitario Software Development 3 24-12-2009 01:28 PM
Error in Operator Overloading DutchDude Software Development 2 13-05-2009 11:54 PM


All times are GMT +5.5. The time now is 02:16 PM.