Showing posts with label Constructor and Destructor. Show all posts
Showing posts with label Constructor and Destructor. Show all posts

Wednesday, 25 January 2012

Program Of Destructor In C++

#include<iostream.h>
#include<conio.h>

class test
{
private:
int a;
public:
test()              // constructor (member initialize)
{
cout<<"\n  constructor fired";
}
 ~test()           //  Destructor ( ~ tiled) realese memory
{
cout ("\n Destructor fired";
}
};
main()
{
clrscr();
test t1;
getch();
}

----------------------------------------------------------------



In object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources(which includes closing database connections, releasing network resources, relinquishing resource locks, etc.) which were acquired by the object along its life cycle and unlink it from other objects or resources invalidating any references in the process. The use of destructors is key to the concept of Resource Acquisition Is Initialization (RAII).

In binary programs compiled with the GNU C Compiler, special table sections called .dtors are made for destructors. This table contains an array of addresses to the relevant functions that are called when the main() function exits.[1] A function can be declared as a destructor function by definining the destructor attribute __attribute__ ((destructor)) for a static function

In a language with an automatic garbage collection mechanism, it would be difficult to deterministically ensure the invocation of a destructor, and hence these languages are generally considered unsuitable for RAII. In such languages, unlinking an object from existing resources must be done by an explicit call of an appropriate function (usually called Dispose()). This method is also recommended for freeing resources rather than using finalizers for that.

Destructor syntax
C++ has the naming convention in which destructors have the same name as the class of which they are associated with, but prefixed with a tilde (~).
In Object Pascal, destructors have the keyword "destructor" and can have user-defined names (but are mostly called "Destroy").
In Perl, the destructor method is named DESTROY.
In Moose object system for Perl, the destructor method is named DEMOLISH.
In Objective-C, the destructor method is named "dealloc".
In PHP 5, the destructor method is named "__destruct". There were no destructors in previous versions of PHP.


In C++
The destructor has the same name as the class, but with a tilde (~) in front of it. If the object was created as an automatic variable, its destructor is automatically called when it goes out of scope. If the object was created with a new expression, then its destructor is called when the delete operator is applied to a pointer to the object. Usually that operation occurs within another destructor, typically the destructor of a smart pointer object.

In inheritance hierarchies, the declaration of a virtual destructor in the base class ensures that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class. Objects that may be deleted in this way need to inherit a virtual destructor.
A destructor should never throw an exception.







Program Of Constructor In C++


#include<iostream.h>
#include<conio.h>
class cal
{
private:
int a;
public:
 cal(int x)
{
a=x;
}
void increament()
{
a=a+1;
}
void display()
{
cout<<"\n  Value is"<<a;
}
};
main()
{
clrscr();
cal c1(3);
c1.display();
c1.increament();
c1.increament();
c1.increament();
c1.display();
getch();
}


------------------------------------------------------------------------------------------------------------------------------------



In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.

A constructor resembles an instance method, but it differs from a method in that it never has an explicit return-type, it is not inherited (though many languages provide access to the superclass's constructor, for example through the super keyword in Java), and it usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a valid state. Immutable objects must be initialized in a constructor.

Programmers can also use the term constructor to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article.[dubious – discuss] For more information, see algebraic data type.

Thursday, 19 January 2012

Program Of Constructor & Destructor In C++

              #include<iostream.h>
#include<conio.h>
class add
{
public:
add()            //Constructor
{
cout<<"\n Constructor Fired";
}
~add()        //Destructor
{
cout<<"\n Destructor Fired";
}


};
main()
{
clrscr();
int i;
add a1;
getch();
}

-----------------------------------------------------------------------------------

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example:class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };


A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile orstatic. A destructor can be declared virtual or pure virtual.

If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.

The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor's class type. Suppose a class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A: A::~A() { }


The compiler first implicitly defines the implicitly declared destructors of the base classes and nonstatic data members of a class A before defining the implicitly declared destructor of A

A destructor of a class A is trivial if all the following are true:
It is implicitly defined
All the direct base classes of A have trivial destructors
The classes of all the nonstatic data members of A have trivial destructors

If any of the above are false, then the destructor is nontrivial.

A union member cannot be of a class type that has a nontrivial destructor.

Class members that are class types can have their own destructors. Both base and derived classes can have destructors, although destructors are not inherited. If a base class A or a member of A has a destructor, and a class derived from A does not declare a destructor, a default destructor is generated.

The default destructor calls the destructors of the base class and members of the derived class.

The destructors of base classes and members are called in the reverse order of the completion of their constructor:
The destructor for a class object is called before destructors for members and bases are called.
Destructors for nonstatic members are called before destructors for base classes are called.
Destructors for nonvirtual base classes are called before destructors for virtual base classes are called.



view source::-- http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr380.htm