Rectangle Class Template

I'm fairly new to class templates, so I'm having a bit of trouble with calling functions from the class. I really am trying to learn, but I'm very lost with how to continue with my main function, or if I even initialized the object from Rectangle correctly.

Requirements:
Create a Rectangle class template that can store the lengths of the sides (in the private section) as any DataType.
Make class functions for setting the width and length of the rectangle.
Make a function that will return "true" if the length is greater than the width and "false" otherwise.
Make two other functions that will return the perimeter and area of the Rectangle.
Test the Rectangle class template by using a float for DataType and a Fraction for DataType.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// RectangleClass.h
#include <cmath>
using namespace std;

template <class T>
class Rectangle
{
private:
	T length, width;
public:
	Rectangle();
	Rectangle(T l, T w)
	{
		l = length;
		w = width;
	}

	void setLength(T l)
	{
		l = length;
	}

	void setWidth(T w)
	{
		w = width;
	}

	void getLength()
	{
		return length;
	}

	void getWidth()
	{
		return width;
	}

	bool checkGreatThan(T length, T width);
	getPerimeter(T length, T width);
	getArea(T length, T width);
};

template<class T>
bool Rectangle<T>::checkGreatThan(length, width)
{
	if (length > width)
		return true;
	else
		return false;
}

// Perimeter
template<class T>
T Rectangle<T>::getPerimeter(length, width)
{
	T perimeter;
	perimeter = length + length + width + width;
	return perimeter;
}

// Area
template<class T>
T Rectangle<T>::getArea(length, width)
{
	T area;
	area = length * width;
	return area;
}



// RectangleMain.cpp
#include <iostream>
#include "RectangleClass.h"

using namespace std;

int main()
{
	Rectangle<float> dRectangle;
	float length, width;

	cout << "Enter the length of the rectangle: ";
	cin >> length;
	dRectangle.setLength(length);
	cout << "Enter the width of the rectangle: ";
	cin >> width;
	dRectangle.setWidth(width);
}
Last edited on
Your code is looking good - just couple of typos in theRectangle(T l, T w) constructor and the void setLength(T l) and void setWidth(T w) functions:

Does it really want to be l = length; or should it be the other way around? (and the same for the w = width;)

Also, although you have declared the default constructor Rectangle();, you have not provided a definition (i.e. assign default values to length and width within curly braces, or use member initialisation if you are feeling adventurous).

As for testing, you just need to add calls to the checkGreatThan getPerimeter and getArea functions on you dRectangle object, and output the result with cout <<

I'm not sure what the Fraction type is - I guess it's a class defined somewhere else, or in a previous exercise. You should just need to create a Rectangle<Fraction> and test it in the same way as you've tested the float version.
As Norm pointed outed out, lines 14,15,20,25 your assignment statements are reversed.

Lines 28,33: Your return type should be type T, not void.

Lines 39,40: Your prototypes should have a return type of T.



Thank you both, I've never made the mistake of reversing my assignment statements before, just wasn't really thinking logically.

For dRectangle.setLength(length);, am I storing the length correctly and as the right data type?

Following this, if i were to call the function checkGreatThan, would bool check = checkGreatThan(length,width); return true or false to check?

Thanks again, I really appreciate the guidance.
Last edited on
Also, dRectangle.setLength(length); is giving me an error of "class 'Rectangle<float>' has no member 'setLength'". How would I go around fixing this?
Why do getPerimeter() and getArea() take parameters? Shouldn't they compute the perimeter and area of this rectangle?
@dhayden,
When I call the function, wouldn't I pass the length and width to the function by using paramters?
wouldn't I pass the length and width to the function by using parameters?

No. You want the perimeter of a dRectangle instance.

You already know the length and width of the instance. That's what the private length and width variables are for. No additional information is needed.
Last edited on
How would I call a function from a class template? Would it be the same as calling from a class?
-----------------------------------------------------------------------
I'm just confused as to why I'm getting the error "class 'Rectangle<float>' has no member 'setLength'"
Last edited on
After making the corrections we've discussed and updating for const correctness, I get no errors compiling the following:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// RectangleClass.h
#include <cmath>
using namespace std;

template <class T>
class Rectangle
{
private:
	T length, width;
public:
	Rectangle()
	{   length = 0;
	    width = 0;
	}
	
	Rectangle(T l, T w)
	{   length = l;
	    width = w;		
	}

	void setLength(T l)
	{   length = l;
	}

	void setWidth(T w)
	{   width = w;
	}

	void getLength() const
	{   return length;
	}

	void getWidth() const
	{   return width;
	}

	bool checkGreatThan() const;
	T getPerimeter () const;
	T getArea () const;
};

template<class T>
bool Rectangle<T>::checkGreatThan() const
{   return (length > width);
}

// Perimeter
template<class T>
T Rectangle<T>::getPerimeter() const
{   T perimeter;
	perimeter = length + length + width + width;
	return perimeter;
}

// Area
template<class T>
T Rectangle<T>::getArea() const
{   T area;
	area = length * width;
	return area;
}

// RectangleMain.cpp
#include <iostream>
//  #include "RectangleClass.h"

using namespace std;

int main()
{
	Rectangle<float> dRectangle;
	float length, width;

	cout << "Enter the length of the rectangle: ";
	cin >> length;
	dRectangle.setLength(length);
	cout << "Enter the width of the rectangle: ";
	cin >> width;
	dRectangle.setWidth(width);
}


How would I call a function from a class template? Would it be the same as calling from a class?

Yes, exactly the same.

Last edited on
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cmath>
using namespace std;

template <class T>
class Rectangle
{
private:
	T length, width;
public:
	Rectangle()
	{
		length = 0;
		width = 0;
	}
		
	Rectangle(T l, T w)
	{
		length = l;
		width = w;
	}

	void setLength(T l)
	{
		length = l;
	}

	void setWidth(T w)
	{
		width = w;
	}

	T getLength()
	{
		return length;
	}

	T getWidth() 
	{
		return width;
	}

	bool checkGreatThan() const;
	T getPerimeter() const;
	T getArea() const;
};

template<class T>
bool Rectangle<T>::checkGreatThan() const
{
	if (length > width)
		return true;
	else
		return false;
}

// Perimeter
template<class T>
T Rectangle<T>::getPerimeter() const
{
	T perimeter;
	perimeter = length + length + width + width;
	return perimeter;
}

// Area
template<class T>
T Rectangle<T>::getArea() const
{
	T area;
	area = length * width;
	return area;
}

struct Fraction
{
public:
	int numerator, denominator;
	Fraction(int n = 0, int d = 0)
	{
		n = numerator;
		d = denominator;
	}

};

Fraction operator+(Fraction frac1, Fraction frac2)
{
	Fraction result;
	result.numerator = (frac1.numerator * frac2.denominator) + (frac2.numerator * frac1.denominator);
	result.denominator = (frac1.denominator) * (frac2.denominator);
	return result;
}

Fraction operator-(Fraction frac1, Fraction frac2)
{
	Fraction result;
	result.numerator = (frac1.numerator * frac2.denominator) - (frac2.numerator * frac1.denominator);
	result.denominator = frac1.denominator * frac2.denominator;
	return result;
}

Fraction operator*(Fraction frac1, Fraction frac2)
{
	Fraction result;
	result.numerator = frac1.numerator * frac2.numerator;
	result.denominator = frac1.denominator * frac2.denominator;
	return result;
}

bool operator>(Fraction frac1, Fraction frac2)
{
	if ((frac1.numerator / frac1.denominator) > (frac2.numerator / frac2.denominator))
		return true;
	else
		return false;
}


Fraction operator*(Fraction frac1, int number)
{
	Fraction result;
	result.numerator = frac1.numerator * number;
	result.denominator = frac1.denominator;
	return result;
}

Fraction operator*(int number, Fraction frac1)
{
	Fraction result;
	result.numerator = number * frac1.numerator;
	result.denominator = number * frac1.denominator;
	return result;
}

void PrintFraction(Fraction frac)
{
	cout << frac.numerator << "/" << frac.denominator;
}

// RectangleMain.cpp

#include <iostream>
#include "RectangleClass.h"
using namespace std;

int main()
{
	Fraction fractionOne, fractionTwo, fracArea, fracPerimeter, fracLength, fracWidth;
	Rectangle<float> dRectangle;
	Rectangle<Fraction> fRectangle;
	float length, width, area, perimeter;
	bool check, fracCheck;


	cout << "Enter the length of the rectangle: ";
	cin >> length;
	dRectangle.setLength(length);
	cout << "Enter the width of the rectangle: ";
	cin >> width;
	dRectangle.setWidth(width); 
	cout << endl;

	check = dRectangle.checkGreatThan();
	if (check == true)
		cout << "The length is longer than the width of the rectangle." << endl;
	else
		cout << "The length is shorter than the width of the rectangle." << endl;

	perimeter = dRectangle.getPerimeter();
	cout << "The perimeter of the rectangle is " << perimeter << " units." << endl;
	area = dRectangle.getArea();
	cout << "The area of the rectangle is " << area << " units squared." << endl;

	cout << "----------------------------------------------------------" << endl;
	cout << "Enter the numerator of the length: ";
	cin >> fractionOne.numerator;
	fRectangle.setLength(fractionOne.numerator);
	cout << "Enter the denominator of the length: ";
	cin >> fractionOne.denominator;
	fRectangle.setLength(fractionOne.denominator);
	cout << "Enter the numerator of the width: ";
	cin >> fractionTwo.numerator;
	fRectangle.setWidth(fractionTwo.numerator);
	cout << "Enter the denominator of the width: ";
	cin >> fractionTwo.denominator;
	fRectangle.setWidth(fractionTwo.denominator);

	cout << "Length: ";
	PrintFraction(fractionOne);
	cout << "\nWidth: ";
	PrintFraction(fractionTwo);
	cout << endl;

	fracCheck = operator>(fractionOne, fractionTwo);
	if (fracCheck == true)
	{
		PrintFraction(fractionOne);
		cout << " is greater than ";
		PrintFraction(fractionTwo);
		cout << "." << endl;
	}
	else
	{
		PrintFraction(fractionTwo);
		cout << " is greater than ";
		PrintFraction(fractionOne);
		cout << "." << endl;
	}
}


I have figured everything out so far with some guidance. However, how would i call the getArea/getPerimeter functions for the Fraction type? Logically, I don't really see how it could work.

Last edited on
Lines 80-81: Your assignment statements are reversed again.

Line 177,180: You're setting the length first to the numerator, then to the denominator. The second call is going to overlay the first. What you want to do here is set the value of the fraction (both numerator and denominator, then set the length to that fraction.

1
2
3
4
5
	cout << "Enter the numerator of the length: ";
	cin >> fractionOne.numerator;
	cout << "Enter the denominator of the length: ";
	cin >> fractionOne.denominator;
	fRectangle.setLength (fractionOne);  // the length is a complete fraction 


Lines 183,186: Ditto for width.

how would i call the getArea/getPerimeter functions for the Fraction type?

Exactly the same way, except they return a Fraction type.

1
2
3
4
5
6
7
8
  Fraction farea;
  Fraction length (1,2);
  Fraction width (3,4);
  Rectangle<Fraction> frect (length, width);

  farea = frect.getArea ();
  cout << "The area is: ";
  PrintFraction (farea);

Last edited on
Everything seems to become a lot more clearer now. I've made a few adjustments to what you said and I've gotten the program to work exactly how I wanted. Thanks everyone for the help
Topic archived. No new replies allowed.