Create a base class called SHAPE. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called TRIANGLE and RECTANGLE from the base SHAPE. Add to the base class, a member function get_data() to initialize the base class data members and another member function display_area() to compute and display the area of the figures. Make display_area() as a virtual function and redefine this function in the derived classes as per the requirements. Using the three classes, design a program that will accept dimensions of a triangle or rectangle interactively and display the area. The two values given, as input will be treated as lengths of the two sides in the case of a rectangle and as base and height in the case of triangles.
Area of a rectangle = side1 * side2
Area of a triangle = ½ * side1 * side2
C++ program i try to solve this but i didnt get currect answers.?
your pointer "sh" doesn't point at your instance of triangle or rectangle, after your switch
also...
rename triangle::triangle_area as area
also
rename rectangle::rec_area as area
hmmm,
if I were you, I'd put some default constructors in there too, and a copy +or
ditch the base::area function, generic shapes won't have areas, you're assuming a generic shape is a rectangle, when I say ditch I mean, make it a pure virtual function, (double shape::area( void ) = 0) so it 'has' to be overridden
add yourself a virtual base class destructor too - virtual ~shape;
and...
make your +ors, call a private function called: (imho) void defaultData(void), which initializes your data etc. so you don't have rogue values floating about
survey results
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment