Constructor Overloading

C++ Programming

C++ Programming Examples

/* Area Calculation */
#include<iostream>
using namespace std;

class area {
	public:

	/* First constructor with one parameter */
	area(int r) {

		/* Cast result into float in C++ notation */
		cout << "Area of Circle: " << float(3.141*(r*r)) << endl; ;
	}

	/* Second constructor with two parameter */
	area(int l , int w) {
		cout << "Area of Rectangle: " << l*w << endl;
	}

	/* Third constructor with three parameter */
	area(int a, int b, int h) {

		/* Cast result into float in C notaion */
		cout << "Area of Trapezium: " << (float) ((a+b)*h)/2 << endl;
	}

	/* Destructor for class 'area' */
	~area() {
		cout << "Destroyed object of the class!" << endl;
	}

};

int main() {

	/* Constructor called implicitly */
	area circle(5);
	area rect(10, 15);
	area trapz(5,10,15);
	return 0;
}


          /****** Output ******/
          

Area of Circle: 78.525


Area of Rectangle: 150


Area of Trapezium: 112.5


Destroyed object of the class


Destroyed object of the class


Destroyed object of the class

Learn more about Constructor Overloading in C++