Hey guys. This is an assignment I've been given by my professor for a C++ course. The assignment description is below:

Create a class ThreeDShape that has a default constructor and pure virtual functions double getSurfaceArea(), double getVolume() and void print(). Function getSurfaceArea() should compute and return the surface area of a three dimensional shape. Function getVolume() should compute and return the volume of a three dimensional shape. Function print() should displays the values of all member variables, the surface area and the volume. The class should also declare a public static constant of type double, PI, with a value of 3.14159265358979. The code for this class and the derived classes should be place in namespace ThreeDimensionalShapes.

Create a derived class of ThreeDShape named Sphere which has the following:
• Private member variable radius.
• A one-parameter constructor that sets the radius. The radius must be greater than 0.
• Appropriate accessor and mutator functions for the radius.
• Implementations of the pure virtual functions inherited form class ThreeDShape.

Create a second class derived from ThreeDShape named Cylinder which has the following:
• Private member variables for the radius and the height of the cylinder.
• A two-parameter constructor that sets the height and the radius of the cylinder. Both the radius and the height must be greater than 0.
• Appropriate accessor and mutator functions for the member variables.
• Implementations of the virtual functions inherited from class ThreeDShape.

Formulas for computing the surface areas and volumes are given below:
Surface Area Volume
Cylinder 2*PI * r * (r + h) PI * pow(r,2) * h where r is radius ; h is height
Sphere: 4* PI*pow(r,2) 4.0/3.0* PI * pow(r, 3) where r is radius

Write a driver program that creates a sphere and a cylinder and then calls the print() function to display information about them. Each class that you create should be separated into an interface (header) file and an implementation file.

// END of description

The parts I don't understand how to code are the virtual functions. Also how would I place the classes in a new namespace called ThreeDimensionalShapes? The other thing I am not clear on is what is meant by "Each class that you create should be separated into an interface (header) file and an implementation file." Would the implementation file be a standard .cpp file? What then should go into the header file? Any help would be very much appreciated!

Recommended Answers

All 3 Replies

for virtual functions http://www.codersource.net/c/c-tutorials/c-virtual-function.aspx

as for splitting the files the .h function should have the deceleration of the class and the .cpp should have the definitions of all the class function. something like this

//foo.h

class Foo
{
public:
       Foo();
       ~Foo() {}
private:
       int bar;
};

// foo.cpp
Foo::Foo()
{
       bar = 0;
}

Excellent, I will read up on the virtual functions and thank you for the info on what should go in a header file.

no problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.