How to use function overloading in C++

1 Answer

0 votes
#include <iostream>

class Rectangle {
	public:
		void area(int x, int y) {
			std::cout << x * y << "\n";
		}
		void area(int x) {
			std::cout << x * x << "\n";
		}
		void area(int x, double y) {
			std::cout << x * y << "\n";
		}
		void area(double x) {
			std::cout << x * x << "\n";
		}
};

int main()
{
	Rectangle rect;
	
	rect.area(5, 8);
	rect.area(12);
	rect.area(8, 4.99);
	rect.area(3.14);
	
	return 0;
}



/*
run:

40
144
39.92
9.8596

*/

 



answered Dec 10, 2020 by avibootz

Related questions

1 answer 238 views
238 views asked Dec 2, 2022 by avibootz
1 answer 203 views
203 views asked Dec 1, 2022 by avibootz
1 answer 264 views
1 answer 256 views
1 answer 336 views
1 answer 350 views
...