0% found this document useful (0 votes)
17 views3 pages

Assignment 3

The document discusses function overloading in C++ using examples of calculating area of different shapes. It demonstrates how the same function name can be used for different parameters by defining multiple functions with the same name but different parameters. It also covers static functions and friend functions.

Uploaded by

Monu Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Assignment 3

The document discusses function overloading in C++ using examples of calculating area of different shapes. It demonstrates how the same function name can be used for different parameters by defining multiple functions with the same name but different parameters. It also covers static functions and friend functions.

Uploaded by

Monu Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Function Overloading
#include <iostream>
using namespace std;
double area(int Radius) {return 3.14 * Radius * Radius;}
double area(double length, double breadth) {return length * breadth;}
double area(int base, int height) {return 0.5 * base * height;}
double area(double side) {return side * side;}
double area(int a, int b, int c) {return 0.5 * (a + b) * c;}
int main() {
int c;
cout << "Enter 1.Circle\n2.Rectangle\n3.Triangle\n4.Square\n5.Trapezoid\n";
cin >> c;
switch (c) {
case 1:
int a;
cout << "Enter the radius of the circle: ";
cin >> a;
cout << "The area of the circle is " << area(a);
break;
case 2:
double length, breadth;
cout << "Enter the length and breadth of the rectangle: ";
cin >> length >> breadth;
cout << "The area of the rectangle is " << area(length, breadth);
break;
case 3:
int base, height;
cout << "Enter the base and height of the triangle: ";
cin >> base >> height;
cout << "The area of the triangle is " << area(base, height);
break;
case 4:
double side;
cout << "Enter the side of the square: ";
cin >> side;
cout << "The area of the square is " << area(side);
break;
case 5:
int al, b, c;
cout << "Enter the first two sides and height of the trapezoid: ";
cin >> a >> b >> c;
cout << "The area of the trapezoid is " << area(a, b, c);
break;
default:
cout << "Invalid choice";
}while(c != 6);
}
2.)Static Function

#include <iostream>
using namespace std;
class A{
int x; static int y;
public:
void setdata(int i){
x=i;y++;
}
static void showdata(){cout<<y;}
void showdata1(){cout<<x;}
};
int A::y;
int main(){
A a;
a.setdata(5);
a.showdata();
a.showdata1();
return 0;
}
3.) Friend function

#include <iostream>
using namespace std;
class Complex{
int a,b;
friend Complex sumComplex(Complex o1, Complex o2);
public:
void setNumber(int n1, int n2){a=n1;b=n2;}
void printNumber(){cout<<"Your number is "<<a<<" + "<<b<<"i"<<endl;}};
Complex sumComplex(Complex o1, Complex o2){
Complex o3;
o3.setNumber((o1.a+o2.a), (o1.b+o2.b) );
return o3;};
int main(){
Complex c1,c2,sum;
c1.setNumber(1,4);c2.setNumber(4, 3);
sum=sumComplex(c1,c2);
sum.printNumber();
return 0;}

You might also like