Anda di halaman 1dari 8

OBJECT ORIENTED PROGRAMMING

Unit IV
Inheritance – public, private, and protected derivations – multiple
inheritance – virtual base class – abstract class – composite objects Runtime
polymorphism – virtual functions – pure virtual functions – RTTI – typeid –
dynamic casting – RTTI and templates – cross casting – down casting .

2 MARKS

1. State Inheritance.

Inheritance is the process by which objects of one class acquire the

properties of objects of another class. It supports the concept of hierarchical

classification and provides the idea of reusability. The class which is inherited is

known as the base or super class and class which is newly derived is known as

the derived or sub class.

2. What are the types of inheritance?

The various types of inheritance are,

· Single inheritance

· Multi-level inheritance

· Multiple inheritance

· Hierarchical inheritance

· Hybrid inheritance

3. Give the syntax for inheritance.

The syntax of deriving a new class from an already existing class is given

by,

class derived-class : visibility-mode base-class

body of derived class

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
4. Define single inheritance.

In single inheritance, one class is derived from an already existing base

class.

Here A is the base class and B is the derived class.

5. Define multi-level inheritance.

In multi-level inheritance, a new class is derived from a class already

derived from the base class.

Here, class B is derived from class A and class C is further derived from

the derived class B.

6. Define multiple inheritance.

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
In multiple inheritance, a single class is derived from more than one base

class.

Here class C is derived from

two base classes A and B.

7. Define Hierarchical inheritance.

In hierarchical inheritance, more than one class is derived from a single

base class.

Here class B and C are derived from class A.

8. Define Hybrid inheritance.

Hybrid inheritance is defined as a combination of more than one

inheritance.

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
Here, Classes A, B and C represent hierarchical inheritance. Classes A, B &

D and classes A, C and D represent multilevel inheritance. Classes B, C and D

represent multiple inheritance.

9. What is a virtual base class?

Here, class D inherits both classes B and C which

are derived from the same base class A. Hence D has

two copies of the properties of class A. This can be avoided by declaring classes B

and C as virtual base classes.

10. What is meant by Abstract base class?

A class that serves only as a base class from which derived classes are

derived. No objects of an abstract base class are created. A base class that

contains pure virtual function is an abstract base class.

11. What are the types of polymorphism?

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
The two types of polymorphism are,

· Compile time polymorphism – The compiler selects the appropriate

function for a particular call at the compile time itself. It can be achieved

by function overloading and operator overloading.

· Run time Polymorphism - The compiler selects the appropriate function

for a particular call at the run time only. It can be achieved using virtual

functions.

12. Define ‘this’ pointer.

A ‘this’ pointer refers to an object that currently invokes a member

function. For e.g., the function call a. show () will set the pointer ‘this’ to the

address of the object ‘a’.

13. What is a virtual function?

When a function is declared as virtual, C++ determines which function to

use at run time based on the type of object pointed to by the base pointer, rather

than the type of the pointer.

14. What are pure virtual functions? Write the syntax.

A pure virtual function is a function declared in a base class that has no

definition relative to the base class. In such cases, the compiler requires each

derived class to either define the function or redeclare it as a pure virtual

function. A class containing pure virtual functions cannot be used to declare any

object of its own. It is also known as “do nothing” function.

The “do-nothing” function is defined as follows: virtual void display ( ) =0;

15. Write some of the basic rules for virtual functions

· Virtual functions must be member of some class.

· They cannot be static members and they are accessed by using object

pointers

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
· Virtual function in a base class must be defined.

· Prototypes of base class version of a virtual function and all the derived

class

versions must be identical.

· If a virtual function is defined in the base class, it need not be redefined in

the derived class.

15. How can a private member be made inheritable?

A private member can be made inheritable by declaring the data members

as protected. When declared as protected the data members can be inherited by

the friend classes.

16. What is RTTI?

Run-time type information (RTTI) is a mechanism that allows the type of

an object to be determined during program execution.

17. What is typeid in RTTI?

The typeid operator provides a program with the ability to retrieve the

actual derived type of the object referred to by a pointer or a reference. This

operator, along with the dynamic cast operator, is provided for runtime type

identification (RTTI) support in C++.

Example:

typeid (a).name (); will return the type of variable a.

18. What is Dynamic casting?

The dynamic cast operator performs type conversions at run time. The

dynamic cast operator guarantees the conversion of a pointer to a base class to a

pointer to a derived class, or the conversion of an lvalue referring to a base class

to a reference to a derived class. A program can thereby use a class hierarchy

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
safely. This operator and the typeid operator provide runtime type information

(RTTI) support in C++.

The expression dynamic cast<T>(v) converts the expression v to type T.

Type T must be a pointer or reference to a complete class type or a pointer to

void. If T is a pointer and the dynamic cast operator fails, the operator returns a

null pointer of type T. If T is a reference and the dynamic cast operator fails, the

operator throws the exception std::bad_cast. You can find this class in the

standard library header <typeinfo>.

The dynamic cast operator requires runtime type information (RTTI) to be

generated, which must be explicitly specified at compile time through a compiler

option.

19. What is down casting?

Down casting is the term used in C++ for casting a pointer or reference to a

base class to a derived class. C++ provides a special explicit cast called dynamic

cast that performs this conversion. Down casting is the opposite of the basic

object-oriented rule, which states objects of a derived class, can always be

assigned to variables of a base class.

20. What is cross casting?

Cross casting only occurs when using multiple inheritances. Here a type

can be derived from two or more base classes. A pointer or reference to such a

type can be passed around as a pointer or reference to any of these base types. A

cross cast is where we have a pointer or reference to an object as one of its bases

but we require it to be a derivative of one of the other bases. Converting from

one base to another is a cross cast, because we are going across the inheritance

graph from one base type to another.

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women
12 MARKS

1. Explain various type of inheritance in detail.

2. Explain about virtual functions in detail.

3. Explain through an example program about pure virtual functions.

4. Write a C++ program for composite objects and Explain?

5. Explain run time polymorphism in detail.

6. (a) Write a C++ program for virtual functions?

(b) Write a C++ program for pure virtual functions?

7. What is RTTI? Explain about typeid and dynamic casting in RTTI?

(Or)

(a) Write a C++ program for typeid in RTTI?

(b) Write a C++ program for dynamic casting in RTTI?

8. Explain the needs of RTTI in run time polymorphism?

9. (a) Write a C++ program for RTTI and templates for typeid?

(b) Write a C++ program for RTTI and templates for dynamic casting?

10. Elaborate cross casting and down casting in run time polymorphism?

**********

( Word to PDF Converter - Unregistered )


http://www.Word-to-PDF-Converter.netPrepared By:
Mrs.J.NirmalaDevi, HOD/IT,Excel College of Engineering for Women

Anda mungkin juga menyukai