Here you will find more than 50000 job interview questions

Job Questions Search Engine


Sponsored Links

What is the difference between operator new and the new operator?

September 2, 2010 by bigboss · Leave a Comment
Filed under: C++ Interview Questions 

1. Operator new()  ib C++ : allocate the memory

2. the costructor of the class is invoked to properly initialize this memory.Constructor of the object is called by the new operator which calls the operator new(). So  the new operator does both 1 and 2. The operator new merely allocates memory, it does not initialize it. where as the new operator also initializes it properly by calling the constructor.

So new() operator is used to allocate the memory (with the overaloading of operator ne()) and then initialize the data.

‘new’ operator allocates new instance of object from the heap thus utilising the most appropiate constructor for the argument.

Operator new is the mechanism of overriding the default heap allocation logic.

What is explicit Constructor in C++ Give an example?

The explicit keyword in C++ is used to declare explicit constructors. Explicit constructors are simply constructors that cannot take part in an implicit conversion. Consider the following example

CODE 1 will work

#include<iostream.h>

class A
{
  int data;

public:
  A(int a):data(a)
  {
      cout<<"A::Construcor...\n";
      cout<<"value of data :="<<data<<endl;
  };
};

int main()
{
  A a1 = 37;

  return (0);
}

./a.out
A::Construcor...
value of data :=37

CODE 2 will not work

#include<iostream.h>

class A
{
  int data;

public:
  explicit A(int a):data(a)
  {
      cout<<"A::Construcor...\n";
      cout<<"value of data :="<<data<<endl;
  };
};

int main()
{
  A a1 = 37;

  return (0);
}

We will get the below Compiler Error:
---------------------------------------
explicit1.cc: In function `int main()':
explicit1.cc:18: error: conversion from `int' to non-scalar type `A' requested 

Another Example of explicit contructor
CODE 1 will work:
#include <iostream>
using std::cout;
using std::endl;
class complexNumbers {
  double real, img;
public:
  complexNumbers() : real(0), img(0) { }
  complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
  complexNumbers( double r, double i = 0.0) { real = r; img = i; }
  friend void display(complexNumbers cx);
};
void display(complexNumbers cx){
  cout<<&quot;Real Part: &quot;<<cx.real<<&quot; Imag Part: &quot;<<cx.img<<endl;
}
int main() {
  complexNumbers one(1);
  display(one);
  display(300);
  return 0;
}

CODE 2 will not work:

#include <iostream>
using std::cout;
using std::endl;
class complexNumbers {
  double real, img;
public:
  complexNumbers() : real(0), img(0) { }
  complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
  explicit complexNumbers( double r, double i = 0.0) { real = r; img = i; }
  friend void display(complexNumbers cx);
};
void display(complexNumbers cx){
  cout<<&quot;Real Part: &quot;<<cx.real<<&quot; Imag Part: &quot;<<cx.img<<endl;
}
int main() {
  complexNumbers one(1);
  display(one);
  complexNumbers two =2;
  display(200);
  return 0;
}

Consider the following statement:
 explicit complexNumbers( double r, double i = 0.0) { real = r; img = i; } Here, we are forcing the compiler not to do an implicit conversion  for this piece of code. Now, if the programmer puts a line containing an  implicit conversation, the compiler returns back with an error:
bash-3.2$ g++ -g -o hello test2.cpp
test2.cpp: In function ‘int main()’:
test2.cpp:22: error: conversion from ‘int’ to non-scalar type ‘complexNumbers’ requested
test2.cpp:23: error: conversion from ‘int’ to non-scalar type ‘complexNumbers’ requested

So instead of using display(200) C++ Programmmer will have to use display(complexNumbers(200))

What is conversion constructor in C++?

Conversion constructor works for the typecasting one type of datatype object  to other user defined datatypes
Convertion of one class object to other class object.
Below example show to convert or typecast REAL class object to COMPLEX Class object
Both REAL class and COMPLEX Class are user defined.

COMPLEX  objComplex(6,3); //6 is real and 3 is imagnary
REAL objReal(5);
objComplex=objReal; 
COMPLEX :: COMPLEX(REAL r)
{
real=r.value; // value is the only data member of REAL class
imag=0;
}   

Or another example would be on a object Point class Point
{
public:
    Point();
    Point( int );
    //...
};

Point P1 = 5;
Sometimes a conversion is required but no conversion.
constructor exists in the class. These conversions cannot be
performed by constructors. The compiler does not look for

intermediate types through which to perform the conversion.
For example, suppose a conversion exists from type Point to
type Rect and a conversion exists from type int to type
Point. The compiler does not supply a conversion from type
int to type Rect by constructing an intermediate object of
type Point.

C++ Constructor Interview Questions

July 1, 2010 by bigboss · Comments Off
Filed under: C++ Interview Questions 

Q: What is a default constructor?

A: A constructor that has no arguments.

Q: What is a conversion constructor?

A: A constructor that accepts one argument of a different type.

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: What is an explicit constructor?

A: A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction.

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

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.

How can I handle a constructor that fails?
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.

  • Categories

    |
  • Tags

    ADO.NET Questions Algorithm Questions ASP.NET Questions auto_ptr Binary tree questions C++ Constructor Interview Questions C++ Questions CISCO Exams Questions Common Interview Questions Core Java Interview Questions Csharp Questions datastructure questions Delphi 6 find command gdb interview questions grep interview questions IBM certification exams questions Infosys Puzzles Java Struts Linked List Problem Linux Command Questions List Manager Interview Questions Markov Algorithm memory leakage mysql Interview Questions Normalization Oracle Application Developer Certification Exam Interview Questions Oracle Questions Perl Questions PHP Questions Pointers Interview Questions PostgreSQL Database Questions pthread interview questions Smart Pointer Solaris Interview Questions SQL SERVER Interview Questions STL STL Map Symbian OS Tricky Interview Questions Unix Interview Questions unix shell Vector Windows OS Questions