Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program demonstrates the use of copy constructor. (C++)


This program demonstrates the use of copy constructor. (C++)
 
This is again a very important interview question. Understand the concept very well.
 
#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;
}
 
rect operator =(rect &r)
{
cout< < endl< < "Assignment operator invoked";
> area=r.area;
len=r.len;
width=r.width;
return rect(len,width);
}
 
rect(rect &r)
{
cout< < endl< < "Copy constructor invoked";
> len=r.len;
width=r.width;
area=r.len*r.width;
}
void display()
{
cout< < 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;
 
rect r3=r1;
r1.display();
r2.display();
r3.display();
}

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP