Chapter - 5: Classes in C++

Modify the class rectangle discussed in the text of this chapter such that a statement


D
Sections
1
Exercises

rectangle r1 = 3;
assigns a value 3 to len as well as br.


Ans:

#include<iostream>

using namespace std;

class rectangle
{
private:
	int len, br;
public:

	rectangle(){} //Default constructor
	rectangle(int a) //Assigning 3 to both the member variables
	{
		len = br = a;
	}

	void getdata()
	{
		cout << endl << "Enter the length and breadth";
		cin >> len >> br;
	}

	void setdata(int l, int b)
	{
		len = l;
		br = b;
	}

	void displaydata()
	{
		cout << endl << "length = " << len;
		cout << endl << "breadth = " << br << endl;
	}

	void area_peri()
	{
		int a, p;
		a = len*br;
		p = 2 * (len + br);
		cout << endl << "area = " << a;
		cout << endl << "perimeter = " << p << endl;
	}
};

int main()
{
	rectangle r1 = 3;
	r1.displaydata();
	return 0;
}

© 2021 Garbage Valuegarbage value logo