tell about copy constructor

Answers were Sorted based on User's Feedback



tell about copy constructor..

Answer / sudha

A copy constructor is a special constructor in the C++
programming language used to create a new object as a copy
of an existing object.

There are 3 important places where a copy constructor is
called.

When an object is created from another object of the same
type
When an object is passed by value as a parameter to a
function
When an object is returned from a function


class B //With copy constructor
{
private:
char *name;
public:
B()
{
name = new char[20];
}
~B()
{
delete name[];
}
//Copy constructor
B(const B &b)
{
name = new char[20];
strcpy(name, b.name);
}
};




Is This Answer Correct ?    6 Yes 0 No

tell about copy constructor..

Answer / madhu

Basic thing, copy constructor will be called whenever a copy
is made. and copy constructors are called when:
1. create a new object using existing object.
2. When is returning to caller.
3. When an object is passed by value as a parameter to a
function

Basically a default copy constructor will be created which
does bitwise copy also know as shallow copy.
This will become a problem when we are dealing with dynamic
memory allocation for variables and leads to dangling pointer.
To overcome we have to override by deep copy.

Is This Answer Correct ?    3 Yes 0 No

tell about copy constructor..

Answer / achal ubbott

e.g. Let there be a class

class Sample
{

};
suppose in main() you do like here

Sample obj1;
Sample obj2 = obj1; // Copy cons called here.
// then you call a function like this

fun(obj1); //Copy cons called here.

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More OOPS Interview Questions

How do you define a class in oop?

0 Answers  


How to overload postfix operator in c++

1 Answers   Mphasis,


WAP to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

1 Answers  


Will I be able to get a picture in D drive to the c++ program? If so, help me out?

0 Answers  


what is abstract class ? when is used in real time ? give a exp

5 Answers  






I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...

0 Answers  


Can we define a class within the interface?

0 Answers  


when to use 'mutable' keyword and when to use 'const cast' in c++

0 Answers   TCS,


What is abstraction with example?

0 Answers  


Are polymorphisms mutations?

0 Answers  


What is the benefit of oop?

0 Answers  


what is multi level inheritance give n example ?

13 Answers   HDFC, Hulas Steel, Ness Technologies,


Categories