Object-oriented Rectangle program
Hi all, I'm new to C++ and have jumped right into object-oriented. Some of the concepts and syntax have me confused like no other. If anyone could take a look at the code an offer pointers/guidane it would be greatly appreciated.
The jist of the assignment is to have code with a class Rectangle that calculates the area and perimeter and determines whether or not the obect is a square. There are some querky quideline such as have 3 overload constructor functions width 0-2 parameters.
Here is the code:
Code:
//Rectangle.cpp
//Rectangle class member functions
#include
<iostream>
#include
<iomanip>
#include
"Rectangle.h"
using
namespace std;
Rectangle::Rectangle()
{
setLength(1.0);
setWidth(1.0);
}
Rectangle::Rectangle(length)
{
setLength();
setWidth(1.0);
}
Rectangle::Rectangle(length, width)
{
setLength();
setWidth();
}
double
Rectangle::calcArea()
{
return length*width;
}
double
Rectangle::calcPerimeter()
{
return (2*length)+(2*width);
}
void
Rectangle::setLength (double l)
{
if (l >= 0 && l <= 20)
length = l;
else
{
length = 1.00;
cout<<
"The length entered is invalid and has been set to 1.00";
}
}
void
Rectangle::setWidth (double w)
{
if (w >= 0 && w <= 20)
width = w;
else
{
width = 1.00;
cout<<
"The width entered is invalid and has been set to 1.00";
}
}
double
Rectangle::getLength() const
{
return length;
}
double
Rectangle::getWidth() const
{
return width;
}
void
Rectangle::booleanSquare()
{
/*
if (length-width < .0001)
*/
}
void
printData()
{
cout<<
"Length: " << length;
cout<<
"Width: " << width;
cout<<
"Area: " << area;
cout<<
"Perimeter: " << perimeter;
}
Code:
//Rectangle.h
//Defintion of class Rectangle
#ifndef
RECTANGLE_H
#define
RECTANGLE_H
class
Rectangle
{
public
:
//overloaded constructor functions
Rectangle ();
Rectangle (
double length);
Rectangle (
double length, double width);
//set functions
void setLength( double );
void setWidth( double );
//get functions
double getLength() const;
double getWidth() const;
//calculation functions
double calcPerimeter() const;
double calcArea() const;
//test for square
void booleanSquare ();
//print functions
void printData();
private
:
double length;
double width;
};
#endif
Code:
//Program to calculate the perimeter and area of a rectangle
#include
<iostream>
#include
"Rectangle.h"
using
namespace std;
int
main()
{
Rectangle object1;
Rectangle object2 (7.1, 3.5);
Rectangle object3 (6.3);
Rectangle object4 (21, 22);
Rectangle object5 = object2;
void printData();
/*
Rectangle object1 (5.4, 10.5);
Rectangle object4 (15.6, 15.6);
*/
void printData();
}
A couple direct questions I have so far.
How can I redefine the values of objects in main (one of the guidelines)
I'm obviously way off on how the Rectangle functions should be written/work I'm receiving errors on most of them. Illegal reference to non static member.
Why are my print function identifiers innaccurat?