forked from Karlina-Bytes/Inheritance_Tutorial
/
Square.cpp
101 lines (90 loc) · 2.88 KB
/
Square.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
//******************************************************************************
// Square.cpp
// Inheritance
//
// Created by Karlina Beringer on July 10, 2014.
//
// This source file contains the Square class definition.
// Square is derived from the Rectangle class.
//******************************************************************************
#include "Square.h"
// Helper method forces length and width to be the same length.
void Square::setAttributes()
{
// Call the setAttributes method from the parent class.
Rectangle::setAttributes();
//setAttributes();
if (length != width)
{
A = Point( 0, 0 );
B = Point( 0, 5 );
C = Point( 5, 5 );
D = Point( 5, 0 );
}
width = 5;
length = 5;
}
// Default constructor calls Rectangle's default constructor.
Square::Square(): Rectangle()
{
setAttributes();
}
// Normal constructor calls Rectangle's normal constructor.
Square::Square( Point A, Point B, Point C, Point D ):
Rectangle( A, B, C, D )
{
setAttributes();
}
// Copy constructor calls Rectangle's copy constructor.
Square::Square( Quadrilateral & quad ): Rectangle( quad )
{
setAttributes();
}
// Getter returns the perimeter of this Rectangle.
// Override the method to customize it for Square.
// Perimeter = Length * 4
double Square::getPerimeter()
{
return length * 4;
}
// Getter returns the area of this Rectangle.
// Override the method to customize it for Square.
// Area = Length * Length
double Square::getArea()
{
return length * length;
}
// Print the contents of this Rectangle.
// Override the method to customize it for Square.
// If no ostream parameter is supplied, default will be cout.
void Square::print( ostream & output )
{
cout << "--------------------------------\n";
output << TYPE << "\n";
cout << "--------------------------------\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 << "\tlength = " << length << "\n";
output << "\tPerimeter = " << getPerimeter() << "\n";
output << "\tArea = " << getArea() << "\n\n";
}
// Friend function behaves like Square'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, Square & sqr )
{
sqr.print( output );
return output;
}
//******************************************************************************
// End of File
//******************************************************************************