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 243 views
243 views asked Dec 2, 2022 by avibootz
1 answer 209 views
209 views asked Dec 1, 2022 by avibootz
1 answer 270 views
1 answer 266 views
1 answer 340 views
1 answer 358 views
...