-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trapezoid.cpp
125 lines (115 loc) · 3.73 KB
/
Trapezoid.cpp
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
//******************************************************************************
// Trapezoid.cpp
// Inheritance
//
// Created by Karlina Beringer on July 10, 2014.
//
// This source file contains the Trapezoid class definition.
// Trapezoid is derived from the Quadrilateral class.
//******************************************************************************
#include "Trapezoid.h"
// Helper method forces exactly two sides to be parallel.
void Trapezoid::setAttributes()
{
// Check that only AB and CD are parallel
// or else that only BC and DA are parallel; not both!
double slope_AB = A.getSlope( B );
double slope_CD = C.getSlope( D );
double slope_BC = B.getSlope( C );
double slope_AD = A.getSlope( D );
if (!(slope_AB == slope_CD xor slope_BC == slope_AD))
{
A = Point( 0, 0 );
B = Point( 1, 1 );
C = Point( 3, 1 );
D = Point( 5, 0 );
}
// Set Trapezoid-specific attributes.
if (slope_AB == slope_CD)
{
if (A.getDistance( B ) > C.getDistance( D ))
{
base1 = A.getDistance( B );
base2 = C.getDistance( D );
}
else
{
base1 = C.getDistance( D );
base2 = A.getDistance( B );
}
height = B.getDistance( C );
}
else
{
if (B.getDistance( C ) > D.getDistance( A ))
{
base1 = B.getDistance( C );
base2 = D.getDistance( A );
}
else
{
base1 = D.getDistance( A );
base2 = B.getDistance( C );
}
height = A.getDistance( B );
}
}
// Default constructor calls Quadrilateral's default constructor.
Trapezoid::Trapezoid(): Quadrilateral()
{
setAttributes();
}
// Normal constructor calls Quadrilateral's normal constructor.
Trapezoid::Trapezoid( Point A, Point B, Point C, Point D ):
Quadrilateral( A, B, C, D )
{
setAttributes();
}
// Copy constructor calls Quadrilateral's copy constructor.
Trapezoid::Trapezoid( Quadrilateral & quad ): Quadrilateral( quad )
{
setAttributes();
}
// Getter returns the area of this Quadrilateral.
// Override the method to customize it for Trapezoid.
// Area = [(Base1 + Base2) * Height] / 2
double Trapezoid::getArea()
{
return (( base1 + base2 ) * height) / 2;
}
// Print the contents of this Quadrilateral.
// Override the method to customize it for Trapezoid.
// If no ostream parameter is supplied, default will be cout.
void Trapezoid::print( ostream & output )
{
output << "------------------------\n";
output << TYPE << "\n";
output << "------------------------\n";
output << "Points:\n";
output << "\tA = " << A << "\n";
output << "\tB = " << B << "\n";
output << "\tC = " << C << "\n";
output << "\tD = " << D << "\n";
output << "Side Lengths:\n";
output << "\tAB = " << A.getDistance( B ) << "\n";
output << "\tBC = " << B.getDistance( C ) << "\n";
output << "\tCD = " << C.getDistance( D ) << "\n";
output << "\tDA = " << D.getDistance( A ) << "\n";
output << "Shape Properties:\n";
output << "\tbase1= " << base1 << "\n";
output << "\tbase2 = " << base2 << "\n";
output << "\theight = " << height << "\n";
output << "\tPerimeter = " << getPerimeter() << "\n";
output << "\tArea = " << getArea() << "\n\n";
}
// Friend function behaves like Trapezoid's print method.
// Overloads the ostream operator.
// Friend is NOT a member of this class, but has access to its members.
ostream & operator << ( ostream & output, Trapezoid & trap )
{
trap.print( output );
return output;
}
//******************************************************************************
// End of File
//******************************************************************************