I have to write a program that outputs the length and width of a rectangle and then calculates and outputs the area, using a class called rectangle...
I thought I was about done, but when I run the program it says width, length and area are being used without being intitialized...
Here is my code...
the header file...
class Rectangle
{
private:
int _width;
int _length;
int _area;
public:
void SetDimensions( int width, int length, int area );
void GetDimensions( int &width, int &length );
void GetArea( int &area, int &width, int &length );
void DisplayDimensions();
void HoldScreen();
Rectangle();
~Rectangle();
};
function definitions...
#include<iostream>
#include "oRectangle.h"
using namespace std;
Rectangle::Rectangle()
{
_width = 0;
_length = 0;
_area = 0;
}
Rectangle::~Rectangle(){}
void Rectangle::SetDimensions( int width, int length, int area )
{
_width = width;
_length = length;
_area = area;
}
void Rectangle::GetDimensions( int &width, int &length )
{
cout << "Enter the length of the rectangle: ";
cin >> _length;
cout << "Enter the width of the rectangle: ";
cin >> _width;
}
void Rectangle::GetArea( int &area, int &width, int &length )
{
area = ( _width * _length );
}
void Rectangle::DisplayDimensions()
{
cout << "Length of Rectangle: " << _length;
cout << "Width of Rectangle: " << _width;
cout << "So Area of Rectangle: " << _area;
cin.ignore( 99, '\n' );
}
void Rectangle::HoldScreen()
{
cout << "Return to continue";
cin.ignore( 99, '\n' );
}
and main...
#include<iostream>
#include "oRectangle.h"
using namespace std;
int main()
{
int width;
int length;
int area;
Rectangle d;
d.SetDimensions( width, length, area );
d.GetDimensions( width, length );
d.GetArea( area, width, length );
d.DisplayDimensions();
d.HoldScreen();
}
Didn't I initialize them in my constructor?
Rectangle::Rectangle()
{
_width = 0;
_length = 0;
_area = 0;
}