Thu Nov 14 2019

Constructor Overloading

C++ Programming1580 views

File Name: constructor-overloading.cpp

/* 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
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.