Total members 11889 |It is currently Thu Mar 28, 2024 4:53 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What are Constructors and Destroyers(Destructors) and how it is used in C++?
----------------------------------------------------------------


Constructors from a conceptual level enable the creation of an object of a given class- a constructor is a method of a class with no return parameters. Constructors also enable the initialization of any variables of a class; you can supply input parameters to constructors. Constructors can have access modifiers- Private, Protected and Public.

A constructor gets called automatically when you create an object of a class; if you don't define your own constructor, by default a constructor is called which is the "Default Constructor". A class can have more than one constructor. It has the same name as the class itself.

Destructors are again methods of a class that enable the destruction of an object after its use; destructors also provide the means of memory clean up after usage. Destructors are always called in the reverse order of Constructors. There can only be one destructor per class and they neither take an input parameter nor do they return anything. Destructors need not be called explicitly.

cpp code
class Vehicle
{
Int Registration = 0;
public:
Vehicle(int regis) // constructor
{
Registration = regis;
}
Virtual void GetRegistration() = 0;
~Vehicle() // destructor
{
// Set things to null and free memory
}
};




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : What are Constructors and Destructors?
 Class constructors     -  



Topic Tags

C++ OOP
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com