You are on page 1of 4

9.

11 (Rectangle Class) Create a class Rectangle with attributes length and width,
each of which defaults to 1. Provide member functions that calculate the perimeter
and the area of the rectangle. Also, provide set and get functions for the length
and width attributes. The set functions should verify that length and width are
each floating-point numbers larger than 0.0 and less than 20.0.

#include <iostream>
using namespace std;
class Rectangle
{
float w,l,a,p;
public:
void solve()
{
a=w*l;
p=2*(w+l);

}
void get_lenght()
{
Length:
cout<<"Enter lengt less than 20 and greater than 0 "<<endl;
cin>>l;
if(l>20)
{
cout<<"lenght greater than 20. Enter value again:"<<endl;
goto Length;
}
else if(l<=0)
{
cout<<"lenght smaller than 0. Enter again:"<<endl;
goto Length;
}
}
void get_width()
{
Width:
cout<<"Enter width less than 20 and greater than 0"<<endl;
cin>>w;
if( w>20)
{
cout<<"Width greater than 20. Enter value again"<<endl;
goto Width;
}
else if (w <=0)
{
cout<<"Width smaller than 0 Enter again"<<endl;
goto Width;
}
}

void display()
{
cout<<"Rectangle area = "<<a<<"m"<<endl;
cout<<"Rectangle Perimeter = "<<p<<"m"<<endl;
}

};
int main()
{
R:
Rectangle c;
c.get_width();
c.get_lenght();
c.solve();
c.display();
return 0;
}

Q.No #2
Create a more sophisticated Rectangle class than the one you created in Exercise
9.11. This class stores only the Cartesian coordinates of the four corners of the
rectangle. The constructor calls a set function that accepts four sets of
coordinates and verifies that each of these is in the first quadrant with no single
x- or y-coordinate larger than 20.0. The set function also verifies that the
supplied coordinates do, in fact, specify a rectangle. Provide member functions
that calculate the length, width, perimeter and area. The length is the larger of
the two dimensions. Include a predicate function square that determines whether the
rectangle is a square.

#include <iostream>
using namespace std;
class Rectangle
{
float w,l,a,p;
float Point_1, Point_2, Point_3,Point_4;
public:
int x1,y1,x2,y2,x3,y3,x4,y4;
void get_Coordinates()
{
Coordinates:
cout<<"enter (x1,y1) (x2,y2) (x3,y3) (x4,y4)"<<endl;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
Point_1 = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
Point_2 = sqrt((x4 - x3)*(x4 - x3) + (y4 - y3)*(y4 - y3));
Point_3 = sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2));
Point_4 = sqrt((x4 - x1)*(x4 - x1) + (y4 - y1)*(y4 - y1));
if (x1>0,x2>0,x3>0,x4>0,y1>0,y2>0,y3>0,y4>0)
{
cout<<"Rectangle in 1st Coordinate"<<endl;
}
else
{
cout<<"Rectangle is not in 1st coordinate"<<endl;
goto Coordinates;
}
if(Point_1!=Point_2)
{
cout<<"Rectangle is not valid. Enter Vvalue again: "<<endl;
goto Coordinates;
}
if(l>20)
{
cout<<"lenght greater than 20. Enter value again:"<<endl;
goto Coordinates;
}
else if(l<=0)
{
cout<<"lenght smaller than 0. Enter again:"<<endl;
goto Coordinates;
}

void solve()
{
a=w*l;
p=2*(w+l);

}
void get_lenght()
{
Length:
Point_1=l;
cout<<" length ="<<endl;
cout<<l;
}
void get_width()
{
Width:
Point_2=w;
cout<<"width = "<<endl;
cin>>w;
}

void display()
{
cout<<"Rectangle area = "<<a<<"m"<<endl;
cout<<"Rectangle Perimeter = "<<p<<"m"<<endl;
}

};
int main()
{
Calculation:
Rectangle c;
c.get_width();
c.get_lenght();
c.solve();
c.display();
return 0;
}

Q.No #3
Create a class that includes a data member that holds a serial number for each
object created from the class. That is, the first object created will be numbered
1, the second 2, and so on. To do this, youll need another data member that
records a count of how many objects have been created so far. (This member should
apply to the class as a whole; not to individual objects. What keyword specifies
this?) Then, as each object is created, its constructor can examine this count
member variable to determine the appropriate serial number for the new object. Add
a member function that permits an object to report its own serial number. Then
write a main() program that creates three objects and queries each one about its
serial number. They should respond I am object number 2, and so on.

#include<iostream>
using namespace std;
class Product
{
static int Counter;
public:
int Number;
Product()
{
static int Counter=1;
Number=Counter;
++Counter;
}
void display()
{
cout<<"I am oject number "<<Number<<endl;
}
};
int main()
{
Product Serial_Number[3];
for(int i=0; i<3; i++)
{
Serial_Number[i].display();
}
return 0;
}

You might also like