Subject Name: OBJECT ORIENTED PROGRAMMING DESIGN
Subject Code: CS T33
2 MARKS AND 11 MARKS FOR ALL UNITS
UNIT II
Constructors and Destructors: Purpose of Constructors and Destructors – overloading
constructors – constructors with default arguments – copy constructors – calling constructors and
destructors – dynamic initialization using constructors – recursive constructor.
Overloading Functions: Overloading unary operators – constraint on increment and decrement
operators – overloading binary operators – overloading with friend functions – type conversion –
one argument constructor and operator function – overloading stream operators.
Inheritance: Introduction – Types of Inheritance – Virtual base classes – constructors and
destructors and inheritance – abstract classes – qualifier classes and inheritance – common
constructor – pointers and inheritance – overloading member function.
PART A – 2 MARK QUESTIONS
1. Define constructor
A constructor is a special member function whose task is to initialize the objects of its
class. It is special because its name is same as class name. The constructor is invoked
whenever an object of its associated class is created. It is called constructor because it
constructs the values of data members of the class
Eg:
integer Class
{
,Object Oriented Programming Design CST33 III –Semester
……
public:
integer( );//constructo r
………
}
2. What is meant by operator overloading?
C++ has the ability to provide the operators with a special meaning for a data type. This
mechanism of giving such special meanings to an operator is known as Operator
overloading. It provides a flexible option for the creation of new definitions for C++
operators.
3. What are the operators that cannot be overloaded?
Class member access operator (. , .*)
Scope resolution operator (::)
Size operator ( sizeof )
Conditional operator (?:)
4. Define 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. For example the
bird ‘Robin’
5. What is meant by multilevel inheritance?
If a class is derived from a class, which in turn is derived from another class, is called
multilevel inheritance. This process can be extended to any number of levels.
Eg:
Base class Grand father
Intermediate
Base class Father
Derived class Child
6. List out the types of inheritance.
Single Inheritance
Multiple Inheritance
,Object Oriented Programming Design CST33 III –Semester
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Multi-path Inheritance
7. What is Function Overloading?
A function with the same name performing different operations is called function
overloading. To achieve function overloading, functions should be declared with the
same name but different number and type of arguments.
8. List some of the special characteristics of constructor.
• Constructors should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types
• They cannot be inherited.
9. Give the various types of constructors.
There are four types of constructors. They are,
• Default constructors – A constructor that accepts no parameters.
• Parameterized constructors – The constructors that can take arguments.
• Copy constructor – It takes a reference to an object of the same class asitself as an
argument.
• Dynamic constructors – Used to allocate memory while creating objects.
10. Define Destructor.
A destructor is used to destroy the objects that have been created by a constructor. It is a
special member function whose name is same as the class and is preceded by a tilde ‘~’
symbol. When an object goes out from object creation, automatically destructor will be
executed.
Example:
class File {
public:
~File(); //destructor declaration
};
File::~File()
{
close(); // destructor definition
}
11. List Some Of The Rules For Operator Overloading.
, Object Oriented Programming Design CST33 III –Semester
• Only existing operators can be overloaded.
• We cannot change the basic meaning of an operator.
• The overloaded operator must have at least one operand.
• Overloaded operators follow the syntax rules of the original operators.
12. Define parameterized constructor.
Constructor with arguments is called parameterized constructor.
Eg:
Class Student
{
int m, n;
Public:
Student (int x, int y)
{
m=x; n=y;
}
13. What are the characteristics of destructor?
A destructor must be declared in the public section of a class.
A class cannot have more than one destructor.
It has no return type.
It is incorrect to even declare a void return type
14. Define Copy Constructor
A copy constructor is used to declare and initialize an object from another object. It takes
a reference to an object of the same class as an argument
Eg: integer i2 (i1);
Would define the object i2 at the same time initialize it to the values of i1.
15. Define explicit constructor.
Constructor can be defined explicitly by using the keyword “explicit” is known as
an explicit constructor. The explicit constructor will be executed when we call the
constructor explicitly.
Ex:
explicit brother (string name)
{