tell about copy constructor
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
What are the data types in oop?
1. Strong name 2. how to prevent a class from being inherited 3. delegates 4. default modifier for interface 5. default modifier for class 6. base class for exception 7. diff bet trigger and view in sql 8. how to exchange values from one page to another page 9. can multiple catch block ll be executed at same time 10. can u store different data types in an array & array list 11. when we ll use trigger 12. try,catch,finally usage
What is the main difference between C++ and Java
Why do while loop is used?
why function overloading is not called as pure polymorphism?
What is polymorphism and example?
what is the virtual function overhead, and what is it used for ? i hope i can get and appropriate answers, thanks a lot....
What is the oops and benefits of oops programming?
What is the main feature of oop?
What is the significance of classes in oop?
What type of Job you are providing?
What is object in oops?