Rectangle class using C++

Implement a rectangle class using C++. Rectangle class will provide rectangle objects. It will calculate area and perimeter. It should have

  • constructors
  • mutators (set methods)
  • accessors (get methods)
  • facilitators
  • inspector
  • destructor
using namespace std;
class Rectangle{
		int length;
		int breadth;
		Rectangle(int l=0, int b=0);
		Rectangle(const Rectangle& r);
		void setLength(int l);
		void setBreadth(int b);
		int getLength();
		int getBreadth();
		int area();
		int perimeter();
		bool isSquare();
Rectangle::Rectangle(int l, int b){
Rectangle::Rectangle(const Rectangle& r){
	length = r.length;
	breadth = r.breadth;
//set methods - mutators
void Rectangle::setLength(int l){
		cout<<"Lenght is negative. Enter again : ";
	length = l;
void Rectangle::setBreadth(int b){
		cout<<"Lenght is negative. Enter again : ";
	breadth = b;
//get methods - accessors
int Rectangle::getLength(){
	return length;
int Rectangle::getBreadth(){
	return breadth;
int Rectangle::area(){
	return length*breadth;
int Rectangle::perimeter(){
	int perimeter = 2*(length+breadth);
	return perimeter;
bool Rectangle::isSquare(){
	if(length==breadth) return true;
	else return false;
	cout<<"Destructor called"<<endl;
int main(){
	Rectangle r1;
	cout<<"Lenght of rectangle is "<<r1.getLength()<<endl;
	cout<<"Breadth of rectangle is "<<r1.getBreadth()<<endl;
	cout<<"Area of rectangle is "<<r1.area()<<endl;
	cout<<"Perimeter of rectangle is "<<r1.perimeter()<<endl;
	Rectangle r2(r1);
	cout<<"Lenght of rectangle r2 is "<<r2.getLength()<<endl;
	cout<<"Breadth of rectangle r2 is "<<r2.getBreadth()<<endl;
	cout<<"Area of rectangle r2 is "<<r2.area()<<endl;
	cout<<"Perimeter of rectangle r2 is "<<r2.perimeter()<<endl;
	return 0;
output of the rectangle class program
output of the rectangle class program

One Reply to “Rectangle class using C++”

Leave a Reply

Your email address will not be published. Required fields are marked *