Subscribe Us

C++ Interview Question

 




Q: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible?

A: There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor

is a code which is responsible for creating an instance of a class and it can’t be delegated to

any other object by virtual keyword means.

 

Q: What is constructor ?

A: Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is

different from other methods in a class.

 

Q: What about Virtual Destructor?

A: Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime

depending on the type of object caller is calling to, proper destructor will be called.

 

Q: What is the difference between a copy constructor and an overloaded assignment operator?

A: A copy constructor constructs a new object by using the content of the argument object. An

overloaded assignment operator assigns the contents of an existing object to another existing

object of the same class.

 

Q: Can a constructor throws an exception? How to handle the error when the constructor fails?

A:The constructor never throws an error.

 

Q: What is default constructor?

A: Constructor with no arguments or all the arguments has default values.

 

Q: What is copy constructor?

A: Constructor which initializes the it's object member variables ( by

shallow copying) with another object of the same class. If you don't implement one in your class

then compiler implements one for you. for example:

(a) Boo Obj1(10); // calling Boo constructor

(b) Boo Obj2(Obj1); // calling boo copy constructor

(c) Boo Obj2 = Obj1;// calling boo copy constructor

 

Q: When are copy constructors called?

A: Copy constructors are called in following cases:

(a) when a function returns an object of that

class by value

(b) when the object of that class is passed by

value as an argument to a function

(c) when you construct an object based on another

object of the same class

(d) When compiler generates a temporary object


Q: Can a copy constructor accept an object of the same class as parameter, instead of reference

of the object?

A: No. It is specified in the definition of the copy constructor itself. It should generate an error if

a programmer specifies a copy constructor with a first argument that is an object and not a

reference.

Q: What is conversion constructor?

A: constructor with a single argument makes that constructor as conversion ctor and it can be

used for type conversion.

for example:

class Boo

{

public:

Boo( int i );

};

Boo BooObject = 10 ; // assigning int 10 Boo object


Q:What is conversion operator??

A:class can have a public method for specific data type conversions.

for example:

class Boo

{

double value;

public:

Boo(int i )

operator double()

{

return value;

}

};

Boo BooObject;

double i = BooObject; // assigning object to variable i of type double.

now conversion operator gets called to assign the value.


Q: How can I handle a constructor that fails?

A: throw an exception. Constructors don't have a return type, so it's not possible to use return

codes. The best way to signal constructor failure is therefore to throw an exception.


Q: How can I handle a destructor that fails?

A: Write a message to a log-fille. But do not throw an exception. The C++ rule is that you must never throw an exception from a destructor that is being called during the "stack unwinding" process of another exception. For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding. During stack unwinding, all the local objects in all those stack frames are destructed. If one of those destructors throws an exception (say it throws a Bar object), the C++ runtime system is in a no-win situation: should it ignore the Bar and end up in the } catch (Foo e) { where it was originally headed? Should it ignore the Foo and look for a } catch (Bare) { handler? There is no good answer:either choice loses information. So the C++ language guarantees that it will call terminate() at this point, and terminate() kills the process. Bang you're dead.

Q: What is Virtual Destructor?

A: Using virtual destructors, you can destroy objects without knowing their type - the correct

destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes. if someone will derive from your class, and if someone will say "new Derived", where "Derived" is derived from your class, and if someone will say delete p, where the actual object's type is "Derived" but the pointer p's type is your class.

Q: Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?

A: No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

Q: What's the order that local objects are destructed?

A: In reverse order of construction: First constructed, last destructed.

In the following example, b's destructor will be executed first, then a's destructor:

void userCode()

{

Fred a;

Fred b;

...

}

Q: What's the order that objects in an array are destructed?

A: In reverse order of construction: First constructed, last destructed.

In the following example, the order for destructors will be a[9], a[8], ..., a[1], a[0]:

void userCode()

{

Fred a[10];

...

}

Q: Can I overload the destructor for my class?

A: No.

You can have only one destructor for a class Fred. It's always called Fred::~Fred(). It never takes

any parameters, and it never returns anything.

You can't pass parameters to the destructor anyway, since you never explicitly call a destructor (well, almost never).

 

Share:

0 Comments:

Post a Comment

If you have any doubts . Please let me know.

Powered by Blogger.

Ad Code

Responsive Advertisement

Ad Code

Responsive Advertisement

Featured post

Search This Blog

Recently added book names

THE HTML AND CSS WORKSHOP   | MICROSOFT POWER BI COOKBOOK   | MongoDB in Action, 2nd Edition  | ADVANCED DEEP LEARNING WITH PYTHON   | Cracking Codes with Python An Introduction to Building and Breaking  | Moris Mano Degital Design 3rd Edition  | Beginning App Development with Flutter by Rap Payne  |react hooks in Action - John Larsen   | Artificial Intelligence A Modern Approach Third Edition Stuart Russel  | Data Structures and Algorithms - Narasimha Karumanchi   | Thomas S.M. - PostgreSQL High Availability Cookbook - 2017  | Gunnard Engebreth PHP 8 Revealed Use Attributes the JIT Compiler   | ICSE Class X Computer Application Notes   | INTERNET OF THINGS PROJECTS WITH ESP32   | 100 aptitude trick(102pgs)s   | OBJECT_ORIENTED_PROGRAMMING Question & Answer   | C questions and answer   | Full_Book_Python_Data_Structures_And_Algorithm   | Jira 8 Administration Cookbook Third Edition  | KALI LINUX WIRELESS PENETRATION TESTING BEGINNERS GUIDE THIRD EDITION - Cameron Buchanan, Vivek Ramachandran  HTML5 & javascript By :- Jeanine Meyer   | Python For Beginners Ride The Wave Of Artificial Intelligence   | HackingTheXbox   | Introduction to Algorithms 3rd.Edition - (CLRS)   | The C++ Programming Language - Bjarne Stroustrup   | Modern C++ Programming Cookbook - Marius Bancila   | Java The Complete Reference Eleventh Edition   Data_Communications and Networking 4th Ed Behrouz A Forouzan   | DevOps with Kubernetes - Hideto Saito   | The-Linux-Command-Line-A-Complete-Introduction   | Assembly Language for X86 Processors KIP R. Irvine   | Effective_Modern_C++ - Scott Meyer

Contact Form

Name

Email *

Message *

Followers

Mobile Logo Settings

Mobile Logo Settings
image

Computer Training School Regd. under Govt. of West Bengal Society Act 1961

Header Ads Widget

Responsive Advertisement

Hot Widget

random/hot-posts

Recent in Sports

Popular Posts

Labels

Blog Archive

Blogger templates