problem getting my classes to work
Feb 21, 2014 at 2:00am UTC
Just trying to get it to work. I am new to classes and have read over the tutorials, including the one on this site, but am still confused. Whats wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
This program calculates the area and perimeter of a rectangle using classes. It then tests whether the two are equal
in both parameters.
*/
#include <iostream>
using namespace std;
//calculates area and perimeter of the rectangle
class Rect
{
double length, width; //sides of the rectangle
double perimeter; //perimeter
double areaRect; //area
public :
Rect(double , double );
//void set_sides(double set_length, double set_width);
double perim(double length, double width);
double area(double length, double width);
double getPerimeter() { return perimeter; }
double getArea() { return areaRect; }
};
Rect::Rect(double a, double b)
{
length = a;
width = b;
}
/*
void Rect::set_sides(double set_length, double set_width)
{
length = set_length;
width = set_width;
}
*/
double Rect::perim(double length, double width)
{
cout << length << '\t' << width << endl;
perimeter = (2 * length) + (2 * width);
cout << perimeter << endl;
return perimeter;
}
double Rect::area(double length, double width)
{
areaRect = length * width;
cout << areaRect << endl;
return areaRect;
}
int main()
{
Rect rect1(5,6);
Rect rect2(10,3);
if (rect1.getPerimeter() == rect2.getPerimeter())
cout << "The perimeter's of each rectangle are the same." << endl;
if (rect1.getArea() == rect2.getArea())
cout << "The area's of each rectangle are the same" << endl;
system("pause" );
return 0;
}
Feb 21, 2014 at 2:29am UTC
the value of your perimeter and areaRect variables is undefined because you never call the method that calculates them.
also you don't need to pass length and width to your perim or area methods.
Feb 21, 2014 at 4:03pm UTC
solved thank you
Topic archived. No new replies allowed.