Page 1
CS2203 - Object Oriented Programming
Branch: B.E Computer Science and Engineering
Year: II Semester: III
__rendered_path__22
__rendered_path__21
Prepared By,
__rendered_path__20
__rendered_path__18__rendered_path__19
L. Maria Michael Visuwasam
__rendered_path__17
Assistant Professor, CSE
__rendered_path__16
__rendered_path__15
UNIT-2
__rendered_path__13__rendered_path__14
1. Define Constructor.
A constructor is a special member function whose task is to initialize the objects of its
__rendered_path__12
class. The constructor name is same as class name. It is called constructor because it
__rendered_path__10__rendered_path__11
constructs the values of data members of the class.
__rendered_path__5
__rendered_path__9
2. List some of the special characteristics of constructor
They should be declared in the public section.
Image_19_0
__rendered_path__7__rendered_path__8
__rendered_path__6Image_21_0
__rendered_path__5Image_23_0
Image_25_0
__rendered_path__3__rendered_path__4
3. Give the various types of constructors. There are four types
__rendered_path__1
constructor that accepts no parameters
Image_27_0
It can take arguments
__rendered_path__2Image_34_0
One argument constructor it can take one argument
Two argument constructor it can take two arguments
__rendered_path__1
Multiple argument Constructor it can more than 2 arguments.
Image_36_0
It takes a reference to an object of the same class as itself as an
argument.
Image_38_0
- used to allocate memory while creating objects.
4. What are the ways in which a constructor can be called?
The constructor can be called in two ways, they are.
__rendered_path__29
L. Maria Michael Visuwasam
Page 1
__rendered_path__30

Page 2
Eg:
integer int1(0,100);
Class objects can be initialized dynamically. The initial values of an object may be
ime. The advantage of dynamic initialization is that we can
provide various initialization formats, using overloaded constructors. It provides
at have been created by a constructor. It
proceeded
by a
What is an operator function. Describe the syntax of an operator function.
-
> operator
L. Maria Michael Visuwasam
Page
__rendered_path__29
5.
6.
7.
Image_42_0
Eg: integer int1=integer(0,100);
Image_44_0
State the dynamic initialization objects.
provided during run t
flexibility of using different data formats.
Define Destructor.
A destructor is used to destroy the objects th
__rendered_path__20
is a special member function whose name is same as the class and
__rendered_path__18__rendered_path__19
tilde „~‟ symbol.
__rendered_path__16__rendered_path__17
The general form is
__rendered_path__15
Return-type classname :: operator op(arug list)
__rendered_path__14
{ Function body }
__rendered_path__13
Where return type -> type of value returned Operator -> keyword Op
being overloaded.
__rendered_path__12
__rendered_path__11
8. List the rules for operator overloading.
__rendered_path__10
__rendered_path__5Image_46_0
__rendered_path__9Image_48_0
Image_50_0
follow the syntax rules of the original operators.
__rendered_path__7__rendered_path__8Image_52_0
9. List some of operators that cannot overload in C++.
__rendered_path__6
__rendered_path__5Image_54_0
Image_56_0
__rendered_path__3__rendered_path__4Image_58_0
rator(sizeof)
Image_60_0
__rendered_path__2__rendered_path__1Image_62_0
10. What are the conditions to be satisfied for casting?
Image_64_0
Image_66_0
Image_68_0
11. What are the types of type conversion?
Image_70_0
ersion from basic type to class type
Image_72_0
Image_74_0
__rendered_path__21
__rendered_path__1__rendered_path__22__rendered_path__30
2

Page 3
12. What is operator overloading?
The mechanism of giving such special meanings to an operator is known as operator
overloading. or In c++ you can give special meanings to operators when they are used with
user defined classes. This is called operator overloading.
13. Why is it necessary to overload an operator?
To define a new relation task to an operator, we must specify what it means in relation to the
class to which the operator is applied. This is done with the help of a special function called
operator function. Or It allows the developer to program using notation closer to the Unit-2_2 marks questions with answers
domain and allow user types to look like types built into the language. Or The ability to tell
__rendered_path__22
the compiler how to perform a certain operation when its corresponding operator is used on
__rendered_path__21
one or more variables.
__rendered_path__20
14. What is a conversion function? How it is created? Explain its syntax
__rendered_path__18__rendered_path__19
The type of data to the right of an assignment operator is automatically converted to the type
__rendered_path__17
of the variable on the left. For e.g., the statements int m; float x=3.14; m=x; Convert x to an
integer before its value is assigned t0 m. thus the fractional part is truncated.
__rendered_path__16
__rendered_path__15
15. When is a friend function compulsory? Give an eg.
__rendered_path__14
A friend function is necessary when you define a function outside the class. And to access the
__rendered_path__13
private members of the class or the member function and also friend class can directly access
the private and protected data.
__rendered_path__12
__rendered_path__11
16. What is meant by copy constructor?
__rendered_path__10
A copy constructor takes a reference to an object of the same class as itself as an argument.
__rendered_path__5
__rendered_path__9
17. What is the need of object initialization?
__rendered_path__7__rendered_path__8Image_78_0
specific default values.
__rendered_path__6
__rendered_path__5Image_80_0
Image_82_0
__rendered_path__3__rendered_path__4
18. What are the two main functions of constructors?
The two main functions are
__rendered_path__1
1. It automatically initializes the object
__rendered_path__2
2. It usually provides initial values for the data members of the object.
__rendered_path__1
19. What are the rules for defining constructor?
Image_84_0
st have the same name as the class itself.
Image_86_0
different names and same arguments.
Image_88_0
Image_90_0
uctor should not have a return statement in the body of the constructor itself.
20. What are the operators which cannot be overloaded as a friend?
1. Assignment operator =
__rendered_path__29
L. Maria Michael Visuwasam
Page 3
__rendered_path__30

Page 4
2. Function call operator ()
3. Array Subscript operator []
4. Access to class member using pointer to object operator ->
21. What are the restrictions of operator overloading
Image_94_0
Image_96_0
Image_98_0
oaded.
Image_100_0
overloaded.
Image_102_0
-defined types. All overloaded must have at least
one argument as user defined type.
__rendered_path__22
__rendered_path__21
22. Difference between overloading of unary and binary operator
__rendered_path__20
All operators having a single argument are unary operators. When we overload these
__rendered_path__18__rendered_path__19
operators as member functions, we do not need to pass any argument explicitly. The pointer
__rendered_path__17
pointing to invoke object is passed as an implicit argument. Operators with two operand are
__rendered_path__16
known as binary operators. They will have a single argument when defined as member.
__rendered_path__15
23. What are function objects.
__rendered_path__13__rendered_path__14
Objects of the classes where () operator is overloaded. In the case objects can be written with
a () and can be treated like functions. Such objects which can be called like a function are
__rendered_path__12
known as function objects.
__rendered_path__5__rendered_path__10__rendered_path__11
24. What are the different types of conversion? Compare them
__rendered_path__9
A constructor converts from a foreign object to a native object while an operator converts a
native object to a foreign object.
__rendered_path__7__rendered_path__8
25. What is wrapper class. A Class which makes a C-like struct or abuilt-in type data
__rendered_path__6
represented as a class. For e.g. an integer wrapper class represents a data type int as a class.
__rendered_path__5
__rendered_path__1__rendered_path__3__rendered_path__4
26. Difference between constructor and destructor.
__rendered_path__2__rendered_path__112
Constructor
Destructor
__rendered_path__1__rendered_path__112
It is used to construct the object.
It is used to destroy the object.
__rendered_path__113
It can be called automatically when an object It is called when an object goes out of scope.
__rendered_path__112
comes into existence.
__rendered_path__114
The constructor function is called every time The destructor function is called every time
__rendered_path__112
an object is created.
the program exits a block.
__rendered_path__29__rendered_path__112
L. Maria Michael Visuwasam
Page 4
__rendered_path__30__rendered_path__115__rendered_path__115__rendered_path__115__rendered_path__112__rendered_path__113__rendered_path__112__rendered_path__114__rendered_path__112__rendered_path__115__rendered_path__115__rendered_path__115__rendered_path__112__rendered_path__113__rendered_path__112__rendered_path__114__rendered_path__112__rendered_path__133__rendered_path__133__rendered_path__133__rendered_path__112__rendered_path__113__rendered_path__112__rendered_path__114__rendered_path__112__rendered_path__133__rendered_path__112__rendered_path__112__rendered_path__113__rendered_path__133__rendered_path__112__rendered_path__114__rendered_path__133__rendered_path__112__rendered_path__112

Page 5
__rendered_path__41
It is the function invoked first before calling It is function called at last.
__rendered_path__41
any function.
__rendered_path__42
It takes any number of arguments.
It takes no argument.
__rendered_path__41
It can be overloaded.
It cannot be overloaded.
__rendered_path__43
Constructor name and class name should be Destructor name and class name should be
__rendered_path__41
the same.
the same but it is prefixed by ~
__rendered_path__41
Eg: student()
Eg: ~student()
__rendered_path__22__rendered_path__44
It cannot be declared as virtual function.
It can be a virtual function.
__rendered_path__21__rendered_path__44
It behaves like a new operator
It behaves like a delete operator.
__rendered_path__18__rendered_path__19__rendered_path__20__rendered_path__44
__rendered_path__17__rendered_path__41
27. Comparison between conversion constructor and conversion operator function.
__rendered_path__16__rendered_path__42
Conversion constructor:
__rendered_path__14__rendered_path__15__rendered_path__41
It is used to convert from some other object to native object. In other words, it
__rendered_path__13__rendered_path__43
converts foreign object to native object.
__rendered_path__12__rendered_path__41
If conversion is needed from firs class to second class then constructor is required.
__rendered_path__10__rendered_path__11__rendered_path__53
Conversion operator function:
__rendered_path__9__rendered_path__5__rendered_path__53
It is used to convert from native object to other object. In other words, it converts
__rendered_path__53
from native object to foreign object.
__rendered_path__7__rendered_path__8__rendered_path__41
It conversion is needed from second class to first class then conversion operator
__rendered_path__6__rendered_path__42
function is required.
__rendered_path__1__rendered_path__2__rendered_path__1__rendered_path__3__rendered_path__4__rendered_path__5__rendered_path__41
L. Maria Michael Visuwasam
Page 5
__rendered_path__29__rendered_path__30__rendered_path__43__rendered_path__41__rendered_path__62__rendered_path__62__rendered_path__62__rendered_path__41__rendered_path__42__rendered_path__41__rendered_path__43__rendered_path__41__rendered_path__81__rendered_path__81__rendered_path__81__rendered_path__41__rendered_path__42__rendered_path__41__rendered_path__43__rendered_path__41__rendered_path__53__rendered_path__53__rendered_path__53__rendered_path__41__rendered_path__42__rendered_path__41__rendered_path__43__rendered_path__41__rendered_path__53__rendered_path__41__rendered_path__41__rendered_path__42__rendered_path__53__rendered_path__41__rendered_path__43__rendered_path__53__rendered_path__41__rendered_path__41