Monday, 28 January 2013

Programming using C++

Today we shall a write a program to find area and perimeter of the rectangle by using classes,constructors, functions and array of objects....The question to the program is as follows..

  create a class rectangle has the attributes length and breadth each of which default to one.It has member function that calculate the perimeter and area of the rectangle. It has set and get functions for both length and breadth.The set function should verify the length and breadth it has integer numbers are between 0 and 20.

Program to the above problem is::


#include<iostream.h>
#include<conio.h>
class rectangle
{
   int length,width;
   public:
   void perimeter();
void area();
void display();
void setdata();
};

rectangle :: rectangle()
{
   cout<<"Constructor called":
length=1;
width=1;
}

void rectangle :: setdata()
{
  cout<<"\n  Enter the length and width of the rectangle:";
cin>>length>>width;
if(-32768<length && length<20 && width>-32768 && width<32768)
{
 }
else
{
    cout<<"Enter valid values for length and width";
}

if((length>0 && length<20) && (width>0 && width<20))
{
}
else

cout<<"\nPlease enter valid length and width between 0 and 20":
cout<<"Enter length of the rectangle":
cin>>length;
cout<<"Enter width of the rectangle":
cin>>width;
}
}

void rectangle :: display()
{

cout<<"Given length and breadth of the rectangle are:";
cout<<length<<width;
}

void rectangle :: area()
{
 int area;
area=length*width;
cout<<"\n area of the given rectangle is"<<area;
}

void rectangle :: perimeter()
{
   perimeter=2*(length+width);
cout<<"Perimeter of the given rectangle is:"<<perimeter;
}

void main()
{
   clrscr();
rectangle ob;
ob.setdata();
ob.display();
ob.area();
ob.perimeter();
getch();
}


No comments:

Post a Comment