Sei sulla pagina 1di 6

Q.

No #1

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 area
{
float thickness,stretch , Area, Perimeter;
public:
area()
{
thickness=1;
stretch=1;
Area=0;
Perimeter=0;
}

void setthickness()
{
Rectangle:
cout<<"Enter thickness of rectangle: ";
cin>>thickness;
if( thickness>20)
{
cout<<"thickness is greater than 20"<<endl;
goto Rectangle;
}
else if (thickness <=0)
{
cout<<"thickness is less than 0"<<endl;
goto Rectangle;
}
}
void setstretch()
{
Rectangle:
cout<<"Enter stretch of rectangle: ";
cin>>stretch;
if(stretch>20)
{
cout<<"stretch is greater than 20."<<endl;
goto Rectangle;
}
else if(stretch<=0)
{
cout<<"stretch is less than 0."<<endl;
goto Rectangle;
}
}
void calculate()
{
Area=thickness*stretch;
Perimeter=2*thickness+2*stretch;

}
void show()
{
cout<<"stretch and thickness is less than 20 and greater than 0." <<endl;
cout<<"Area of rectangle = "<<Area<<endl;
cout<<"Perimeter of rectangle = "<<Perimeter<<endl;
}

};
int main()
{
Rectangle:
area c;
c.setstretch();
c.setthickness();
c.calculate();
c.show();
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 area
{
float thickness , stretch, Area, Perimeter,coordinates;
float P1, P2, P3, P4;
public:
int x1,x2,x3,x4,y1,y2,y3,y4;
area()
{
P1=0;
P2=0;
P3=0;
thickness=1;
stretch=1;
coordinates=0;
Area=0;
Perimeter=0;
}
void setcoordinates()
{
Rectangle:
cout<<"enter value of coordinates x1,x2,x3,x4,y1,y2,y3,y4"<<endl;
cin>>x1>>x2>>x3>>x4>>y1>>y2>>y3>>y4;
P1 = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
P2 = sqrt((x4 - x3)*(x4 - x3) + (y4 - y3)*(y4 - y3));
P3 = sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2));
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 Rectangle;
}
if(P1!=P2)
{
cout<<"Rectangle is not valid";
goto Rectangle;
}

void setthickness()
{
Rectangle:
cout<<"thickness of rectangle: ";
thickness=P1;
cout<<thickness;
cout<<endl;
if( thickness>20)
{
cout<<"thickness is greater than 20";
goto Rectangle;
}
else if (thickness <=0)
{
cout<<"thickness is equal or less than 0"<<endl;
goto Rectangle;
}
}
void setstretch()
{
Rectangle:
cout<<"stretch of rectangle: ";
stretch=P3;
cout<<stretch;
cout<<endl;

if(stretch>20)
{
cout<<"stretch is greater than 20."<<endl;
goto Rectangle;
}
else if(stretch<=0)
{
cout<<"stretch is equal or less than 0."<<endl;
goto Rectangle;
}
}
void calculate()
{
Area=thickness*stretch;
Perimeter=2*thickness+2*stretch;

}
void show()
{
cout<<"thickness and stretch is less than 20 and greater than 0." <<endl;
cout<<"Area of rectangle = "<<Area<<endl;
cout<<"Perimeter of rectangle = "<<Perimeter<<endl;
if(P1=P3)
{
cout<<"it is a Square"<<endl;
}
else
{
cout<<"it is not a square";
}
}

};
int main()
{
Rectangle:
area c;
c.setcoordinates();
c.setthickness();
c.setstretch();
c.calculate();
c.show();
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 serial
{
static int count;
public:
int number;
serial()
{
static int count=1;
number=count;
++count;
}
void show()
{
cout<<"I am an object number "<<number<<"\n";
}
};
int main()
{ serial a[5];
for(int j=0; i<5; j++)
{
a[j].show();
}
return 0;
}

Potrebbero piacerti anche