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 151 views
151 views asked Dec 2, 2022 by avibootz
1 answer 132 views
132 views asked Dec 1, 2022 by avibootz
1 answer 181 views
1 answer 173 views
1 answer 232 views
1 answer 253 views
...