Created
September 11, 2018 18:21
-
-
Save SibTiger/960475513391ac33fe250a37751c3e4d to your computer and use it in GitHub Desktop.
Introductory to OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ===================================================== | |
// Programmer: Nicholas Gautier | |
// Class: CS1514; Computer Science II [Tutoring Copy] | |
// Assignment #: N/A | |
// Due Date: N/A | |
// Instructor: Dr. Jawad Drissi | |
// Description: This program is designed to create a class that has | |
// a dependency with another class. This program is | |
// merely a an example of creating a class that | |
// specifically requires another class to obtain and | |
// manipulate values to such class. With this program, | |
// we have an special array class that depends on the | |
// Box class for details; height, width, length. | |
// | |
// introductory to OOP | |
// | |
// NOTE: I am using Visual Studio 2017, but there shouldn't be any issues - it is cross platform friendly. | |
// ---- | |
// COMPILING NOTES FOR G++ | |
// - NOTHING YET - | |
// ---- | |
// Return Codes: | |
// 0 = Successful Operation | |
// !0 = See Operating System Documentation | |
// ===================================================== | |
// Inclusions | |
// ================================ | |
#include <iostream> // We must use this to capture data from the | |
// user and to display messages on the terminal | |
// buffer (or essentially, the user's screen.) | |
#include <stdlib.h> // Used for the rand() | |
// ================================ | |
// Macro Definitions | |
// ================================ | |
#define _ARRAYSIZE_ 5000 // How many elements we want within the array. | |
// NOTE: This is used in the ArrayOfBoxes class. | |
// All other parts of this program can access | |
// this macro-definition, but this only really | |
// being used in the ArrayOfBoxes. | |
// ================================ | |
// Box [CLASS] | |
// ----------------------------- | |
// Documentation: | |
// This class holds information regarding common data refering to a box form. | |
// ----------------------------- | |
class Box | |
{ | |
private: | |
int width; // Width of the box | |
int height; // Height of the box | |
int length; // Length of the box | |
public: | |
int getWidth(); // Retrieve the width of the box | |
void setWidth(int); // Set the width of the box | |
int getHeight(); // Retrieve the height of the box | |
void setHeight(int); // Set the height of the box | |
int getLength(); // Retrieve the length of the box | |
void setLength(int); // Set the length of the box | |
}; // CLASS - Box | |
// Get Width | |
// =============================== | |
// Documentation: | |
// This function will merely return the value of the | |
// box's width. | |
// =============================== | |
// Return | |
// Width [int] | |
// Width of the box. | |
// =============================== | |
int Box::getWidth() | |
{ | |
return width; | |
} // Box::getWidth() | |
// Set Width | |
// =============================== | |
// Documentation: | |
// This function will merely update the value of the | |
// box's width. | |
// =============================== | |
// Parameters | |
// newWidth [int] | |
// New width of the box. | |
// =============================== | |
void Box::setWidth(int newWidth) | |
{ | |
width = newWidth; | |
} // Box::setWidth() | |
// Get Height | |
// =============================== | |
// Documentation: | |
// This function will merely return the value of the | |
// box's height. | |
// =============================== | |
// Return | |
// Height [int] | |
// Height of the box. | |
// =============================== | |
int Box::getHeight() | |
{ | |
return height; | |
} // Box::getHeight() | |
// Set Height | |
// =============================== | |
// Documentation: | |
// This function will merely update the value of the | |
// box's height. | |
// =============================== | |
// Parameters | |
// newHeight [int] | |
// New height of the box. | |
// =============================== | |
void Box::setHeight(int newHeight) | |
{ | |
height = newHeight; | |
} // Box::setHeight() | |
// Get Length | |
// =============================== | |
// Documentation: | |
// This function will merely return the value of the | |
// box's length. | |
// =============================== | |
// Return | |
// Length [int] | |
// Length of the box. | |
// =============================== | |
int Box::getLength() | |
{ | |
return length; | |
} // Box::getLength() | |
// Set Length | |
// =============================== | |
// Documentation: | |
// This function will merely update the value of the | |
// box's Length. | |
// =============================== | |
// Parameters | |
// newLength [int] | |
// New length of the box. | |
// =============================== | |
void Box::setLength(int newLength) | |
{ | |
length = newLength; | |
} // Box::setLength() | |
// ----------------------------------------------- | |
// ArrayOfBoxes [CLASS] | |
// ----------------------------- | |
// Documentation: | |
// This class will hold a specific amount of box information. | |
// Because we want to hold 2 or more boxes, we will need | |
// this Class to hold as many boxes as we want - in an array! | |
// ----------------------------- | |
class ArrayOfBoxes | |
{ | |
private: | |
Box arrayOfBoxes[_ARRAYSIZE_]; // Are array of boxes | |
// NOTE: This depends on the Box class! | |
public: | |
void FillArray(); // Fill the array with random data | |
void ReadIndex(int); // Display information from a specific box | |
void SetIndex(int, int, int, int); // Manually update the information regarding a box. | |
void Sum(); // Provide the sum of the all the boxes height, | |
// length, width. | |
}; // CLASS - ArrayOfBoxes | |
// Fill Array | |
// =============================== | |
// Documentation: | |
// This function will populate all of the boxes that we want to hold | |
// based on the array size; the information available such as the: | |
// height, width, and the length, will be randomized by using the | |
// rand() function. | |
// =============================== | |
void ArrayOfBoxes::FillArray() | |
{ | |
// Start at the very first box - and update it's height, length, and width | |
// And then continue until we reach the maximum array size. | |
for (int i = 0; i < _ARRAYSIZE_; i++) | |
{ | |
arrayOfBoxes[i].setHeight(rand()); // Set the height | |
arrayOfBoxes[i].setLength(rand()); // Set the length | |
arrayOfBoxes[i].setWidth(rand()); // Set the width | |
} // for() | |
} // ArrayStuff::FillArray() | |
// Read Index | |
// =============================== | |
// Documentation: | |
// This function will provide information regarding a specific | |
// box within the array. | |
// =============================== | |
// Parameters | |
// key [int] | |
// The index that we want to inspect; to be used with the | |
// array. | |
// =============================== | |
void ArrayOfBoxes::ReadIndex(int key) | |
{ | |
std::cout << "Box ID: " << key << " Contains the following information:" << std::endl // Box ID | |
<< "Height:" << "\t" << arrayOfBoxes[key].getHeight() << std::endl // Box Height | |
<< "Width:" << "\t" << arrayOfBoxes[key].getWidth() << std::endl // Box Width | |
<< "Length:" << "\t" << arrayOfBoxes[key].getLength() << std::endl; // Box Length | |
} // ArrayOfBoxes::ReadIndex() | |
// Set Index | |
// =============================== | |
// Documentation: | |
// This function will allow the user to manually update | |
// a specific box's information | |
// =============================== | |
// Parameters: | |
// key [int] | |
// The index that we want to update. | |
// length [int] | |
// New length information to set. | |
// width [int] | |
// New width information to set. | |
// height [int] | |
// New height information to set. | |
void ArrayOfBoxes::SetIndex(int key, int length, int width, int height) | |
{ | |
arrayOfBoxes[key].setLength(length); // Update the length | |
arrayOfBoxes[key].setWidth(width); // Update the width | |
arrayOfBoxes[key].setHeight(height); // Update the height | |
} // ArrayOfBoxes::SetIndex() | |
// Sum | |
// =============================== | |
// Documentation: | |
// This will provide the summation of all of the boxes | |
// height, length, and the width. The information will | |
// be displayed on the terminal buffer. | |
// =============================== | |
void ArrayOfBoxes::Sum() | |
{ | |
// Declarations and Initializations | |
// ------------------------------- | |
int totalLength = 0; // Hold the sum of the Length | |
int totalWidth = 0; // Hold the sum of the width | |
int totalHeight = 0; // Hold the sum of the Height | |
// ------------------------------- | |
// Scan the entire array and accumulate the results | |
for (int i = 0; i < _ARRAYSIZE_; i++) | |
{ | |
totalHeight += arrayOfBoxes[i].getHeight(); // Get the sum for the height | |
totalLength += arrayOfBoxes[i].getLength(); // Get the sum for the length | |
totalWidth += arrayOfBoxes[i].getWidth(); // Get the sum for the width | |
} // for() | |
// Provide the information to the end-user | |
std::cout << "The sum for the following:" << std::endl | |
<< "Height: " << "\t" << totalHeight << std::endl | |
<< "Length: " << "\t" << totalLength << std::endl | |
<< "Width: " << "\t" << totalWidth << std::endl; | |
} // ArrayOfBoxes::Sum() | |
// ----------------------------------------------- | |
// Main | |
// =============================== | |
// Documentation: | |
// The main entry point to our program; the spine | |
// =============================== | |
// Return | |
// Exit Code [int] | |
int main() | |
{ | |
// Declarations and initializations | |
// ------------------------------ - | |
ArrayOfBoxes myBoxes; // Create a new instance of the ArrayOfBoxes | |
// ------------------------------- | |
// Now, lets play around with the newly created myBoxes! | |
myBoxes.FillArray(); // Populate the array of boxes | |
myBoxes.ReadIndex(50); // Provide information from index 50 | |
std::cout << std::endl; // Provide a new line; easier to read from the screen. | |
myBoxes.SetIndex(50, 4, 2, 8); // Manually provide new values to set into index 50 | |
myBoxes.ReadIndex(50); // With the information updated, now lets re-inspect | |
// the information to see if it was saved. | |
std::cout << std::endl; // Provide a new line; easier to read from the screen. | |
myBoxes.Sum(); // Calculate the sum and display it to the end-user. | |
std::cout << std::endl; // Provide a new line; easier to read from the screen. | |
// This will allow those that are using the Windows platform to view the results | |
// before the console window is terminated. Otherwise, without this - I can't see | |
// the results. | |
getc(stdin); // Press the 'enter' key to continue | |
// Terminate the program | |
return 0; | |
} // main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment