You are on page 1of 13

J.K.K.

NATTRAJA COLLEGE OF ENGINEERING & TECHNOLOGY


Natarajapuram, (Near Erode) Kumarapalayam- 638 183.
Course Code : CS6456 Subject Name: Object Oriented Programming
Dept/Year/Semester: EEE/ II/ VI Academic Year: 2016 - 2017

QUESTION BANK WITH ANSWERS

UNIT - 2: BASIC CHARACTERISTICS OF OOP

PART-A 2 Marks

1. Define Constructor.
A constructor is defined as a special member of a class, which has no
return type. It can
be used to initializes an object immediately upon creation.

2. Write some special characteristics of constructor.


Some special characteristics of constructor are,
i. They should be declared in the public section
ii. They are invoked automatically when the objects are created.
iii. They do not have return types, not even void and therefore, and
they cannot return
values.
iv. They cannot be inherited, though a derived class can call the base
class
v. They can have default arguments.
vi. Constructors cannot be virtual function.

3. How the objects are initialized dynamically?


The objects an initialized dynamically to call parameterized constructor
or methods we
should the pass values to the object ie, for the constructor integer
add(int a, int b) it is
invoked by integer a (10, 18). This value can be get during run time.
i.e., f or above
constructor
Example:
int p,q; cin>>p>>q; integer add(p,q);

4. Define operator overloading?


Operator overloading is the mechanism of giving such special meanings
to an operator is
known. It provides a flexible option for the creation of new definitions
for C++ operators.

5. List out the operators that cannot be overloaded.


The operators that cannot be overloaded are,
i. Class member access operator (., .*)
ii. Scope resolution operator (::)
iii. Size operator (sizeof)
iv. Conditional operator (? :)

6. List out the operators that cannot be overload as friend functions.


The operators that cannot be overload as friend function are:
i. Assignment operator =
ii. Function call operator ( )
iii. Array subscript operator [ ]
iv. Access to class member using pointer to object operator ->

7. State purpose of using operator function? Write its syntax.


To define an additional task to an operator, we must specify what it
means in relation to
the class to which the operator is applied. This is done by Operator
function , which
describes the task. Operator functions are either member functions or
friend functions.
The general form is :
return type classname :: operator op(arglist )
{
// function body
}

8. Write any four rules for operator overloading.


Rules for Operator overloading are,
i. Only the existing operators can be overloaded.
ii. The overloaded operator must have atleast one operand that is of
user defined data
type.
iii. The basic meaning of the operator should not be changed.
iv. Overloaded operators follow the syntax rules of the original
operators.
v. They cannot be overridden.

9. List out the different types of conversion?


The different types of conversions are,
i. Basic type to Class type.
ii. Class type to Basic type.
iii. Class type to Class type
iv. Basic type to Basic type

10. Write the syntax to convert from class to basic type.


The syntax to convert from class to basic type is, Operator type_name (
)
{

Function statements;

}

11. What are the conditions to satisfy the type casting function?
The conditions to satisfy the type casting function are,
i. It must be a class member.
ii. It must not specify a return type.
iii. It must not have any arguments.

12. Define Friend function? Write the syntax.


A function that has access to the private member of the class but is not
itself a member of
the class is called friend functions. Friend function is preceded by the
keyword friend.
The general form is:
friend datatype function name (object dec);
13. Write some properties of friend function.
The properties of friend function are,
i. Friend function is not in the scope of the class to which it has been
declared as friend.
Hence it cannot be called using the object of that class.
ii. Usually it has object as arguments.
iii. It can be declared either in the public or private part of a class.
iv. It cannot access member names directly. It has to use an object
name and dot
membership operator with each member name. eg: ( A . x )

14. What is function overloading? Give an example.


Function overloading means we can use the same function name to
create functions that
perform a variety of different tasks.
Example:
int add (int a, int b); //add function with 2 arguments of same type
int add (int a, int b, int c); //add function with 3 arguments of same type

15. Write at least four rules for Operator overloading.


Rules for Operator overloading are,
i. Only the existing operators can be overloaded.
ii. The overloaded operator must have at least one operand that is of
user defined data
type.
iii. The basic meaning of the operator should not be changed.
iv. Overloaded operators follow the syntax rules of the original
operators.
v. They cannot be overridden.

16. What is meant by static invocation of virtual function?


It is possible to call virtual function using as object of the class or using
a scope
resolution operator. In that case, virtual function is invoked statically. This is
known as
static invocation of the virtual function. The syntax for static invocation of
virtual
function is,
pointer- -function();

17. What is meant by inheritance?


Inheritance is the process by which objects of one class acquire the
properties of another
class. It supports the concept of hierarchical classification. It provides the
idea of
reusability. We can add additional features to an existing class without
modifying it by
deriving a new class from it.

18. What is meant by visibility mode? Mention the visibility modes


available.
Visibility mode specifies whether the features of the base class are
privately derived or
publicly derived. There are three visibility modes. They are,
i. Private
ii. Public
iii. Protected
19. What are the types in inheritance?
The types in inheritance are,
i. Single inheritance
ii. Multiple inheritance
iii. Multilevel inheritance
iv. Hierarchical inheritance
v. Hybrid inheritance

20.What is a scope resolution operator?


Scope resolution operator is used to uncover the hidden variables. It
also allows access to
global version of variables. Scope resolution operator is used to define the
function
outside the class.

PART-B
16-MARKS QUESTIONS WITH ANSWERS

1. Explain the various types of constructors that are available in C++ with
Suitable examples.
Constructors
Constructors are special class functions which performs initialization of every
object. The Compiler calls the Constructor whenever an object is created. Constructors
initialize values to object members after storage is allocated to the object. While defining
a constructor you must remember that the name of constructor will be same as the name
of the class, and contructors never have return type.

Syntax:
class A// Class name A
{
int x;
public:
A(); //Constructor name A
};

Types of Constructors
Constructors are of three types:

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor

1. Default Constructor

Default constructor is the constructor which doesn't take any argument. It


has no parameter.

Syntax:

class_name() {
-----
-----
}

Example Program:
#include<iostream>
using namespace std;

class Marks
{
public:
int maths;
int science;

//Default Constructor
Marks()
{
maths=0;
science=0;
}
display() {
cout << "Maths : " << maths <<endl;
cout << "Science :" << science << endl;
}
};

int main() {
//invoke Default Constructor
Marks m;
m.display();
return 0;
}

Output:

Maths: 0
Science: 0

2. Parameterized Constructor

Parameterized constructor is by passing the parameters. We can call the parameterized


constructor using
Implicit call
Ex:
Image I (5, 3);
Explicit call
Ex:
Image I=I (5, 3);

Syntax:

class_name(Argument_List)
{
-----
-----
}

Example Program:

#include<iostream>
using namespace std;
class Marks
{
public:
int maths;
int science;
Marks(int mark1,int mark2) //Parametrized Constructor
{
maths = mark1;
science = mark2;
}
display() {
cout << "Maths : " << maths <<endl;
cout << "Science :" << science << endl;
}
};
int main() {
//invoke Parametrized Constructor
Marks m(90,85);
m.display();
return 0;
}

Output

Maths : 90
Science : 85

3. Copy Constructor
Copy constructor is a special type of constructor in which new object is created is
copy of existing object.

Syntax:
class_Name (const class_Name &obj)
{
// body of constructor
}

Example Program:
#include<iostream>
using namespace std;

class marks
{
public:
int maths;
int science;

//Default Constructor
marks()
{
maths=0;
science=0;
}

//Copy Constructor
marks(const marks &obj)
{
maths=obj.maths;
science=obj.science;
}

display(){
cout<<"Maths : " << maths
cout<<"Science : " << science;
}
};

int main(){
marks m1;
marks m2(const marks &m1);
m2.display();
return 0;
}

Output

Maths : 0
Science : 0

4. Dynamic Constructor

Dynamic constructor is used to allocate the memory to the objects at the run time.
Memory is allocated at run time with the help of 'new' operator. By using this constructor,
we can dynamically initialize the objects.
Output:

The value of obj1's ptr is:100


The value of obj2's ptr is: 90

2. Explain the concept of destructor with an example.

The destructor is called when the object is destroyed. The object can be destroyed
automatically when the scope of the objects end or explicitly by using operator delete. The
destructor must have the same name as the class, but proceeded with a tilde sign (~) and it
must also return no value.
3. What is meant by polymorphism? Explain the various types of
polymorphism in C++ with suitable examples.
Polymorphism is the ability to use an operator or function in different ways.
Polymorphism gives different meanings or functions to the operators or functions. Poly,
referring too many, signifies the many uses of these operators and functions. A single
function usage or an operator functioning in many ways can be called polymorphism.
Polymorphism refers to codes, operations or objects that behave differently in different
contexts.
Types of Polymorphism:
C++ provides two different types of polymorphism.

run-time
compile-time

Run-time:
The appropriate member function could be selected while the programming is
running. This is known as run-time polymorphism. The run-time polymorphism is
implemented with inheritance and virtual functions.

Ex: Virtual functions

Virtual functions:
A function qualified by the virtual keyword. When a virtual function is called via a
pointer, the class of the object pointed to determine which function definition will be used.
Virtual functions implement polymorphism, whereby objects belonging to different
classes can respond to the same message in different ways.
Virtual Function in C++

A virtual function is a member function of class that is declared within a base class and re-
defined in derived class.

When you want to use same function name in both the base and derived class, then
the function in base class is declared as virtual by using the virtual keyword and again re-
defined this function in derived class without using virtual keyword.

Syntax

virtual return_type function_name()


{
.......
.......
}

Example:

#include<iostream.h>
#include<conio.h>

class A
{
public:
virtual void show()
{
cout<<"Hello base class";
}
};
class B : public A
{
public:
void show()
{
cout<<"Hello derive class";
}
};

void main()
{
clrsct();
A aobj;
B bobj;
A *bptr;
bptr=&aobj;
bptr->show(); // call base class function

bptr=&bobj;
bptr->show(); // call derive class function
getch();
}

Output

Hello base class


Hello derive class

Compile-time:
The compiler is able to select the appropriate function for a particular call at
compile-time itself. This is known as compile-time polymorphism. The compile-time
polymorphism is implemented with templates.

Ex:
Operator overloading
Function overloading

1. Operator Overloading

The process of making an operator to exhibit different behaviors in


different instances is known as operator overloading.
The general form of an operator function is:

return type classname: : operator(op-arglist)


{
Function body
}

The process of overloading involves the following steps:

1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function operator op () in the public part of the class. It
may be either a member function or a friend function.
3. Define the operator function to implement the required operations.
Example Program: Binary Operator Over loading
#include<iostream.h>
#include<conio.h>

class overloading
{
int value;
public:
void setValue(int temp)
{
value = temp;
}
overloading operator+(overloading ob)
{
overloading t;
t.value=value+ob.value;
return(t);
}
void display()
{
cout<<value<<endl;
}
};

int main()
{
overloading obj1,obj2,result;
int a,b;

cout<<"Enter the value of Complex Numbers a,b:";


cin>>a>>b;
obj1.setValue(a);
obj2.setValue(b);

result = obj1+obj2;

cout<<"Input Values:\n";
obj1.display();
obj2.display();

cout<<"Result:";
result.display();

getch();
return 0;
}

Output:

Enter the value of Complex Numbers a, b: 10


5
Input Values:
10
5
Result: 15
2. Function Overloading

Function overloading is a concept in which one can use many functions having
same function name but can pass different number of parameters or different types of
parameters.
There are two types of polymorphism in C++. Compile time polymorphism and
runtime polymorphism. Function overloading comes under the Compile time
polymorphism.

Example:
#include<iostream.h>
#include<conio.h>

class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}

Output:
30
60

4. What is function overloading? Explain with suitable example.


Function overloading is a concept in which one can use many functions having
same function name but can pass different number of parameters or different types of
parameters.
There are two types of polymorphism in C++. Compile time polymorphism and
runtime polymorphism. Function overloading comes under the Compile time
polymorphism.

Example:
#include<iostream.h>
#include<conio.h>

class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}

Output:
30
60