How Does Throwing And Catching Exceptions Differ From Using Setjmp And Longjmp?
The throw operation calls the destructors for automatic objects instantiated since entry to the try block.
How Many Ways Are There To Initialize An Int With A Constant?
There are two ways for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);
What Are The Differences Between A C++ Struct And C++ Class?
The default member and base-class access specifiers are different.
Explain The Scope Resolution Operator?
It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
How Do You Link A C++ Program To C Functions?
By using the extern "C" linkage specification around the C function declarations.
How Do I Initialize A Pointer To A Function?
This is the way to initialize a pointer to a function
void fun(int a)
{
void main()
{
void (*fp)(int);
fp=fun;
fp(i);
}
}
What Does Extern Mean In A Function Declaration In C++?
It tells the compiler that a variable or a function exists, even if the compiler hasn't yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.
How Do I Declare An Array Of N Pointers To Functions Returning Pointers To Functions Returning Pointers To Characters?
If you want the code to be even slightly readable, you will use typedefs.
typedef char* (*functiontype_one)(void);
typedef functiontypeone (*functiontype_two)(void);
functiontype_two myarray[N]; //assuming N is a const integral
What Is The Auto Keyword Good For In C++?
Local variables occur within a scope; they are "local" to a function. They are often called automatic variables because they automatically come into being when the scope is entered and automatically go away when the scope closes. The keyword auto makes this explicit, but local variables default to auto auto auto auto so it is never necessary to declare something as an auto auto auto auto.
What Is The Difference Between Char A[] = "string"; And Char *p = "string";?
In the first case 6 bytes are allocated to the variable a which is fixed, where as in the second case if *p is assigned to some other value the allocate memory can change.
What Can I Safely Assume About The Initial Values Of Variables Which Are Not Explicitly Initialized?
It depends on complier which may assign any garbage value to a variable if it is not initialized.
What Does Extern Mean In A Function Declaration?
Using extern in a function declaration we can make a function such that it can used outside the file in which it is defined.
An extern variable, function definition, or declaration also makes the described variable or function usable by the succeeding part of the current source file. This declaration does not replace the definition. The declaration is used to describe the variable that is externally defined.
If a declaration for an identifier already exists at file scope, any extern declaration of the same identifier found within a block refers to that same object. If no other declaration for the identifier exists at file scope, the identifier has external linkage.
What Is The Best Way To Declare And Define Global Variables?
The best way to declare global variables is to declare them after including all the files so that it can be used in all the functions.
How Do You Decide Which Integer Type To Use?
It depends on our requirement. When we are required an integer to be stored in 1 byte (means less than or equal to 255) we use short int, for 2 bytes we use int, for 8 bytes we use long int.
A char is for 1-byte integers, a short is for 2-byte integers, an int is generally a 2-byte or 4-byte integer (though not necessarily), a long is a 4-byte integer, and a long long is a 8-byte integer.
Anything Wrong With This Code?
t*p = 0;
Delete P;
Yes, the program will crash in an attempt to delete a null pointer.
Anything Wrong With This Code?
t *p = Newt[10];
Delete P;
Everything is correct, Only the first element of the array will be deleted", The entire array will be deleted, but only the first element destructor will be called.
What Problems Might The Following Macro Bring To The Application?
#define sq(x) x*x
What Is An Html Tag?
An HTML tag is a syntactical construct in the HTML language that abbreviates specific instructions to be executed when the HTML script is loaded into a Web browser. It is like a method in Java, a function in C++, a procedure in Pascal, or a subroutine in FORTRAN.
Why Are Arrays Usually Processed With For Loop?
The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a. length -1. That is exactly what a loop does.
What Is Polymorphism In C++? Explain With An Example?
"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus '+' sign, used for adding two integers or for using it to concatenate two strings.
What Do You Mean By Pure Virtual Functions?
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.
class Shape
{
public: virtual void draw() = 0;
};
What Is A Scope Resolution Operator?
A scope resolution operator (::), can be used to define the member functions of a class outside the class.
What Is The Difference Between An External Iterator And An Internal Iterator? Describe An Advantage Of An External Iterator?
An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.
What Are Virtual Functions In C++?
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.
What Is Abstraction In C++?
Abstraction is of the process of hiding unwanted details from the user.
Which Recursive Sorting Technique Always Makes Recursive Calls To Sort Subarrays That Are About Half Size Of The Original Array?
Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in 0(n log n) time.
What Is Friend Function In C++?
As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.
What Is A C++ Class?
Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.
Suppose That Data Is An Array Of 1000 Integers. Write A Single Function Call That Will Sort The 100 Elements Data [222] Through Data [321]?
quicksort((data + 222),100);
What Is The Difference Between An Object And A Class?
Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.
A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.
What Are 2 Ways Of Exporting A Function From A Dll?
1 .Taking a reference to the function from the DLL instance.
2. Using the DLL 's Type Library.
What Do You Mean By Binding Of Data And Functions?
Encapsulation.
What Is The Word You Will Use When Defining A Function In Base Class To Allow This Function To Be A Polimorphic Function?
virtual.
What Is Virtual Class And Friend Class?
Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.
What Is Boyce Codd Normal Form In C++?
A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds
a- > b is a trivial functional dependency (b is a subset of a).
a is a superkey for schema R.
What Is A Copy Constructor And When Is It Called?
A copy constructor is a method that accepts an object of the same class and copies it's data members to the object on the left part of assignement
class Point2D{ int x; int y;
public int color;
protected bool pinned;
public Point2D() : x(0),y(0){} //default (no argument)constructor
public Point2D( const Point2D & );
};
Point2D::Point2D( const Point2D & p )
{
this->x = p.x; this->y = p.y;
this->color = p.color;
this->pinned = p.pinned;
}
main()
{
Point2D MyPoint;
MyPoint.color = 345;
Point2D AnotherPoint = Point2D( MyPoint);
// now AnotherPoint has color = 345
What Do You Mean By Inheritance?
Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.
Can We Define A Constructor As Virtual In C++?
No, we cannot define constructors as virtual because constructors have the same name as their classes and no two constructors of base-derived classes can have the same name. So, when we initialize an object of the base or derived class with the help of virtual constructors, the base constructor is invoked instead of the derived constructor. Therefore, it is not possible to define a constructor as virtual.
Can We Declare A Base-class Destructor As Virtual?
Yes, we can declare a base-class destructor as virtual that makes all derived-class destructors as virtual even if they do not have the same name as base-destructor. The problem arises when the derived class's pointer refers to a base class. For this reason, the base class destructor should be declared as virtual so that the appropriate destructor is called on calling the delete method of the base class object.
How Does A Copy Constructor Differs From An Overloaded Assignment Operator?
A copy constructor uses the value of an argument to construct a new object. We can use an overload assignment operator to assign the value of an existing object of the same class to another existing object in that class.
Define The Process Of Error-handling In Case Of Constructor Failure?
If the constructor does not have the return statement, then it indicates failure in handling the error by throwing an exception.
What Is A Default Constructor?
A zero-argument constructor or a constructor in which all the arguments have default values is called a default constructor.
For example
A Al; // default constructor called.
Define The Process Of Handling In Case Of Destructor Failure?
In order to handle a failed destructor, you need to write a message to a log file; however, do not throw an exception. There is a rule in C++ that exception cannot be thrown from a destructor, which is called when the process of "stack unwinding" occurs in other exceptions. For example, if someone says throw waste files(), the stack frames between the throw waste files() and the catch (waste files) will get popped. This is known as stack unwinding. It is the process of destroying all the local objects related to those stack frames and calling destructors in case of throwing of an exception by one of those destructors. For example, if an object named Bar is thrown, then the C++ runtime system is in a neutral situation means either to avoid the Bar and end up in the catch (waste files) or ignore the function Foo and look for a catch (Bar) handler. It will call in the terminate () process to end the program.
What Is A Virtual Destructor?
Virtual destructors help in destroying objects without knowing their type. With the help of the virtual function mechanism, the appropriate destructor for the object is invoked. In the case of abstract classes, destructors can also be declared as pure virtual functions. For example, class A derives from class B. Then, on calling the derived class dynamically at the execution time, the destructor will first call the derived class that is class A, and then the base class that is class B.
It is important to note that theVirtual keyword, when used with the destructor, ensures the calling of all the derived and base class destructors and therefore helps in the proper execution and closing of the program.
Can You Explicitly Call A Destructor On A Local Variable?
No, the destructor is called when the block closes in which the local variable was created.
If a destructor is called explicitly twice on the same object, the result ends in an undefined behavior because the local variable gets destroyed when the scope ends.
What Are Shallow And Deep Copies?
A shallow copy is used to copy actual values of the data. It means, if the pointer points to dynamically allocated memory, the new object's pointer in the copy still points to items inside the old objects and the returned object will be left pointing to items that are no longer in scope. A copy of the dynamically allocated objects is created with the help of a deep copy. This is done with the help of an assignment operator, which needs to be overloaded by the copy constructor.
What Do You Understand By Zombie Objects In C++?
In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.
Should The This Pointer Can Be Used In The Constructor?
We can use the this pointer in the constructor in the initialization list and also in the body. However, there is a feeling that the object is not fully formed so we should not use this pointer. Let's understand the use of the this pointer with the help of the following example. The declared data members of a base class and/or the declared data members of the constructor's own class can be accessed by the constructor {body} and/or a function called from the constructor. This is possible because of the full construction of constructor's body at the time of execution. The preceding example always works.
The override in the derived class is not possible for a function called from the constructor and the {body} of a constructor which is independent of calling the virtual member function by the explicit use of the this pointer.
Please make sure that the initialization of other data member has already been done before passing the data member to another data member's initializer in this object. With the help of some straightforward language rules (independent of the specific compiler that you are using), you can confirm about the determination of initialization of data members.
Without the prior knowledge of these rules, there is no use of passing any data member from the this object.
What Do You Understand By A Pure Virtual Member Function?
A virtual function with no body structure is called a pure virtual member function. You can declare a pure virtual member function by adding the notation =0 to the virtual member function declaration. For example, area () is a virtual function, but when it is declared as shown in the following expression, it becomes a pure virtual member function: virtual int area () =0;
To avoid a compilation error, all the classes need to implement pure virtual classes that are derived from the abstract class.
How Can Virtual Functions In C++ Be Implemented?
When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table, contains an entry for each virtual function in the class. The constructor of the class is used to create this table. Each object of the class in memory includes a variable called the vptr, which points to the class's common vtbl. The _rst (which creates the vtable) is constructed by the base classes after the construction of the derived classes. The derived class constructor overwrites the entries of the vtable, if its constructor overrides any virtual function of the base class. Therefore, you need not call every function from a constructor just to avoid the error prone scenario of calling the Base class constructor instead of the derived class constructor (which fails to set the entries of objects of vtable).
What Do You Understand By Pure Virtual Function? Write About Its Use?
A pure virtual function in a base class must have a matching function in a derived class. A program may not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. If you want programmers to override certain functions in a class, such as those that need information customized for a particular installation, you should make these functions pure virtual.
The throw operation calls the destructors for automatic objects instantiated since entry to the try block.
How Many Ways Are There To Initialize An Int With A Constant?
There are two ways for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);
What Are The Differences Between A C++ Struct And C++ Class?
The default member and base-class access specifiers are different.
Explain The Scope Resolution Operator?
It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
How Do You Link A C++ Program To C Functions?
By using the extern "C" linkage specification around the C function declarations.
How Do I Initialize A Pointer To A Function?
This is the way to initialize a pointer to a function
void fun(int a)
{
void main()
{
void (*fp)(int);
fp=fun;
fp(i);
}
}
What Does Extern Mean In A Function Declaration In C++?
It tells the compiler that a variable or a function exists, even if the compiler hasn't yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.
Wipro Frequently Asked C++ Interview Questions Answers |
How Do I Declare An Array Of N Pointers To Functions Returning Pointers To Functions Returning Pointers To Characters?
If you want the code to be even slightly readable, you will use typedefs.
typedef char* (*functiontype_one)(void);
typedef functiontypeone (*functiontype_two)(void);
functiontype_two myarray[N]; //assuming N is a const integral
What Is The Auto Keyword Good For In C++?
Local variables occur within a scope; they are "local" to a function. They are often called automatic variables because they automatically come into being when the scope is entered and automatically go away when the scope closes. The keyword auto makes this explicit, but local variables default to auto auto auto auto so it is never necessary to declare something as an auto auto auto auto.
What Is The Difference Between Char A[] = "string"; And Char *p = "string";?
In the first case 6 bytes are allocated to the variable a which is fixed, where as in the second case if *p is assigned to some other value the allocate memory can change.
What Can I Safely Assume About The Initial Values Of Variables Which Are Not Explicitly Initialized?
It depends on complier which may assign any garbage value to a variable if it is not initialized.
What Does Extern Mean In A Function Declaration?
Using extern in a function declaration we can make a function such that it can used outside the file in which it is defined.
An extern variable, function definition, or declaration also makes the described variable or function usable by the succeeding part of the current source file. This declaration does not replace the definition. The declaration is used to describe the variable that is externally defined.
If a declaration for an identifier already exists at file scope, any extern declaration of the same identifier found within a block refers to that same object. If no other declaration for the identifier exists at file scope, the identifier has external linkage.
What Is The Best Way To Declare And Define Global Variables?
The best way to declare global variables is to declare them after including all the files so that it can be used in all the functions.
How Do You Decide Which Integer Type To Use?
It depends on our requirement. When we are required an integer to be stored in 1 byte (means less than or equal to 255) we use short int, for 2 bytes we use int, for 8 bytes we use long int.
A char is for 1-byte integers, a short is for 2-byte integers, an int is generally a 2-byte or 4-byte integer (though not necessarily), a long is a 4-byte integer, and a long long is a 8-byte integer.
Anything Wrong With This Code?
t*p = 0;
Delete P;
Yes, the program will crash in an attempt to delete a null pointer.
Anything Wrong With This Code?
t *p = Newt[10];
Delete P;
Everything is correct, Only the first element of the array will be deleted", The entire array will be deleted, but only the first element destructor will be called.
What Problems Might The Following Macro Bring To The Application?
#define sq(x) x*x
What Is An Html Tag?
An HTML tag is a syntactical construct in the HTML language that abbreviates specific instructions to be executed when the HTML script is loaded into a Web browser. It is like a method in Java, a function in C++, a procedure in Pascal, or a subroutine in FORTRAN.
Why Are Arrays Usually Processed With For Loop?
The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a. length -1. That is exactly what a loop does.
What Is Polymorphism In C++? Explain With An Example?
"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus '+' sign, used for adding two integers or for using it to concatenate two strings.
What Do You Mean By Pure Virtual Functions?
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.
class Shape
{
public: virtual void draw() = 0;
};
What Is A Scope Resolution Operator?
A scope resolution operator (::), can be used to define the member functions of a class outside the class.
What Is The Difference Between An External Iterator And An Internal Iterator? Describe An Advantage Of An External Iterator?
An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.
What Are Virtual Functions In C++?
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.
What Is Abstraction In C++?
Abstraction is of the process of hiding unwanted details from the user.
Which Recursive Sorting Technique Always Makes Recursive Calls To Sort Subarrays That Are About Half Size Of The Original Array?
Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in 0(n log n) time.
What Is Friend Function In C++?
As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.
What Is A C++ Class?
Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.
Suppose That Data Is An Array Of 1000 Integers. Write A Single Function Call That Will Sort The 100 Elements Data [222] Through Data [321]?
quicksort((data + 222),100);
What Is The Difference Between An Object And A Class?
Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.
A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.
What Are 2 Ways Of Exporting A Function From A Dll?
1 .Taking a reference to the function from the DLL instance.
2. Using the DLL 's Type Library.
What Do You Mean By Binding Of Data And Functions?
Encapsulation.
What Is The Word You Will Use When Defining A Function In Base Class To Allow This Function To Be A Polimorphic Function?
virtual.
What Is Virtual Class And Friend Class?
Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.
What Is Boyce Codd Normal Form In C++?
A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds
a- > b is a trivial functional dependency (b is a subset of a).
a is a superkey for schema R.
What Is A Copy Constructor And When Is It Called?
A copy constructor is a method that accepts an object of the same class and copies it's data members to the object on the left part of assignement
class Point2D{ int x; int y;
public int color;
protected bool pinned;
public Point2D() : x(0),y(0){} //default (no argument)constructor
public Point2D( const Point2D & );
};
Point2D::Point2D( const Point2D & p )
{
this->x = p.x; this->y = p.y;
this->color = p.color;
this->pinned = p.pinned;
}
main()
{
Point2D MyPoint;
MyPoint.color = 345;
Point2D AnotherPoint = Point2D( MyPoint);
// now AnotherPoint has color = 345
What Do You Mean By Inheritance?
Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.
Can We Define A Constructor As Virtual In C++?
No, we cannot define constructors as virtual because constructors have the same name as their classes and no two constructors of base-derived classes can have the same name. So, when we initialize an object of the base or derived class with the help of virtual constructors, the base constructor is invoked instead of the derived constructor. Therefore, it is not possible to define a constructor as virtual.
Can We Declare A Base-class Destructor As Virtual?
Yes, we can declare a base-class destructor as virtual that makes all derived-class destructors as virtual even if they do not have the same name as base-destructor. The problem arises when the derived class's pointer refers to a base class. For this reason, the base class destructor should be declared as virtual so that the appropriate destructor is called on calling the delete method of the base class object.
How Does A Copy Constructor Differs From An Overloaded Assignment Operator?
A copy constructor uses the value of an argument to construct a new object. We can use an overload assignment operator to assign the value of an existing object of the same class to another existing object in that class.
Define The Process Of Error-handling In Case Of Constructor Failure?
If the constructor does not have the return statement, then it indicates failure in handling the error by throwing an exception.
What Is A Default Constructor?
A zero-argument constructor or a constructor in which all the arguments have default values is called a default constructor.
For example
A Al; // default constructor called.
Define The Process Of Handling In Case Of Destructor Failure?
In order to handle a failed destructor, you need to write a message to a log file; however, do not throw an exception. There is a rule in C++ that exception cannot be thrown from a destructor, which is called when the process of "stack unwinding" occurs in other exceptions. For example, if someone says throw waste files(), the stack frames between the throw waste files() and the catch (waste files) will get popped. This is known as stack unwinding. It is the process of destroying all the local objects related to those stack frames and calling destructors in case of throwing of an exception by one of those destructors. For example, if an object named Bar is thrown, then the C++ runtime system is in a neutral situation means either to avoid the Bar and end up in the catch (waste files) or ignore the function Foo and look for a catch (Bar) handler. It will call in the terminate () process to end the program.
What Is A Virtual Destructor?
Virtual destructors help in destroying objects without knowing their type. With the help of the virtual function mechanism, the appropriate destructor for the object is invoked. In the case of abstract classes, destructors can also be declared as pure virtual functions. For example, class A derives from class B. Then, on calling the derived class dynamically at the execution time, the destructor will first call the derived class that is class A, and then the base class that is class B.
It is important to note that theVirtual keyword, when used with the destructor, ensures the calling of all the derived and base class destructors and therefore helps in the proper execution and closing of the program.
Can You Explicitly Call A Destructor On A Local Variable?
No, the destructor is called when the block closes in which the local variable was created.
If a destructor is called explicitly twice on the same object, the result ends in an undefined behavior because the local variable gets destroyed when the scope ends.
What Are Shallow And Deep Copies?
A shallow copy is used to copy actual values of the data. It means, if the pointer points to dynamically allocated memory, the new object's pointer in the copy still points to items inside the old objects and the returned object will be left pointing to items that are no longer in scope. A copy of the dynamically allocated objects is created with the help of a deep copy. This is done with the help of an assignment operator, which needs to be overloaded by the copy constructor.
What Do You Understand By Zombie Objects In C++?
In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.
Should The This Pointer Can Be Used In The Constructor?
We can use the this pointer in the constructor in the initialization list and also in the body. However, there is a feeling that the object is not fully formed so we should not use this pointer. Let's understand the use of the this pointer with the help of the following example. The declared data members of a base class and/or the declared data members of the constructor's own class can be accessed by the constructor {body} and/or a function called from the constructor. This is possible because of the full construction of constructor's body at the time of execution. The preceding example always works.
The override in the derived class is not possible for a function called from the constructor and the {body} of a constructor which is independent of calling the virtual member function by the explicit use of the this pointer.
Please make sure that the initialization of other data member has already been done before passing the data member to another data member's initializer in this object. With the help of some straightforward language rules (independent of the specific compiler that you are using), you can confirm about the determination of initialization of data members.
Without the prior knowledge of these rules, there is no use of passing any data member from the this object.
What Do You Understand By A Pure Virtual Member Function?
A virtual function with no body structure is called a pure virtual member function. You can declare a pure virtual member function by adding the notation =0 to the virtual member function declaration. For example, area () is a virtual function, but when it is declared as shown in the following expression, it becomes a pure virtual member function: virtual int area () =0;
To avoid a compilation error, all the classes need to implement pure virtual classes that are derived from the abstract class.
How Can Virtual Functions In C++ Be Implemented?
When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table, contains an entry for each virtual function in the class. The constructor of the class is used to create this table. Each object of the class in memory includes a variable called the vptr, which points to the class's common vtbl. The _rst (which creates the vtable) is constructed by the base classes after the construction of the derived classes. The derived class constructor overwrites the entries of the vtable, if its constructor overrides any virtual function of the base class. Therefore, you need not call every function from a constructor just to avoid the error prone scenario of calling the Base class constructor instead of the derived class constructor (which fails to set the entries of objects of vtable).
What Do You Understand By Pure Virtual Function? Write About Its Use?
A pure virtual function in a base class must have a matching function in a derived class. A program may not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. If you want programmers to override certain functions in a class, such as those that need information customized for a particular installation, you should make these functions pure virtual.
Post a Comment