0

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;
	
}
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Gagless
0

You don't seem to understand the concept of using an object. An object has its own data members, which are abstracted from a function like main(). Once you set the rectangle's width and length, you don't need to keep on passing 'length' and 'width' as arguments to a function unless you plan on changing some data. The rectangle already knows its height and width.

>Didn't I initialize them in my constructor?
You initialized _width, _length, and _area in your constructor. What you didn't initialize were the variables that you declared in main(). Those won't get initialized by your constructor.

0

You don't seem to understand the concept of using an object. An object has its own data members, which are abstracted from a function like main(). Once you set the rectangle's width and length, you don't need to keep on passing 'length' and 'width' as arguments to a function unless you plan on changing some data. The rectangle already knows its height and width.

>Didn't I initialize them in my constructor?
You initialized _width, _length, and _area in your constructor. What you didn't initialize were the variables that you declared in main(). Those won't get initialized by your constructor.

edit
Nevermind. I understand now, thank you very much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.