Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include "Thing.h"
- #include "Sphere.h"
- #include "Cube.h"
- #include "Cylinder.h"
- #include "ColoredCylinder.h"
- #include "ColoredSphere.h"
- #include "ColoredRectangularPrism.h"
- #include "ColoredCube.h"
- using namespace std;
- void populateThingPtr(Thing* &ptr, ifstream& inFile, char nameBuff[], char colorBuff[], double dbArr[]);
- int main() {
- const int numRecords = 50;
- Thing* shapePtr = nullptr;
- char nameBuff[30], colorBuff[9];
- double dbArr[4];
- //Pointers below will be used for downcasting
- GeometricSolid* gS = nullptr;
- ColoredThing* cT = nullptr;
- colorBuff[8] = 0; //color is always 8 bytes, so set index 9 to null permanently
- ifstream inFile("ass3data.bin", ios::binary | ios::in);
- if (!inFile) {
- cerr << "Unable to open input file" << endl;
- exit(1);
- }
- //Write headers
- cout << fixed << setprecision(4);
- cout << setw(30) << left << "Type of object" << setw(9) << right << "Volume" << " ";
- cout << left << setw(15) << "Color" << right << setw(15) << "Density" << endl;
- for (int i = 0; i < numRecords; i++) {
- inFile.read(nameBuff, 4 * sizeof(char));//4 for null
- populateThingPtr(shapePtr, inFile, nameBuff, colorBuff, dbArr);
- gS = dynamic_cast<GeometricSolid*>(shapePtr);
- if (gS) { //if downcast from Thing* to GeometricSolid* was successful
- cout << *gS;
- }
- cT = dynamic_cast<ColoredThing*>(shapePtr);
- if (cT) {// if downcast from Thing* to ColoredThing* was successful
- cout << *cT;
- }
- cout << endl;
- delete shapePtr;
- }
- }
- void populateThingPtr(Thing* &ptr, ifstream& inFile, char nameBuff[], char colorBuff[], double dbArr[]) {
- if (nameBuff == static_cast<string>("spn")) {//need to cast to string in order for == to work
- inFile.read(reinterpret_cast<char*>(dbArr), 1 * sizeof(double));
- ptr = new Sphere(dbArr[0]);
- }
- else if (nameBuff == static_cast<string>("cyn")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
- ptr = new Cylinder(dbArr[0], dbArr[1]);
- }
- else if (nameBuff == static_cast<string>("ren")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 3 * sizeof(double));
- ptr = new RectangularPrism(dbArr[0], dbArr[1], dbArr[2]);
- }
- else if (nameBuff == static_cast<string>("cun")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 1 * sizeof(double));
- ptr = new Cube(dbArr[0]);
- }
- else if (nameBuff == static_cast<string>("cyc")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 3 * sizeof(double));
- inFile.read(colorBuff, 8 * sizeof(char));
- ptr = new ColoredCylinder(dbArr[0], dbArr[1], dbArr[2], colorBuff);
- }
- else if (nameBuff == static_cast<string>("spc")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
- inFile.read(colorBuff, 8 * sizeof(char));
- ptr = new ColoredSphere(dbArr[0], dbArr[1], colorBuff);
- }
- else if (nameBuff == static_cast<string>("rec")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 4 * sizeof(double));
- inFile.read(colorBuff, 8 * sizeof(char));
- ptr = new ColoredRectangularPrism(dbArr[0], dbArr[1], dbArr[2], dbArr[3], colorBuff);
- }
- else if (nameBuff == static_cast<string>("cuc")) {
- inFile.read(reinterpret_cast<char*>(dbArr), 2 * sizeof(double));
- inFile.read(colorBuff, 8 * sizeof(char));
- ptr = new ColoredCube(dbArr[0], dbArr[1], colorBuff);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment