Você está na página 1de 6

ANGEL COLLEGE OF ENGINEERING AND TECHNOLOGY

TIRUPUR-641665
Approved by AICTE, New Delhi and Affiliated to Anna University, Chennai
An ISO 9001:2008 Certified Institution

EC6301- OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES


TWO MARKS WITH ANSWERS

Unit-II
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. List the types of inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
3. Define Polymorphism.
Polymorphism is an important concept of OOPs. Polymorphism means one name,
multiple forms. It is the ability of a function or operator to take more than one form at
different instances.
4. List and define the two types of Polymorphism.
Operator Overloading The process of making an operator to exhibit different
behaviors at different instances.
Function Overloading Using a single function name to perform different types of
tasks. The same function name can be used to handle different number and different types
of arguments.
5. What are the types of type conversions?
There are three types of conversions. They are
Conversion from basic type to class type done using constructor
Conversion from class type to basic type done using a casting operator
Conversion from one class type to another done using constructor or casting operator

6. What are the conditions should a casting operator satisfy?


The conditions that a casting operator should satisfy are,
It must be a class member.
It must not specify a return type.
It must not have any arguments.
7. Define constructor overloading.
A class can have multiple constructors. All the constructors have the same name as their
class and they differ only in terms of their signature i.e in terms of the number of
arguments, or data types of their arguments or both. This is called constructor
overloading.
Eg:
class integer
{
int m,n;
public:
integer( ) //default constructor
{ m=0;n=0; }
integer(int a,int b) //parameterized constructor
{ m=a; n=b; }
integer(&i) //copy constructor
{ m=i. m;
n=i.n; }
void main()
{
integer i1; //invokes default constructor
integer i2(45,67);//invokes parameterized constructor integer i3(i2); //invokes copy
constructor
}
8. What are the advantages of using inheritance?
Advantages of using inheritance
1. We are in need of extending the functionality of an existing class.

2. We have multiple classes with some attributes common to them. We would like to
avoid problems of inconsistencies between the common attributes.
3. We would like to model a real world hierarchy in our program in a natural way.
9. Define virtual function?
When a function is made as virtual, c++ determines which function o use at run time
based on the type of object pointed by the base pointer, rather than the type of the pointer.
Thus by making the base pointer to point to different objects we can execute different
versions of virtual function.
10. List the access specifier in c++.
C++ contains three access specifiers. Private, Protected and public.
Private members can be accessed only within the declared class.
Protected members can be accessed within the declared class and the immediate
derived class.
Public members can be accessed any where inside the program.
11. What is meant by pure virtual function?
A virtual function that is declared in a base class but not defined there. The
responsibility for defining the function falls on the derived classes, each of which
generally provides different definitions.
It is illegal to create instances of a class that declares a pure virtual function. So
such a class is necessarily an abstract base class.
12. How to define derived classes?
A derived class can be defined by specifying its relational ship with the base class in
addition to its own details.
The syntax is:
class derived-class-name : visibility-mode base-class-name
{

.
};
Here the visibility mode is optional and if present, may be either private or public. The
default mode is private.
When a base class is privately inherited by a derived class, public members of the base
class become private members of the derived class and therefore the public members of
the base class can only be accessed by the member functions of the derived class.

When the base class is publicly inherited, public members of the base class become
public members of the derived class and therefore they are accessible to the objects of the
derived class.
13. What is dynamic binding or late binding?
Binding refers to the linking of a procedure to the code to be executed in response to the
call. Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at the run-time.
14. Define abstract class
Abstract class is one that is not used to create objects. An abstract class is designed only
to act as a base class. It is a design concept in program development and provides a base
upon which other classes may be built.
The general form is
abstract class classname
{
Class definition;
}
15. Define virtual destructor
The virtual destructor will call explicitly the most derived run time destructor of class so
that it will be able to clear the object in a proper way.
Example
class Base
{
public:
Base(){ cout<<"Constructing Base";}
// this is a virtual destructor:
virtual ~Base(){ cout<<"Destroying Base";}
};
16. What is a concrete class?
An abstract class is one which defines an interface, but does not necessarily provide
implementations for all its member functions. An abstract class is meant to be used as the

base class from which other classes are derived. The derived class is expected to provide
implementations for the member functions that are not implemented in the base class. A
derived class that implements all the missing functionality is called a concrete class.
17. What is the need to declare a base class as virtual?
When a declared class need to redefine the method of the base class, then the method
of the base class is declared as virtual
When a derived class is derived from two classes and these two classes are themselves
derived from one base class, then ambiguity may occur while using the member functions
of parent classes because two copies of the parent classes are included in the derived
class. To avoid this problem the base class is declared as virtual.
18. Define composition and inheritance
Inheritance means deriving a new class from a base class which makes use of some or
all the properties of base class. Inheritance represents a is-a relationship
Composition means a class contains the object of another class so that the derived
class can use the functionalities of the container class. Composition represents the has-a
relationship.
19. State the rules for using virtual fuction.
Must be a member of some class
Cannot be static members
Accessed by using object pointers
Can be a friend of another class
Cannot have virtual constructors but can have virtual destructors
If a virtual function is defined in the base class, it need not be necessarily redefined in
the derived class. In such cases, calls will invoke the base function.
20. What is the use of this pointer?
This pointer represents an object that invokes a member function. The this pointer
points to the object for which the function was called.
Example
Class test
{
int num;
public:
void get(int num)
{

this->num=num;
}
}

Você também pode gostar