Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task: An equilateral triangle is a triangle whose sides are equal. If two equliateral triangles are glued together along a common side, this will form
Task:
An equilateral triangle is a triangle whose sides are equal. If two equliateral triangles are "glued" together along a common side, this will form a diamond. You are to write a class called Diamond, using filenames diamond.h and diamond.cpp, that will allow the creation and handling of diamonds based on the above description, whose sides are integers in the range 1-39. Use the const qualifier on member functions wherever it is appropriate.Details:
- The single constructor for the Diamond class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with a default of '*'). If the size provided is less than 1, set the size to 1. If the size provided is greater than 39, set the size to 39. The class will need to provide internal storage for any member data that must be kept track of.
- There should be member functions GetSize, Perimeter, and Area, which will return the size of a side, the perimeter of the diamond, and the area of the diamond, respectively. The first 2 should return integer results. The Area function should return its result as a double.
- There should be member functions Grow and Shrink, which will increase or decrease (respectively) the size of the Diamond's sides by 1, unless this would cause the size to go out of bounds (out of the 1-39 range); in the latter case, Grow and Shrink should make no change to the size.
- There should be member functions SetBorder and SetFill, which each allow a new border or fill character (respectively) to be passed in as a parameter. There is a chart of ASCII characters in an appendix of the textbook. The characters that should be allowed for the border or fill characters are any characters from the '!' (ascii 33) up through the '~' (ascii 126). If an attempt is made to set the border or fill characters to anything outisde the allowable range, the function should set the border or fill back to its original default (the ones listed for the constructor -- the border default is '#' and the fill default is '*').
- There should be a member function called Draw that will display a picture of the Diamond on the screen. You may assume that the cursor is already at the beginning of a line when the function begins, and you should make sure that you leave the cursor on the line following the picture afterwards (i.e. print a newline after the last line of the diamond). Use the border character to draw the border of the diamond, and use the fill character to draw the internal characters. Separate the characters on a line in the picture by a single space to make the Diamond look more proportional (so that the halves look more like equilateral triangles). You may not use formatting functions like setw to draw the diamond. This must be handled with loops. (You will only print out the newline, spaces, the border character, and maybe the fill character on any given line).
- Provide a member function called Summary that displays all information about a diamond: its size, perimeter, area, and a picture of what it looks like. When displaying the area (decimal data), always show exactly 2 decimal places. Your output should be in the exact same format as mine (seen in the linked sample run below)
- I am providing a sample driver program (called driver.cpp) that uses objects of type Diamond and illustrates the usage of the member functions. You can get the driver.cpp at the end of the document.
- General Requirements
- No global variables, other than constants!
- All member data of your class must be private
- Use the const qualifier on member functions wherever it is appropriate.
- You will need to use the
library for output. You may use the library for formatting your decimal output to two places, if you wish to use the parameterized stream manipulators, but you may not use setw() or other output formatting functions for drawing the actual diamond. You may use the library - Do not use langauge or library features that are C++11-only
- When you write source code, it should be readable and well-documented.
- Your diamond.h file should contain the class declaration only. The diamond.cpp file should contain the member function definitions.
driver.cpp:
#include#include #include "diamond.h" using namespace std; int main() { // create some Diamonds Diamond d1( -5 ), d2( 7, '^' ), d3( 12, 'X', 'O' ), d4( 50 , '$' , 'o'); // display original Diamonds cout << "d1 has size = " << d1.GetSize() << " units. "; d1.Draw(); cout << " d2 has size = " << d2.GetSize() << " units. "; d2.Draw(); cout << " d3 has size = " << d3.GetSize() << " units. "; d3.Draw(); cout << " d4 has size = " << d4.GetSize() << " units. "; d4.Draw(); cout << ' '; d1.Shrink(); // demonstrate shrink d2.Shrink(); d3.Grow(); // and grow d4.Grow(); cout << "d1 now has size = " << d1.GetSize() << " units. "; cout << "d2 now has size = " << d2.GetSize() << " units. "; cout << "d3 now has size = " << d3.GetSize() << " units. "; cout << "d4 now has size = " << d4.GetSize() << " units. "; // demonstrate perimeter cout << "d2 has perimeter = " << d2.Perimeter() << " units. "; cout << "d3 has perimeter = " << d3.Perimeter() << " units. "; // and area cout << "d2 has area = " << d2.Area() << " square units. "; cout << "d3 has area = " << d3.Area() << " square units. "; d1.Draw(); d1.Grow(); // show that fill character cout << "d1 grows: "; // appears only when size d1.Draw(); // is at least 3 d1.Grow(); cout << "... and grows: "; d1.Draw(); cout << ' '; d1 = d2; // demonstrate the default overload of the // assignment operator cout << "d1 now has size = " << d1.GetSize() << " units. "; d1.Draw(); // demonstrate the changing of border and fill characters d2.SetBorder('@'); d2.SetFill('-'); cout << "d2 now looks like: "; d2.Draw(); cout << ' '; d2.SetBorder(' '); // illegal border d2.SetFill('\a'); // illegal fill cout << "d2 now looks like: "; d2.Draw(); cout << ' '; cout << " Here is a summary on d3: "; // demonstrate summary d3.Summary(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started