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;
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 :)
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().