What is the difference between a constant pointer and a pointer to a constant?

Constant pointer:

A Constant pointer is a pointer variable whose content can not be changed once it is assigned.

Ex: int * const ptr 1=4;

In the above statement, ptr is a constant pointer that stores the address of value 4. That means we can not change the content of the pointer variable ptr1. The pointer prt1 always points to the same address location.

Pointer to a constant:

A Pointer to a constant is a pointer variable that points to the address whose value can not be changed.

Ex: int cont * ptr2=&m;

In the above statement, the pointer ptr always pointer to that address whose values are the same but to that address may be different.

Differences between the new operator and the delete operator?

The new operator is used for memory allocation. This operator is used to create an object.

Ex: int >*p=new int[10];

The delete operator is used to deallocate the object from the memory.

Ex: Delete []p;

How is polymorphism achieved at:

1> Compile time. 2> Run time

Compile time:

The compile time polymorphism is achieved by operator overloading or by function overloading. Using operator overloading the operator ‘+’ can be used as an addition operator of two integers in one situation. This plus operator can be used to concatenate two strings by using overloading the ‘+’ operator. This distinction of the working principle of ‘+’ operate can be known at the time of compile time. This type of phenomenon is called compile-time polymorphism.

Run-time polymorphism:

The run-time polymorphism is achieved by the virtual function. In the virtual function concept, the virtual function is declared in the base class with no internal statement. That means the function is defined without any statement. Now the base class is derived by a different derived class, with the same function name which is declared virtual in the base class. Each derived class is defined as its own function according to the virtual function name. When an object of a derived class has its own function with the same name defined in the base class. This type of situation of the function call is decided at run time.

Differences between macro and inline functions?

MacroInline
Macro is a pre-processor directive.Inline function is a type of function
There is no error checking involved with macro.The error_cheaking facilities involved with inline function.
The macro can not be overloaded.The inline function can be overloaded.
The compiler never ignores the macroThe compiler can ignore the inline declaration. If the function is too long

What is a void pointer?

The void pointer is a generic type of pointer. In C++ special care has to be taken to handle to assignment of void pointer to another pointer type.

For Example: void *p;

char * s;

p=s;

int * i;

p= i;

In the above statement, the void pointer p can assign the address of the integer type variable as well as the character type variable depending on the situation.

Advantage of polymorphism?

  1. Using polymorphism the same code can be used.
  2. Reduce the program development time.
  3. The Same function name can be used for different function definitions.
  4. The derived class can be created from the base class with common data members and methods between them.
  5. It allows the overloading of the operator to show that an operation can exhibit different behavior.

What is the avoid pointer?

The void pointer is a generic type of pointer. In C++ special care has to be taken to handle to assignment of void pointer to other pointer.

For Example: void *p;

char * s;

p=s;

int *i;

p=i;

In the above statement, the void pointer p can assign the address of integer type variable as well as character type variable as well as character type variable depending on the situation.

Advantage of polymorphism?

  1. Using polymorphism the same code can be used.
  2. Reduce the program development time.
  3. The Same function name can be used in different function definitions.
  4. The derived class can be created from the base class with common data members and methods between them.
  5. It allows the overloading of the operator to show that an operation can exhibit different behavior.

Difference between static member function and constant member function:

The static member function can have access to only static members, declared in the same class. The static member may be another static member function or static member variable declared within the class.

In the above class, the static member function shows count can have access only to static member variable count.

Constant member function:

The constant member function guarantees that we modify any of its class member data which means this type of member function can not modify the value of the member variable.

In the above class, the function modify data is treated as constant member data. That means this function has no right to change or modify the value of member data of that class.

Why constructor function without initialization is essential?

The constructor is invoked whenever an object of its associated class is created, at the time of the object of its associated class is created at the time of object creation the constructor of a class requests the operating system for allocation of memory for that object and attaches a memory address to the object name. If the use of a constructor does not create memory space then dynamic memory allocation will be meaningful less that’s why it is essential to attach an address of memory space to the corresponding object name.

What is the advantage of using a reference variable compared to a pointer variable?

For using a pointer variable same value is described by two different memory spaces. One for the pointer variable and another for the value itself but in the reference variable different names can be used for the same memory space, which that means for using the reference variable only a single memory space is required for each variable.

That’s why using a reference variable is advantageous, over a pointer variable.

A friend function can not be used to overload the assignment operator “–” Explain why.

An assignment operator is done by overloading the “–” operator to assign one value to the member variable of an object. If the “–” operator is overloaded and also declared as a friend function then it is not possible to assign an operation to be a member variable of an object. Because the friend function can not be used as a local member function of an object. That’s why the overloaded assignment operator can not be used as a friend function.

We have two classes ‘X’ and ‘Y’ if ‘a’ is an object of ‘X’ and b is an object of Y and we want to say a=b;

What type of conversion is used and where?

One class to another class type conversion routine should be used. Here the class ‘Y’ is known as the source class and ‘X’ is known as the destination class. Such conversion between objects of different classes can carried out by either a constructor or a conversion function depending on the source class and destination class.

What is a template class?

A class created from a class template is called a template class. The syntax for defining an object of a template class is:

class name <type> object name (arg list); the process of creating a template class from a class template is called instantiation. In the compiler, perform the error analysis when an instantiation takes place. It is therefore essential to create and debug an ordinary class before converting it into a temple. All template classes can be considered as class templates but all class template is not template class.

What are the differences between the C structure and the C++ class?

C++ supports all the features of structure as defined in c but C++ has expanded its capability further to suit its OOP philosophy. It attempts to bring the user-defined type as close as possible to the built-in data types and also provides a facility to hide the data which is one of the main precepts of OOP. Inheritance a mechanism by which one type can inherit characteristics from other types, is also supported by C++.

In C++, a structure can have both variables and functions as members. It can also declare some of its members as “private” so that the external function cannot access them directly. In C++, the structure names are stand. Alone and can be used like any other type name. That is, the keyword structure can be omitted in the declaration of the structure variable.

For Example– We can declare the student variable A as student A; // c++ declaration This is an error in c. c++ incorporates all these extensions in another user-defined type known as class. There is very little syntactical difference between structures and classes in C++; therefore, they can be used interchangeably with minor modifications. Since the class is a specially introduced data type in C++, most C++ programmers tend to use the structures for holding only data and classes to hold both the data and functions.

In this context, one important point to note is that whatever the features discussed concerning class all these features are supported by the structure of C++ but the traditional c.


Leave a Reply

Your email address will not be published. Required fields are marked *