CS 325 HW3: Thread Safety, Polymorphism, Overriding, Overloading, Virtual Destructors, Assignments of Computer Science

The third homework assignment for cs 325 course. It covers various topics including thread safety, polymorphism, overriding, overloading, virtual destructors, and virtual pointer table. Students are required to answer detailed questions on these topics.

Typology: Assignments

Pre 2010

Uploaded on 02/25/2010

koofers-user-oi0
koofers-user-oi0 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework #3
CS 325
Due: 9:30 am on Tuesday 17-Feb
Answer the following questions in detail:
1. What does it mean for a function to be thread-safe?
More than one thread can correctly run the function simultaneously thus
no locking, racing, etc.
2. Suppose class W inherits from class X which inherits from Y and this
inherits from the base-class B0?
Consider the following declarations:
virtual void B0::basicFunction()
void Y::basicFunction()
virtual void X::basicFunction()
void W::basicFunction()
then describe the polymorphic behavior of the pointers:
B0 *b0_ptr;
Y *y_ptr;
X *x_ptr;
W *w_ptr;
3. What is the difference between overriding and overloading?
Overriding a member function replaces a function with the same name through
inheritance: the most recent inherited class may redefine a member function with
the same name.
Overloading a function allows several functions with different signatures to have
the same name.
4. Why do we use virtual destructors and how do they ensure
everything is destructed?
Destructors are always invoked in the opposite order of the constructors. Thus,
making a destructor virtual ensures the destructor associated with the pointer is
pf2

Partial preview of the text

Download CS 325 HW3: Thread Safety, Polymorphism, Overriding, Overloading, Virtual Destructors and more Assignments Computer Science in PDF only on Docsity!

Homework # CS 325 Due: 9:30 am on Tuesday 17-Feb

Answer the following questions in detail:

  1. What does it mean for a function to be thread-safe****?

More than one thread can correctly run the function simultaneously – thus no locking, racing, etc.

  1. Suppose class W inherits from class X which inherits from Y and this inherits from the base-class B0?

Consider the following declarations:

virtual void B0::basicFunction() void Y::basicFunction() virtual void X::basicFunction() void W::basicFunction()

then describe the polymorphic behavior of the pointers:

B0 *b0_ptr; Y *y_ptr; X *x_ptr; W *w_ptr;

  1. What is the difference between overriding and overloading?

Overriding a member function replaces a function with the same name through inheritance: the most recent inherited class may redefine a member function with the same name.

Overloading a function allows several functions with different signatures to have the same name.

  1. Why do we use virtual destructors and how do they ensure everything is destructed?

Destructors are always invoked in the opposite order of the constructors. Thus, making a destructor virtual ensures the destructor associated with the pointer is

invoked which in turn invokes the rest of the destructors in the inverse order as the constructors.

  1. Give a detailed description of the virtual-pointer table, say v_ptr, and how it implements polymorphism.

Each class with a virtual member function (or inheriting virtually) has a virtual pointer table v_ptr. Thus all classes that inherit from a class with a v_ptr table access this table. More later…