Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im needing help with the following c++ code: The program you are making needs to calculate the Volume and Surface area of regular blocks and

Im needing help with the following c++ code:

The program you are making needs to calculate the Volume and Surface area of regular blocks and save them as a record in a database. It currently has a Rectangle class and a main menu. You need to create a Block class that is derived from the Rectangle class.

1. Create the Block class that will be derived from the Rectangle class.

1. Create a constructor that receives the height, length and width and passes length and width along to the Rectangle class

2. Create a getSurfArea method and a getVolume method.

2. In the makeBlock function, ask the user for a height. Instantiate the Block class and use the getSurfArea and getVolume functions to find the total SurfArea and total Volume.

3. Write the line to save the block info (saveBlock() function in Rectangle.h file)

4. Write the code that will search through the file to find a block in the getBlock() function.

Rectangle Class:

//This file defines the Rectangle and Block classes as well as handling DB

functions

//getBlock and saveBlock

#include

#include

#include

using namespace std;

//Rectangle Class Declaration

class Rectangle

{

private:

long width;

long length;

public:

//constructor sets width and length

Rectangle(long w, long l)

{ width = w; length = l; calcArea(); }

//accessor functions for width and length

long getWidth()

{ return width; }

long getLength()

{ return length; }

//returns the rectangle's area

long calcArea()

{

long temp = length * width;

return temp;

}

};

class Block : public Rectangle

{

//<--------------------------------------------------Block code here

};

void saveBlock(string ID, long width, long length, long height)

{

ofstream fileOut;

//open file

fileOut.open("Blocks.txt", ios::app);

//<-------------------------code to place comma delimited block in

text file

fileOut.close();

}//end saveBlock

void getBlock(string blockID)

{

ifstream fileIn;

string record;

fileIn.open("Blocks.txt");

//if file doesn't open

if (fileIn.fail())

{

cout << "Problem Opening File.";

}

else

{

while(!fileIn.eof())

{

//<-----------------------------place code to search through

file for block

}//end while

cout << "Block not on record";

return;

}//end else

}//end getBlock

Main Menu:

/*

Description: This program will calculate the surface area and volume of a

regular

block which. It uses a rectangle class for the base of the block and then

derives

the block class from the rectangle class.

*/

#include

#include

#include "Rectangle.h"

using namespace std;

void makeBlock();

//creates new block

int main()

{

//menu variables

int choice = 0;

string blockID;

while (choice !=3)

{

//main menu

cout << " \tMain Menu ";

cout << "1.\tCreate New Block ";

cout << "2.\tRetrieve Saved Block ";

cout << "3.\tExit ";

cin >> choice;

//process choice

switch (choice)

{

case 1:

makeBlock();

break;

case 2:

cout<< "Enter block ID: ";

cin >> blockID;

getBlock(blockID);

break;

case 3:

break;

default:

cout << choice << "is not a valid choice. ";

}//end switch

}//end while

}//end Main

void makeBlock()

{

string saveYN;

string blockID;

long length, width, height, SurfArea, Volume;

//user input for block length

do

{

cout << "Enter the length of the base as a positive rational

number in meters: ";

cin >> length;

}while(length < 0);

//Rectangle base width

do

{

cout << "Enter the width of the base: ";

cin >> width;

}while(width < 0);

//<----------------------place code here that prompts user for height.

//instances of the Rectangle class

Rectangle rec(width, length);

//<----------------------------------instance of block class code here

//<----------------------------------------calculations here

//calculate

cout << "The surface area of the block is: ";//<-----display surface

area here

cout << "The block has a volume of: "; //<-----------dispay volume

here

cout << "Save this block (y/n)? ";

cin >> saveYN;

if( saveYN == "y" || saveYN == "Y")

{

cout << "block ID: ";

cin >> blockID;

saveBlock(blockID, width, length, height);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago