



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




After completing this lab, you should:
Inheritance is a key aspect of object-oriented languages that facilitates code reuse. Using inheri- tance, we are able to easily allow some derived class Y to inherit the functionality of a base class X, and at the same time add to and replace the base class’s interface and behavior in the derived class. If class Y inherits its base functionality from class X, we say that Y “is-a” X.
This “is-a” relationship allows us to work with instances of derived classes as though they were instances of the base class — in other words, we may assign references to derived class objects to base class pointers and invoke any functions on derived class objects that are defined in the interface for the base class. Since the derived class may modify or add to the base class definition, the behavior that results from such interaction should be based on the actual object type with which we are interacting. This powerful feature of inheritance, termed polymorphism, is another key aspect of object-oriented programming languages.
Using the following set of classes, this lab will have you experiment with the inheritance mechanism supported by C++. You will compare inheritance to the simpler form of code reuse that we have used thus far, and will modify the classes to implement and test various forms of inheritance and ensure that, when needed, polymorphism is achieved.
class Shape { private: int ID; public: Shape (int myID=0) : ID(myID) { }
float area () const { return 0.0; } int getID () const { return ID; } void setID (int newID) { ID = newID; } }
class Circle { private: float radius; public: Circle (int myID, float myRadius); float area () const { return radius * radius * 3.1415; }; };
class Square { private: float width; public: Square (int myID, float width); float area () const { return width * width; } };
You may download header files for the above Shape, Circle, and Square classes, and all source code shown in the exercises below from the course website.
Composition is another form of code reuse that we have had some experience using. We say that a class Y which contains one or more instance(s) of class X in its attribute list is composed of objects of class X. Composition implements a “has-a” relationship, as an object of class Y that contains an object of X has an X.
Whether using composition or inheritance to reuse code, it is always necessary to ensure that the contained/base class’s attributes are properly initialized before using them. How do we perform this initialization in either case?
Solution:
Solution:
On occasion we are unsure of the data type that will be obtained from a base class container — it is then unsafe to use a regular explicit cast to convert from the base class to a derived class. The C++ dynamic cast provides a type-safe downcast that only returns a pointer to the desired type if the cast is proper — otherwise, zero is returned.
For example, in the following code segment, the dynamic cast fails, and circumference is not invoked on the returned pointer:
Shape *shapePtr; Circle circlePtr; Square sq1(1, 1.0); shapePtr = &sq1; circlePtr = dynamic_cast<Circle>(shapePtr); if (circlePtr) cout << "Circle’s circumference: " << circlePtr->circumference();
Note: In order to enable dynamic casting in C++, it is necessary to enable runtime type informa- tion — to do this, access Project->Settings in MSVC, go to the “C++ Language” category in the “C/C++” tab and click “Enable Run-Time Type Information (RTTI)”.
Replace the dynamic cast in the above listing with a regular explicit downcast — what happens? Use your current knowledge of how inheritance is implemented to try and explain the result.
Solution:
Modify your array implementation of the list ADT from lab 3 in your lab manual so that it holds an array of Shape objects by replacing the declaration of DataType in listarr.h with:
typedef Shape DataType
You will also need to make the following changes to your list implementation:
The following test file, available as test2.cpp, attempts to insert a number of different shape objects into the list of shapes and retrieves them in order to calculate the total area of all the shapes in the list.
void main () { float totalArea = 0.0; List shapes(5); Circle c1(0, 1.0), c2(1, 2.0); Square s1(2, 1.5), s2(3, 2.5); shapes.insert(c1); shapes.insert(c2); shapes.insert(s1); shapes.insert(s2); shapes.gotoBeginning(); do { totalArea += shapes.getCursor().area(); } while (shapes.gotoNext()); cout << "Total area: " << totalArea << endl; }
Compile and execute the above program. Notice that, despite making the area function in the Shape class virtual, we are still not getting the desired results. Explain why this is the case, and discuss what might be done to fix the problem.
Solution: