Hello everyone! I've made the mistake of not asking my teacher for help when I had the time. Now I'm stuck and lost. I didn't do this because I felt I should be able to figure this out myself since this is my major. So, I'm giving up and needing advice. I would love any help.

Write a program that uses a class named Rectangle. The class has floating point attributes length and width. It has member functions that calculate the perimeter and the area of the rectangle. It also has set and get functions for both length and width, The set functions verify that length and width are each floating point numbers large than 0.0 and 20.0. If invalid length or width are given, then length and width will be set to 1.0. A member Boolean function will determine if the rectangle is a square (A square exists if the length and the width differ by less than .0001) The class will have a destructor that displays a message indicating that an object has "gone out of scope".

The class will have 3 overloaded constructor functions. The first will have no parameters ( in this function set the length and width to 1.0 in the body of the function. The second will have one parameter (length). (in this function set the width to 1.0 in the body of the function.) The third will have two parameters (length and width). This third constructor will set length and width to 1.0 in the body of the function if the values for these members are invalid.

Error messages will indicate that an attempt has been made to create an object with invalid parameters. Test the performance of your class by performing the following tasks in your program in the given order: Declare object 1 with no parameters. Declare object 2 with valid parameters for length (7.1) and width (3.2. Declare object 3 with only a length (6.3). Declare object 4 with invalid parameters for length and width. Declare object 5 and initialize it by assigning object 2. Display the length, width, perimeter, area, of all 5 objects and indicate wether or not they are squares. Write all output data to a file.

So, hopefully my code so far isn't awful. I'm sorry if it is. I understand that area and perimeter need to be passed by value returning, but i'm unsure on how to do this. I also am confused by the destructor, and I'm sure there are other things I did wrong that I need pointed out. Here's what I have so far.

This is my header file Rectangle.h

#ifndef Rectangle_H
#define Rectangle_H

class Rectangle
{
public:
	Rectangle();
	Rectangle(float length);
	Rectangle(float length, float width);
	~Rectangle();

	void setLengthAndWidth(float, float);
	void setLength(float Length);
	void setWidth(float Width);
	void calculatePerimeter();
	void calculateArea();
	void isSquare();
	void printInfo();

	float getLength();
	float getWidth();

private: 
	float length;
	float width;
	float area;
	float perimeter;
};

#endif

This is my Member function cpp file

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"

using namespace std;

Rectangle::Rectangle()
	{length = width = 1.0;}

Rectangle::Rectangle(float length)
	{setLengthAndWidth (length, 1.0);}

Rectangle::Rectangle(float length, float width)
	{setLengthAndWidth (length, width);}

void Rectangle::setLengthAndWidth(float Len, float Wid)
{
	setLength(Len);
	setWidth(Wid);
}

void Rectangle::setLength(float length)
{
	if (length >= 0 || length <= 20.0) 
		length = length;
	else
		length = 1.0;
}

void Rectangle::setWidth(float width)
{
	if (width >= 0 || width <= 20.0) 
		width = width;
	else
		width = 1.0;
}

void Rectangle::calculatePerimeter()
{
	(length * 2) + (width * 2) = perimeter;
		return perimeter;
}

void Rectangle::calculateArea()
{
	length * width = area;
		return area;
}

float Rectangle::getLength()
{
	return length;
}

float Rectangle::getWidth()
{
	return width;
}

void Rectangle::isSquare()
{
	return fabs(length - width) < .0001;
}

void Rectangle::printInfo()
{
	cout << "the length is " << length << endl << "the width is " << width << endl;
	cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;

	if(Rectangle.isSquare)
	
		cout << "the rectangle is square" << endl;
	else 
	
		cout << "The rectangle is not a square " << endl;
	
}

Rectangle::~Rectangle()
{
	cout << "the object has gone out of scope. ";
}

And this is my main cpp file

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"

using namespace std;

int main()
{
	Rectangle objectOne;
	Rectangle objectTwo(7.1, 3.2);
	Rectangle objectThree(6.3);
	Rectangle objectFour(200,300);
	Rectangle objectFive = objectTwo;

	cout << "The first objects information is\n  ";
	objectOne.printInfo();

	cout << "The second objects information is\n  ";
	objectTwo.printInfo();

	cout << "The third objects information is\n  ";
	objectThree.printInfo();

	cout << "The fourth objects information is\n  ";
	objectFour.printInfo();

	cout << "The fifth objects information is\n  ";
	objectFive.printInfo();
}

I would be forever thankful for help. This is due tomorrow and I'm worried I'm light years behind from finishing it, but I guess it's my own fault for thinking I could do it on my own. Thanks guys!

Eagletalon commented: Decent intelligent question after really trying +2

Recommended Answers

All 16 Replies

Alright mate, first thing is first... bloody well done on getting this far by yourself, unlike many others that use this forum you actually tried and deserve recognition for that fact...

now what are you having troubles with? do you have compiling errors or are you having incorrect or not anticipated output? or do you not know where to go or what seems to be the problem here?

Thanks, I'm still disappointed in myself for not being able to finish it on my own. Makes me scared that I'm going into this as my major :\

My main issue is understanding the destructor and if I have that running correctly and how I would exactly pass perimeter and area. My teacher was talking to another student and I believe he said they would either be reference or value returning, I thought I wrote it down but the only note I have says it's value but i'm questioning if that would be right. I'm still a little lost when it comes to reference functions in OOP programming.

I'm not too sure about what all will be wrong with it right now I'm making it all cout and then when it runs i'll change it to output file. But the only problems that show up as underlined red when I try to build my program are in my member function .cpp code

void Rectangle::calculatePerimeter()
{
	(length * 2) + (width * 2) = perimeter;
		return perimeter;
}

The errors in the code above show up as are right next to the first "(" brace and it says "expression must be a modifiable value" Second one is on my " return perimeter" and it says " Return value type does not match function type"

Next one is

void Rectangle::calculateArea()
{
	length * width = area;
		return area;
}

on "length" it says "expression must be a modifiable value" and the other one is under "return area" and it says "return value type does not match function type"

Last but not least

void Rectangle::isSquare()
{
	return fabs(length - width) < .0001;
}

under "fabs" it says "return value does not match the function type"

void Rectangle::calculatePerimeter()
{
	(length * 2) + (width * 2) = perimeter;
		return perimeter;
}

The errors in the code above show up as are right next to the first "(" brace and it says "expression must be a modifiable value" Second one is on my " return perimeter" and it says " Return value type does not match function type"

alright here I can see 2 things that are troubling me... 1st place a function declared as "void" can not have a return type... so if you take that away the error should disappear...

And secondly: "(length * 2) + (width * 2) = perimeter;" should this not be -> "perimeter = (length * 2) + (width * 2);"? as you assign the value?

void Rectangle::calculateArea()
{
	length * width = area;
		return area;
}

on "length" it says "expression must be a modifiable value" and the other one is under "return area" and it says "return value type does not match function type"

Again the same 2 problems

void Rectangle::isSquare()
{
	return fabs(length - width) < .0001;
}

under "fabs" it says "return value does not match the function type"

and again with the return in the "void" function...

also when working with a return, I make it my standard practice to always return a already set variable rather than a calculation... easier to debug and find problems... but thats just me

so no real big problems just a bit of a syntax error... all in all bloody well done still :)

I just fixed my code from
length * width = area; to area = length * width; that was a derp on my part
so now here are my 5 error list errors

Error 1 error C2562: 'Rectangle::calculatePerimeter' : 'void' function returning a value h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 48

Error 2 error C2562: 'Rectangle::calculateArea' : 'void' function returning a value h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 54

Error 3 error C2562: 'Rectangle::isSquare' : 'void' function returning a value h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 69

Error 4 error C2059: syntax error : '.' h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 77

Error 5 error C2181: illegal else without matching if h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 81

Yeah as I said... a function declared "void"... (eg. void Rectangle::isSquare()) can not have a "return"... just take out those lines and the errors should cease

Sorry! :P I was looking through my code and I noticed that so I thought I'd update the status then I saw your reply. So with my other functions I would just set those as say.

float calculatePerimeter();

in my Rectangle.h file?

I'm gonna give it a go and see what I get. I appreciate you so much, you have no idea =)

It all seems to be fine now except for my if statement. I don't quite see what's wrong with my syntax :P Sorry it's an awfully shitty easy question, it's been a good 6 months since I've programmed with C++ and forgot some of the basics.

void Rectangle::printInfo()
{
	cout << "the length is " << length << endl << "the width is " << width << endl;
	cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;

	if(Rectangle.isSquare)
		cout << "the rectangle is square" << endl;
	else 
		cout << "The rectangle is not a square " << endl;
	
}

"Error 1 error C2059: syntax error : '.' h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 77"

"Error 2 error C2181: illegal else without matching if h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 79"

Which if gives you the problem?

Also on the function data types... something like "calculate perimeter" I would make a void function... but then you dont have a line in that function with "return"... you just leave it out...

In the case of "IsSquare()" I would make it a bool and use it in a way like:

if (object1.IsSquare(){

    cout << "Object 1 is a square" << endl;
}

then the return would be a true/false ofcourse

Sorry! I edited my last post with the code that was giving me an error. I figured I would put issquare in my print info function because it seemed like it would be less work to just be able to call printinfo from main. Would it be a better way to set that in main as two calls as printinfo and issquare?

Alright you have 2 errors there... I think keep it as it is (in PrintInfo()) but remove the text "Rectangle."... so it looks like:

void Rectangle::printInfo()
{
	cout << "the length is " << length << endl << "the width is " << width << endl;
	cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;
 
	if(isSquare)
		cout << "the rectangle is square" << endl;
	else 
		cout << "The rectangle is not a square " << endl;
 
}

then we check the other error if it is still there

Mkay, I changed it to your code and I this error when I build it

"Error 1 error C3867: 'Rectangle::isSquare': function call missing argument list; use '&Rectangle::isSquare' to create a pointer to member h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 77"

I'm just guessing but would I change this code to

float &Rectangle::isSquare()
{
	return fabs(length - width) < .0001;
}

Because I don't have that function set as a reference

Mkay, I changed it to your code and I this error when I build it

"Error 1 error C3867: 'Rectangle::isSquare': function call missing argument list; use '&Rectangle::isSquare' to create a pointer to member h:\c++\programming assignment one\programming assignment one\programming assignment 1 member functions.cpp 77"

I'm just guessing but would I change this code to

float &Rectangle::isSquare()
{
	return fabs(length - width) < .0001;
}

Because I don't have that function set as a reference
Also just realize I need to change it from float to bool

My apologies I also made a mistake... should be if(issquare())... typo on my side

Keep it as Rectangle::isSquare()... just make it a bool and give it a try

No more errors! You've been such an amazing help thanks so much! Setting you to my solver of this, but I just need ask a noobie question you may know. I've only built programs with visual studios 2010 that have only one .cpp file which is main. Since this is my first one I've done with 3 files I'm wondering where I'd find my output. As in where my couts have posted. I've already built it says it was a successful build

Alright, I figured it out. Sorry! I've got three warnings in my main now next to the declared objects in class Rectangle

it says
"Warning 1 warning C4305: 'argument' : truncation from 'double' to 'float' H:\C++\Programming assignment one\Programming assignment one\Programming assignment 1 main.cpp 17"

"Warning 2 warning C4305: 'argument' : truncation from 'double' to 'float' H:\C++\Programming assignment one\Programming assignment one\Programming assignment 1 main.cpp 17
"

"Warning 3 warning C4305: 'argument' : truncation from 'double' to 'float' H:\C++\Programming assignment one\Programming assignment one\Programming assignment 1 main.cpp 18
"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.