Pages

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. [Updated]

Friday 3 May 2013

#include <iostream.h>
class area //declare class
{
      float width, lenght, areaofrect; //attributes
      public:
      area() //construcor
      {
          width=1;
          lenght=1;
          areaofrect=0;
      }
      //functions
      void setwidth() //function to set width
      {
           again:
           cout<<"Enter width of rectangle: ";
           cin>>width;
           if(width<0 || width>20)
           {
                  cout<<"You cannot enter width greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void setlenght() //function to set lenght
      {
           again:
           cout<<"Enter lenght of rectangle: ";
           cin>>lenght;
           if(lenght<0 || lenght>20)
           {
                  cout<<"You cannot enter lenght greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void calculation() //function to calculate area of rectangle
      {
           areaofrect=width*lenght; //farmula of area of rectangle
      }
      void display() //function to display result
      {
           cout<<"Area of rectangle according to given parameters is: "<<areaofrect<<endl;
      }
    
};
main()
{
      again:  //lable
      area c; //create object
      char ask;
      //cla function of class area
      c.setwidth();
      c.setlenght();
      c.calculation();
      c.display();
      // ask from user to calculate again.
      cout<<"You want to calculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask== 'y')
      {
            goto again;
      }
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *