Class- a private variable issue:

Hello,

This is my first time posting, sorry if I make a mistake :(

I have made a basic class program to test out the use of private and public sections. But when I run the program my variable area is not passed back with the correct value. I believe it is a pointer issue, but anywhere I look I am not getting the answer I need.

here is my code:

#include <iostream>
using namespace std;

class CRectangle {
private:
double height;
double width;
double area;

public:

CRectangle(){
height = 0; width = 0; area = 0;
}

CRectangle(double x, double y){
height = x; width = y;
}

~CRectangle(){};

void calculateArea(){
area = height * width;
}

double getHeight() const{
return height;
}

double getWidth () const{
return width;
}

double getArea () const{
return area;
}

};

void valid_input(double ans){
if (cin.fail())
{
cout << "This is invalid input! e4xiting program class.cpp " << endl;
exit(1);
}
}

int main () {
double height;
double width;

cout << "Please enter the height:" << endl;
cin >> height;
valid_input(height);

cout << "Now the width: " << endl;
cin >> width;
valid_input(width);

CRectangle rect(height, width);

cout << "Height: " << rect.getHeight() << endl;
cout << "Width: " << rect.getWidth() << endl;
cout << "Area: " << rect.getArea() << endl;
return 0;
}

Thanks alot!
RawrImALion
Welcome to the boards!

In the future, please use the code brackets to contain code; make it easier on the eyes :)

That being said, can you tell us what happens when you compile and run? What exactly is breaking?
Last edited on
Thank you, and im so sorry that I did not use code brackets.

when I run the program I enter 5 for the width, and 5 for the height so the output looks like

Height : 5
Width: 5
Area: 4.24813e-314


and no matter what I enter for the Height and Width the Area stays the same.

Thanks again,
edit: wow, two posts in the time it took me to type mine!!! :)

for future reference:
1
2
// code that is posted with code tags, and that is properly indented and
// whitespaced is much easier to read... thus easier to help you  :) 


anyway, your problem is simple...
take a look at each of your constructors.
you'll notice that the default constructor initializes the 'area' variable in the class... the explicit constructor, however, does not. so when you create your rectangle and pass in the height and width to the constructor, that is fine, but then when you ask for the area... it hasn't ever been set.

also, the 'CalculateArea' member function isn't really necessary, easier to just do that in the constructor :)

p.s. just worth noting is that had you called 'CalculateArea' before asking for the value of 'area' this code should work, but as i said, easier to take care of in the constructor :)
Last edited on
Thank you so much!

It worked like a charm! The information I gained here was so much more help then my professor gave me. Again I am so sorry about not using the code tags :( but thank you for understanding and still helping me with my issue.

also i would just not have the are variable unless the assignment tells you to, since its very simple to calculate(h*w), and change the function to getArea().

just a suggestion
Topic archived. No new replies allowed.