Subject Name: OBJECT ORIENTED PROGRAMMING DESIGN
Subject Code: CS T33
2 MARKS AND 11 MARKS
UNIT III
Pointers and arrays: Pointer to class and object – pointer to derived classes and base classes –
accessing private members with pointers – address of object and void pointers –characteristics
of arrays – array of classes.
Memory: Memory models – The new and delete operators – Heap consumption – Overloading
new and delete opetaors – Execution sequence of constructors and destructors – specifying
address of an object – dynamic objects.
Binding, Polymorphism and Virtual Functions: Binding in C++ – Pointer to derived class
objects – virtual functions – Array of pointers – Abstract classes – Virtual functions in derived
classes – constructors and virtual functions – virtual destructors – desctructos and virtual
functions.Strings - Declaring and initializing string objects – relational operators – Handling
string objects – String attributes – Accessing elements of strings – comparing and exchanging
and Miscellaneous functions.
PART A – 2 MARK QUESTIONS
1. What is meant by dynamic binding or late binding?
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.
2. What are abstract classes?
Classes containing at least one pure virtual function become abstract classes. Classes
inheriting abstract classes must redefine the pure virtual functions; otherwise the
derived classes also will become abstract. Abstract classes cannot be instantiated.
,CS T33 OBJECT ORIENTED PROGRAMMING AND DESIGN Y2/S3
3. Define virtual function.
Virtual functions allow programmers to declare functions in a baseclass, which can be
defined in each derived class. A pointer to an object of a base class can also point to
the objects of its derived class.
4. Define Pure Virtual Function.
Pure virtual function is declared as a function with its declaration followed by = 0.
Virtual functions are defined with a null body. It has no definition. These are similar
to do nothing or dummy function
5. What are the properties of pure virtual function?
* A pure virtual function has no implementation in the base class.
* A class with pure virtual function cannot be instantiated.
* It acts like an empty bucket that the derived class is supposed to fill.
* A pure virtual member function can be invoked by its derived class.
6. What is the syntax used for writing pure virtual function?
classmyclass
{
public:
virtualreturntypefunctionname(args) = 0 ;
}
7. Define polymorphism.
Polymorphism means the ability to take more than one form. That means it performs
more than one operation by using a single operator. Polymorphism is implemented
using the overloaded functions and operators.
8. Define pointer to an object.
Pointer to an object is a variable containing an address of an object. It is similar to a
pointer to any other variable.
Syntax:
Classname *ptr-to-object;
Ptr-to-object = &object;
Ptr-to-object = new classname;
9. Define this pointer.
,CS T33 OBJECT ORIENTED PROGRAMMING AND DESIGN Y2/S3
This pointer is a pointer to an object invoked by the member function. Thus member
function of every object has access to a pointer named “this”, which points to the
object itself.
10. Define pointers to member
It is possible to take the address of a member of a class and assign it to a pointer. The
address of a member can be obtained by applying the operator &to a “fully qualified”
class member name. A class member pointer can be declared using the
operator::*with the class name.
Eg: class A
{
int m;
public:
void show( );
};
pointer to member m is defined as
int A::*ip=&A::m;
A::*->pointer to member of A class
&A::m->address of the m member of A class
11. What are the types of polymorphism?
Basic Types
i)Runtime polymorphism
ii)Compile time polymorphism
Major Types
a.Run-time polymorphism
b.Compile time polymorphism
c.ad-hoc polymorphism
d.Parametric polymorphism
e.Virtual functions
f.Function name overloading
g.Operator overloading
12. What is compile time polymorphism?
, CS T33 OBJECT ORIENTED PROGRAMMING AND DESIGN Y2/S3
The overloaded member functions are selected for invoking by matching arguments
both type and number.This information is known to the compiler at the compile time
and therefore compiler is able to select the appropriate function for a particular call at
the compile time itself.This is called early binding or static binding or static
linking.Also known as compile time polymorphism.
13. List the features of pointers.
Pointers save the memory space
Execution time with pointer is faster because data is manipulated with the
address.
The memory is accessed efficiently with the pointers. The pointer assigns the
memory space dynamically that leads to reserve specific bytes in the memory.
Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
14. Define array.
Array is a collection of elements of similar data types in which each element is
unique and located in separate memory locations.Declaration of an array is given
below:
int a[5];
it tells the compiler that ‘a’ is an integer type of array and it must store five integers.
15. Write the characteristics of arrays.
The declaration inta[5] is creation of five variables of integer type in memory.
Instead of declaring five variables for five values, the programmer can define
them in an array.
All the elements of an array share the same name, and they are distinguished from
one another with the help of element number.
The element number in an array plays major role in calling each element.
Any particular element of an array can be modified separately without disturbing
other elements.
Any element of an array a[] can be assigned/equated to another ordinary variable
or array variable of its type.
16. What is void pointer?
Pointers can also be declared as void type. Void pointers cannot be dereferenced
without explicit type conversion. This is because being void the compiler cannot
determine the size of the object the pointer points to.
17. What is the advantage of using dynamic initialization?