Using C++ Create the inheritance hierarchy illustrated below. The GeometricSolid
ID: 3780304 • Letter: U
Question
Using C++ Create the inheritance hierarchy illustrated below. The GeometricSolid, Circular, and Rectangular classes should be abstract. Create three pure virtual functions in the GeometricSolid class and define/declare them in the hierarchy as necessary:
1. A name() function that returns a string for all classes.
2. A volume() function that returns the volume of the solid.
3. A surface_area() function that returns the surface_area of the solid.
The Circular class should have a data member that represents a radius.
The Rectangular class should have three data members, representing length, width, and height.
Input data for this assignment will be provided in a 50 record randomly ordered text file. The format for this file will be a two-byte character code (“cy”, “sp”, “re”, or “cu”) indicating the type of solid, followed by 1, 2, or 3 doubles representing requisite data members of the class. Specifically the data format will appear as:
GeometricSolid Code # of doubles Representing
Cylinder cy 2 radius and height
Sphere sp 1 radius
RetangularPrism re 3 length, width, and height
Cube cu 1 side
Input file: (http://voyager.deanza.edu/~bentley/ass10data.txt)
Program needs to perform the following tasks:
1. Read in the data file, correctly processing each particular type of GeometricSolid.
2. Using a pointer to a GeometricSolid, and making use of polymorphism, allocate memory dynamically for a specific type of GeometricSolid. Using polymorphism print the type, surface area and volume for each record.
3. The output should be formatted in fixed notation with 4 decimal place accuracy. The output should match that which is partially displayed below.
Program Output
Required main()
sphere 834.8106 2268.0663
cylinder 56.3300 16.9848
cube 9416.0418 62169.2591
rectangular prism 158.9869 33.8483
sphere 21.1164 9.1244
sphere 173.7657 215.3873
rectangular prism 296.4794 294.3741
cylinder 2120.9347 7015.5794
... cylinder Circular Geometric Solid Sphere Rectangular RectangularPrism Cube
Explanation / Answer
#include<conio.h>
#include<iostream.h>
//base class
Class Cylinder
{
Public:
Int red,high;
Float volu,area;
Int volume(int r,int h)
{
Red=r;
High=h;
volu=3.14f*r*r*h;
}
}
Void surface1(int r1,int h1)
{
Red=r1;
High=h1;
area=2*3.14f*r1(h1+r1);
}
}
Class sphere
{
Public:
Int rad;
Float v,a;
Public:
//member functions
Void volume2(int r)
{
Rad=r;
V=4/3.3.14*r*r*r;
}
}
Void surface2(int r1)
{
rad=r1;
a=4*3.14*r1*r1;
}
}
Class Cube
{
Public:
Int side;
Float v,a;
Public:
//member functions
Void volume3(int s)
{
Side=s;
V=s*s*s;
}
}
Void surface3()
{
Side=s1;
a=6*s1*s1;
}
Class RectangularPrism:public cube
{
Public:
int len,wid.hig;
Float v,a;
//member functions
Ofstream f3;
F3.open("code", ios::out);
Void volume4(int l,int w,int h)
{
Volume3();
Len=l;
Wid=w;
Hig=h;
V=l*w*h;
}
}
Void surface4(int l1,int w1;int h1)
{
Surface3();
Len=l1;
Wid=w1;
Hig=h1;
a=2(l1w1+l1h1+w1h1);
}
Class Rectangular
{
Rec *ptrsec;
String name;
Public:
//member functions
String name(string nam1)
{
Name=nam1;
}
Ofstream f2;
F2.open("code", ios::out);
Void volume()
{
For(unsingned i=0;i<50;i++)
{
If(!strcmp(code,”re”))
{
Fin>>l>>w>>h>>;
Ptrrec=new RectangularPrism(l,w,h);
Else
{
Fin>>s;
Ptrrec =new cube(s);
}
}
Void surface()
{
For(unsingned i=0;i<50;i++)
{
If(!strcmp(code,”re”))
{
Fin>>l1;w1,h1;
Ptrrec= new RengularPrime(l1,w1,h1);
}
Else
{
Fin>>s1;
Ptrrec=new Cube(s1);
}
Cout<<left<<ptrrec->name()<<”RetangularPrime”;
Cout<<left<<ptrrec->name()<<”Cube”;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.