This assignment will give you practice with inheritance and polymorphism.
- Create an abstract Solid base class. It should consist of:
- data members to represent the (x,y,z) coordinates of a solid in 3D space
- at least one constructor
- a function that displays the coordinates of a Solid object as (x,y,z)
- pure virtual functions that: return the volume of the object
- a non-virtual function that prints all information about a Solid object (it should call the 5 functions listed above)
- Derive from the Solid class the following classes:
- RectangularSolid This class should have three members: length, width, and height.
- Sphere This class should have one member, radius.
- Cylinder This class should have two data members, radius and height.
- Cone This class should have two data members, radius and height.
- From the RectangularSolid class, derive a Cube class. It does not have any data members. It only needs a constructor, a type function, and a "print details" function.
return the surface area of the object
return the type of the solid
print "specialized" details about a Solid object (i.e. radius, height, width, …)
Use the following main() as a final test of your program:
int main() {
RectangularSolid Rec(1.,2.,3.,4.,5.,6.);
Sphere Sph(1.,2.,3.,4.);
Cylinder Cyl(1.,2.,3.,4.,5.);
Cone Con(1.,2.,3.,4.,5.);
Cube Cub(1.,2.,3.,4.);
Solid* ps;
// Rectangular Solid test
ps = &Rec;
ps->print();
// Sphere test
ps = &Sph;
ps->print();
// Cylinder test
ps = &Cyl ;
ps->print();
// Cone test
ps = &Con ;
ps->print();
// Cube test
ps = &Cub ;
ps->print();
return 0;
}
****** Program Output ******
I am a rectangular solid located at (1,2,3)
length=4 width=5 height=6
volume=120 surface area=148
I am a sphere located at (1,2,3)
radius=4
volume=268.082 surface area=???
I am a cylinder located at (1,2,3)
radius=4 height=5
volume=??? surface area=???
I am a cone located at (1,2,3)
radius=4 height=5
volume=??? surface area=???
I am a cube located at (1,2,3)
side=4
volume=64 surface area=???

沒有留言:
張貼留言