Hi.
I have to create this program in c++ but I have many problems when I try to create can you help me please.


Create a class rectangle. The clas has attributes length and width, each of which defaults to 1. It has members function that calculate the perimeter and the area of the Rectangle. It has set and get functions for both length and width. The set functions should verify that length and width are floating-point numbers larger than 0.0 and less than 20.0. Create a program to test the Rectangle class.

I'm appreciate your help as soon possible because these is a assigment for Tuesday.


THANKS YOU

my code.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


	class Rectangle
	{
	double Area;
	double Perimeter;

public:
	Rectangle ( double Area, double Perimeter)
	{
	  Area = A;
	  Perimeter = P;
	}
	void setArea ( double Area)
	{
		Area = A;
	}
	double getArea ()
	{
		return length * width;
	}
	void setPerimeter (double perimeter)
	{
		Perimeter = P;
	}
	double getperimeter ()
	{
		return length * width;
	}
	}

    int main()
	{
		Rectangle rectangleOne;

		rectangle = new Rectangle();

		System.out.println("\nThe values of rectangle are: ");
		System.out.println();

		System.out.println("The rectangle is: ");
		System.out.println(rectangleOne.calculateArea() );

		System.out.print("The perimeter is: ");
		System.out.println(rectangleOne.Perimeter() );

		return 0;
	}
	}
}

Instead of area and perimeter...it should be length and breadth....why are u using java code in c++...

here is my new code.
I know that I missing something but i don't know what is.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

     double length;
	 double width;
	 {
	 double Rectangle ( double length, double width)
	 {
	Length(L);
	Width(W);
	}
	void setLength ( double length)
	{
		length = L;
	}
	double getlength ()
	{
		return length;
	}
	void setwidth (double width)
	{
		width = W;
	}
	double getwidth ()
	{
		return width;
	}
	int main ( int arg, char * argv[])
	{
		cout << " The values for the rectangle are: ";
		cin >> length;
		cin >>width;
		cout << "\n";

		cout << "The rectangle is: ";
		cout << " The area is: ";
		cout << " The perimeter is: ";
		return 0;
	}

the program show me this error.

1>c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\triangle\triangle\triangle.cpp(10) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\documents and settings\hp_administrator\my documents\visual studio 2005\projects\triangle\triangle\triangle.cpp(44) : fatal error C1004: unexpected end-of-file found
1>Build log was saved at "file://c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\Triangle\Triangle\Debug\BuildLog.htm"
1>Rectangle - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Somewhere on these lines...u will need extra member functions for calculation of area and parameter.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Rectangle
{
	
double length;
double width;

public:

Rectangle(){}
Rectangle ( double L, double W):length(L),width(W){}
void setLength ( double L)
{
length = L;
}
double getlength ()
{
return length;
}
void setWidth (double W)
{
width = W;
}
double getwidth ()
{
return width;
}

};

int main ( int arg, char * argv[])
{

Rectangle obj1;
Rectangle obj2(10,20);
cout << "\n";
cout<<obj2.getlength();
cout << "\n";
return 0;
}

It looks like you still have to implement these requirements:

The clas has attributes length and width, each of which defaults to 1. It has members function that calculate the perimeter and the area of the Rectangle. It has set and get functions for both length and width. The set functions should verify that length and width are floating-point numbers larger than 0.0 and less than 20.0.

Have you made an attempt at them that you need assistance with?

Hi.
I have to create this program in c++ but I have many problems when I try to create can you help me please.


Create a class rectangle. The clas has attributes length and width, each of which defaults to 1. It has members function that calculate the perimeter and the area of the Rectangle. It has set and get functions for both length and width. The set functions should verify that length and width are floating-point numbers larger than 0.0 and less than 20.0. Create a program to test the Rectangle class.

I'm appreciate your help as soon possible because these is a assigment for Tuesday.


THANKS YOU

#include <iostream>
#include <stdio.h>
typedef enum
{
	FALSE, TRUE
} Boolean;

class Rectangle{
	double length;
	double width;

public:
	Rectangle ( double L, double W): length(L), width(W) {}
	Boolean setlength ( double L)
	{
   	if ((L < 0) || (L > 20))
      {
      	return FALSE;
      }
      else
      {
			length = L;
         return TRUE;
      }
	}
	double getlength ()
	{
		return length;
	}
	Boolean setwidth (double W)
	{
   	if ((W < 0) || (W > 20))
      {
      	return FALSE;
      }
      {
      	width = W;
         return TRUE;
      }
	}
	double getwidth ()
	{
		return width;
	}

   double perimeter (double L, double W)
   {
   	return (L + W)*2;
   }
   double area (double L, double W)
   {
   	return L * W;
   }
};

/*
Create a class rectangle. The clas has attributes length and width, each of
which defaults to 1. It has members function that calculate the perimeter and
the area of the Rectangle. It has set and get functions for both length and
width. The set functions should verify that length and width are floating-point
numbers larger than 0.0 and less than 20.0. Create a program to test the
Rectangle class.

*/
int main ()
{
	double length, width;
   Rectangle myRectangle(1, 1);
   do
   {
		cout << "\nEnter the length: ";
		cin >> length;
      if (!(myRectangle.setlength(length)))
      {
      	cout << "\nInvalid length !!!";
         length = 0;
      }
   } while(!length);

   do
   {
		cout << "\nEnter the width ";
		cin >> width;
      if (!(myRectangle.setwidth(width)))
      {
      	cout << "\nInvalid width !!!";
      	width = 0;
      }
   } while(!width);

	cout << "\n";

   cout << "length = " << myRectangle.getlength();
   cout << endl;
   cout << "width = " << myRectangle.getwidth();
   cout << endl;
   cout << "The area is: " << myRectangle.area(length, width);
	cout << endl;
   cout << "The perimeter is: " << myRectangle.perimeter(length, width);
	return 0;
}

This code should work. :rolleyes:

This article has been dead for over six months. Start a new discussion instead.