WHEN A COPY CONSTER IS CALL ?

Answers were Sorted based on User's Feedback



WHEN A COPY CONSTER IS CALL ? ..

Answer / preeti

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 A //With copy constructor
{
private:
char *name;
public:
A()
{
name = new char[20];
}
~A()
{
delete name[];
}
//Copy constructor
A(const A &b)
{
name = new char[20];
strcpy(name, b.name);
}
};

Is This Answer Correct ?    9 Yes 0 No

WHEN A COPY CONSTER IS CALL ? ..

Answer / achal ubbott

Question on copy constructor is a classic one for an
interview. Since most modern day c++ compilers provide a
default copy constructor, most people don't get to try
hands over it. But in some cases it becomes mandatory to
define your own copy constructor and override the default
one.
So the places when CC is invoked are:-

1. calling a function e.g. void f(sample A);

2. creating an object from the existing object.
e.g. sample A=B; // here B is existing object.

3. When a function returns a copy of object.

e.g. sample f()
{
sample a;
return a;
}

Is This Answer Correct ?    2 Yes 0 No

WHEN A COPY CONSTER IS CALL ? ..

Answer / vishwa

emp e;//default constr
emp e(10);//paramatrisized constr
emp e(e1);//copy constr
emp e = e1;//copy constr

Is This Answer Correct ?    2 Yes 0 No

WHEN A COPY CONSTER IS CALL ? ..

Answer / muthu_tek

emp e;//default constr
emp e(10);//paramatrisized constr
emp e(e1);//copy constr

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More OOPS Interview Questions

How do you define a class in oop?

0 Answers  


how to find the correct email address format by using the programe?

1 Answers   Accenture,


How to execute business logic for only once ..?even though user clicks submit button multiple times by mistake..? (i disabled JavaScript)

1 Answers  


What is Inheritance, Multiple Inheritance, Shared and Repeatable Inheritance?

4 Answers   Accenture, L&T,


what is the difference between inter class and abstract class...?

0 Answers  






Difference between realloc() and free?

9 Answers   HP,


OOP'S advantages of inheritance include:

1 Answers   Infosys,


Why do while loop is used?

0 Answers  


Name an advantage of linked list over array?

11 Answers   IBM, Infosys,


what is graphics

0 Answers  


#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

0 Answers  


why function overloading is not called as pure polymorphism?

2 Answers  


Categories