Example for function overloading
//function volume() is overloaded three times #include<iostream.h> using namespace std; //Declarations int volume(int); double volume(double,int) ; int volume(int,int,int); int main() { cout<<volume(10)<<\n; cout<<volume(2.5,8)<<\n; cout<<volume(10,5,15)<<\n; return 0; } //function definitions int volume(int s) //cube { return(s*s*s); }
double volume(double r,int h) { return(3.14*r*r*h); } int volume(int l,int b,int h) { return(l*b*h); }
//cylinder
//rectangular box
Output: 1000 157.26 750
Friend function example
#include<iostream.h> #include<conio.h> using namespace std; class sample { int a; int b; public: void setvalue() { a=25; b=40; } friend float mean(sample s); };
float mean(sample s) { return float(s.a+s.b)/2.0; } int main() { sample x; [Link](); cout<<mean value=<<mean(x)<<\n; getch(); return 0; }