8.1 What does inheritance mean in C++? Answer: 1. Inheritance is a

advertisement
8.1 What does inheritance mean in C++?
Answer:
1. Inheritance is a way to establish Is-a relationship between objects
2. Inheritance is a mechanism of reusing and extending existing classes without modifying
them, thus producing hierarchical relationships between them.
3. When one class inherits from another, the derived class inherits the variables and functions
of the base class. These variables and functions become part of the derived class.
4. When deriving a class from a base class, the base class may be inherited through public,
protected or private inheritance.
8.5 How do the properties of the following two derived classes differ?
(a) Class D1: private B {//…..}
(b) Class D2: public B {//…...}
Answers:
Same point:
Class D1 and Class D2 inherited from class B
Different point:
Different inherited way
1. Private Inheritance: When deriving from a private base class, public and protected
members of the base class become private members of the derived class.
2. Public Inheritance: When deriving a class from a public base class, public members of the
base class become public members of the derived class and protected members of the base class
become protected members of the derived class.
8.10 When do we make a class virtual?
Answer:
1. Virtual classes are inner classes of another outer class, which behave like virtual function.
This means they can be overridden in a subclass of the outer class, and the run time type of a
virtual class depends on the run time type of an object of the outer class. (Just like the run time
type of an object decides which virtual function should be used.)
2. Whenever a derived class requires direct inheritance from a base class, even if it inherits
that base class indirectly. This feature is commonly used in conjunction with multiple
inheritances.
That is, if V is a base class from which B is derived, and D is derived from B, then D inherits
from V indirectly (through B). But if B is virtually derived from V, then D will inherit directly from
V.
9.2 How is polymorphism achieved at (a) compile time, and (b) run time?
Answer:
1. Polymorphism is defined as one interface to control access to a general class of actions.
There are two types of polymorphism one is compile time polymorphism and the other is run
time polymorphism.
2. Polymorphism means that functions assume different forms at different times. In case of
compile time it is called function overloading
3. Compile time: Polymorphism is functions and operators overloading.
Runtime time: Polymorphism is done using inheritance and virtual functions
9.4 Explain, with an example, how you would create space for an array of objects using pointers.
Answer:
1. The simplest way is to allocate raw memory of the required length using
a pointer-to-pointer to the class of object. Once you have the memory, you can access the
individual elements just as you would any other type of array, to either instantiate new objects or
to point to existing objects.
2. The following example uses the class itself to allocate a dynamic array of a given size, via a
static member function.
/ / Declare a simple class with a default constructor, one member variable and a static
member function.
class MyClass
{
public:
MyClass():m_int(0){} // Default constructor initialises member variable.
static MyClass** CreateArray(unsigned int count); // Static member function.
private:
int m_int; // Member variable.
};
int main()
{
int i = 0;
MyClass** ppArray;
// Instantiate objects in a fixed-size array (uses default constructor).
MyClass Array[10];
// Instantiate a dynamic array of objects (and check for NULL).
if( ppArray = MyClass::CreateArray( 10 ))
{
for( i=0; i<10; ++i )
// Point array elements to the existing objects.
ppArray[i] = &Array[i]; // Any existing object will do here.
// Finished with dynamic array (does NOT destroy the existing objects).
delete( ppArray );
ppArray = NULL;
}
// Instantiate a new dynamic array (and check for NULL).
if( ppArray = MyClass::CreateArray( 5 ))
// Instantiate new objects via default constructor.
for( i=0; i<5; ++i )
ppArray[i] = new MyClass();
// Note: it's worth checking each element is not NULL before accessing it!
// Destroy each object that was created.
for( int i=0; i<5; ++i ) {
delete( ppArray[i] );
ppArray[i] = NULL;
}
// Finished with dynamic array.
delete( ppArray );
ppArray = NULL;
return( 0 ); // Array[10] will now fall from scope
}
9.8 Why do we need virtual functions?
Answer:
1. A virtual function or virtual method is a function or method whose behavior can be
overridden within an inheriting class by a function with the same signature.
2. When a derived class inherits from a base class, an object of the derived class may be
referred to via a pointer or reference of either the base class type or the derived class type. If
there are base class methods overridden by the derived class, the method actually called by such
a reference or pointer can be bound either 'early' (by the compiler), according to the declared
type of the pointer or reference, or 'late' (i.e. by the runtime system of the language), according
to the actual type of the object referred to.
3. Virtual functions are resolved 'late'. If the function in question is 'virtual' in the base class,
the most-derived class's implementation of the function is called according to the actual type of
the object referred to, regardless of the declared type of the pointer or reference. If it is not
'virtual', the method is resolved 'early' and the function called is selected according to the
declared type of the pointer or reference.
4. Virtual functions allow a program to call methods that don't necessarily even exist at the
moment the code is compiled.
Download