Answer to Question #297291 in C++ for Rajen

Question #297291

Develop a C++ program to perform constructor overloading upon default, parameterized and copy constructors in a class named "Rectangle" and include member function for calculating area and perimeter of a rectangle.

1
Expert's answer
2022-02-13T13:00:38-0500




#include<iostream>




using namespace std;




class Rectangle
{
private:
	float width;
	float height;
public:
	Rectangle(){
		this->width=0;
		this->height=0;
	}
	Rectangle(float width,float height){
		this->width=width;
		this->height=height;
	}


	// Copy constructor
	Rectangle(const Rectangle &rectangle) {
		this->width=rectangle.width;
		this->height=rectangle.height;
	}


	float area()
	{
		return width*height;
	}
	float perimeter()
	{
		return 2*(width+height);
	}
};


int main(){


	Rectangle rectangle1(4,5);
	Rectangle rectangle2(rectangle1);


	cout<<"Area of the rectangle 1: "<<rectangle1.area()<<"\n";
	cout<<"Area of the rectangle 1: "<<rectangle1.perimeter()<<"\n";


	cout<<"\nArea of the rectangle 2: "<<rectangle2.area()<<"\n";
	cout<<"Area of the rectangle 2: "<<rectangle2.perimeter()<<"\n";


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog