0% found this document useful (0 votes)
30 views

Example For Function Overloading

The document provides an example of function overloading in C++. It overloads the volume() function three times to calculate the volume of a cube, cylinder, and rectangular box. The main() function calls each overloaded volume() function and outputs the results. A second example demonstrates a friend function that can access private members of a class. The mean() friend function calculates the average of two private variables in the sample class.

Uploaded by

Naveen Chaubey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Example For Function Overloading

The document provides an example of function overloading in C++. It overloads the volume() function three times to calculate the volume of a cube, cylinder, and rectangular box. The main() function calls each overloaded volume() function and outputs the results. A second example demonstrates a friend function that can access private members of a class. The mean() friend function calculates the average of two private variables in the sample class.

Uploaded by

Naveen Chaubey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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; x.setvalue(); cout<<mean value=<<mean(x)<<\n; getch(); return 0; }

You might also like