Wednesday, November 17, 2010
1. Does a class provide default Copy Constructor?
Yes! The default copy constructor is being provided automatically by the compiler if not implemented separately. In this, it puts the code for coping the data members and other variables that stay in stack. If something is being created by malloc or new in the heap, those are not being copied by the default copy constructor provide by the compiler.
2. What does a default Copy Constructor do?
A default copy constructor is being provided by the compiler, when an attempt to copy an existing object is made. In this case the control goes to the default copy constructor. It generates a new object, and makes the values of data members of the new object which are in the stack, same as the parent object. It doesn’t copy the variables that are created in the heap. Simply speaking, a compiler supplied default copy constructor doesn’t take care of the things in an object, that are being created using malloc/calloc or new.
3. Can a program have a virtual constructor?
Generally we don’t need an overridable constructor. So constructor should not be declared as virtual. But in a class, we can make a static method, which will call the private/protected constructor and create an object. In that case the constructor is called as virtual constructor.
4. When does a programmer need to implement his own copy constructor?
Though compiler automatically provides the default copy constructor, sometime a programmer needs to implement his own copy constructor. We can take up a case here:
In the default constructor some memory allocation has been done for few data members and hence those will be created in the heap. In the destructor corresponding de-allocation code is there. Now if an attempt to copy an object is made, it won’t call the default constructor but it will call the default copy constructor which will copy the data member variable from stack and copy the pointers but won’t allocate any new memory space for the new copied object. So same pointers will exist in both the parent and copied object. This will not only create a great ambiguity but runtime error will occur when attempts will be made to delete both the objects. First object will get deleted properly. When it will try to delete the other object, the common pointer won’t exist and runtime error will come. Even if user stops the application it will try to delete all the objects in the memory and runtime error will occur. In a single word, copy constructor is needed to be implemented independently, when the parent object has some allocated memory in heap for some of its data member and that gets deleted by destructor. Copy constructor should consist that much of code for memory allocation for the newly created object.Apart from this, programmer can implement his own copy constructor to copy any other things, which he wants specifically....................
5. 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.
Logically thinking, if we can consider the same as copy constructor also, big confusion will come. If we pass an object as a function parameter, by value, it will get copied first and then be passed to the function. At the time of getting copied it should call the copy constructor. So in this case, if we consider the above described constructor as copy constructor, at the very beginning of the function call, it wil attempt to copy the object and hence again call the same function and hence will go towards an infinite loop. This will probably be ended up with an unexpected stack overflow.
It will give compiler error if an object of the same class is being passed to a constructor, but it doesn’t give error if more than one different objects are being passed to the constructor . In this case it doesn’t treat it as copy constructor, but a normal overloaded constructor.
6. What is the return parameter of a constructor and why?
Constructor in never being called directly. It is being called automatically by the compiler when an object in being created (or copied). Hence it can’t return any parameter. Logically thinking, if it would have been made to return a parameter, how do we put the code to create an object?
7. Are the “default constructor” and “constructor with default parameter” same?
Default constructor is a constructor, which can be called with no argument. So a constructor with all the parameters as default argument can be called as default constructor. A constructor with one or more default parameters (but not all the parameters) can be called “constructor with default parameter” but that won’t be the default constructor.
If a constructor with no argument and a constructor with all default arguments are being implemented then object-creation will generate an ambiguity regarding which constructor is to be called.
Education News And Updates
- Abbreviations (1)
- Admissions (45)
- AFCAT (5)
- After 12th (HSC) (4)
- Agriculture (14)
- AICTE (5)
- AIEEE (15)
- AIMA (2)
- AIPMT (3)
- Air Force (6)
- Android (13)
- Answers (6)
- Application Details (59)
- Architecture (5)
- Articles (24)
- asistant (1)
- B.Ed (7)
- B.SC (5)
- Bank Exam (57)
- Best Deals (3)
- Books And Authors (3)
- business (1)
- Career (33)
- CAT (5)
- CBSE (4)
- CDS (6)
- CET (5)
- cisco update (1)
- CLAT (1)
- Clinical research (1)
- CMAT (1)
- College Festivals (2)
- College Projects (3)
- Colleges (11)
- Colleges and Institutes (51)
- Communication Skill (1)
- Competitive Exam Help (227)
- Computer Awareness (3)
- Computers (27)
- Courses (13)
- CPMT (2)
- CTET (1)
- Current Affair (10)
- CWE (5)
- CyberLink DVD Suite (1)
- DBMS (6)
- Diploma (7)
- DOS (1)
- Education News (189)
- eligibility (7)
- email marketing (1)
- Employment news (1)
- Employment Tips (61)
- Engineering (50)
- Entertainment (52)
- Entrance Exam dates (118)
- Environment (1)
- Exam Alerts (28)
- Exam Details (189)
- Exam Results (21)
- Exam Time Table (3)
- Facebook Tricks (12)
- Fashion Designing (1)
- form details (4)
- Forthcoming Competitive Exams (2)
- Free Books (13)
- Free Download (20)
- free trial (3)
- Friends (2)
- Game Designer/Developer (1)
- Game Reviews (1)
- GATE (6)
- General Knowledge (81)
- General talk (104)
- GMAT (2)
- Google (8)
- Government job (10)
- Graduation (1)
- Graphics Designer (1)
- GRE (1)
- Great People (1)
- Greetings (1)
- Guitar Codes (1)
- Health Tip (12)
- Heigher Studies (4)
- Hotel Management (1)
- How to (14)
- HSC (4)
- IAS Exam (3)
- IIT (2)
- Important Facts (2)
- India (1)
- Indian Army (6)
- Indian History (3)
- Inspiration (1)
- Intelligent Quotes (3)
- Interview Preparation Tips (10)
- ipad (2)
- iphone (5)
- IT Jobs (1)
- Language (1)
- Law (3)
- Linux (2)
- Lovely Quotes (19)
- Lyrics(Songs) (24)
- M.SC (2)
- Maharashtra State Board (3)
- Marathi (3)
- MAT (2)
- MBA Entrance Exams (36)
- MBBS (3)
- MCA Entrance Exams (16)
- MCQ (3)
- media player (1)
- Medical (23)
- Mobile Specifications (50)
- Music (4)
- NAMT (1)
- NATA (2)
- National Eligibility Test (1)
- Net-Security (10)
- New Mobile Phones (50)
- New Tech_Education (130)
- Nokia (1)
- Notification (100)
- Nursing (1)
- online admission (3)
- online meeting (1)
- OS. (4)
- PCS Exam (1)
- Ph.D (16)
- physiotherapy (1)
- PL/SQL (4)
- Pocket Money (9)
- Poetry (6)
- Postgraduate (3)
- PowerDirector (1)
- PowerDVD (1)
- Question Paper (83)
- Quiz (9)
- Recruitment (15)
- selection procedure (3)
- Self Improvement (8)
- short service (1)
- SMS Of The Day (5)
- Software_Tools (23)
- Solved Papers (43)
- Space Science (1)
- SSC (13)
- Study Tips (33)
- syllabus (8)
- Tamilnadu (1)
- Tech News (78)
- Telecom News (21)
- Templates (1)
- Tips For Online Money (16)
- Today's Puzzle (31)
- Today's Quote (32)
- trial (1)
- Tutorial (9)
- Twitter (1)
- University (13)
- UNIX (2)
- UNO (7)
- UPSC Exams (16)
- Videos (12)
- Wallpapers (3)
- Web Design And Myths (8)
- What's New (5)
0 Comments:
Post a Comment
comment