Methods overloading

In method overloading we can make the methods with the same name but with different parameters. When we call a function then C++ compiler matches the name and then number of parameters and type of parameters with the function definition.  

e.g:      class  rectangle

{

int  length;

int  breadth;

getdata()

{

length = 5;

breadth = 8;

}

getdata(int l, int b)

{

length = l;

breadth = b;

}

}

 

Program:

/*PROGRAM TO IMPLEMENT FUNCTION OVERLOADING*/

#include<iostream.h>

#include<conio.h>

class rectangle

{

int length,breadth;

public:

rec()

{

length=breadth=0;

cout<<“**function with zero argument called**”<<endl<<endl;

}

rec(int a)

{

length=breadth=a;

cout<<“**function with one argument called**”<<endl<<endl;

}

rec(int l,int b)

{

length=l;

breadth=b;

cout<<“**function with two argument called**”<<endl<<endl;

}

int area()

{

return(length*breadth);

}

};

void main()

{

clrscr();

rectangle r1;

r1.rec();

cout<<“\tArea of rectangle zero arguments is:”<<r1.area()<<endl<<endl;

rectangle r2;

r2.rec(5);

cout<<“\tArea of rectangle with one argument is:”<<r2.area()<<endl<<endl;

rectangle r3;

r3.rec(11,12);

cout<<“\tArea of rectangle with two arguments is:”<<r3.area()<<endl<<endl;

getch();

}

Leave a Reply

Your email address will not be published. Required fields are marked *