You are on page 1of 4

Constructors & Destructors When an object of a class is created, C++ calls the constructor for that class.

If no constructor is defined, C++ invokes a default constructor, which allocates memory for the object, but doesn't initialize it. A constructor is similar to a member function, but with the following differences.

No return type. No return statement. Always a public member of a class Has Same name as class tag name The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile. Overloading of constructors is possible.

Even if a class is not equipped with a constructor, the compiler will generate code for one, called the implicit default constructor. This will typically call the default constructors for all class members. Constructors are used to create, and can initialize, objects of their class type. Type of Constructors:1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor Default Constructor This constructor has no arguments in it. The default Constructor is also called as the no argument constructor. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have no constructor initializer and a null body. 1. class X 2. { Member function with same name as class 3. private: tag name, public member with no return type- Default Constructor 4. int a,b; 5. public: 6. X(); 7. ... WE can write a constructor inside the 8. }; class as inline or outside the class 9. 10. X::X() 11. { 12. a=0; 13. b=0; 14. }

Parameterized Consructor: - constructors that can take arguments are termed as parameterized constructors. The number of arguments can be greater or equal to one(1). It should nopt have default value. class X { Member function with same name as class private: tag name, public member with parameters Parameterized constructor int a,b; public: X(int i) { a=i;} X(int i,int j) { a=i; b=j;} ... }; class X { private: values int a,b; public: X(int i=10,int j=20) { a=i; b=j; } i will 10 and i will 20 therefore, a will 10 and b will 20 ... }; X X1; i will 40 and j will 20 therefore, a will 40 and b will 20 X X2(40); X X3(50,60); i will 50 and j will 60 therefore, a will 50 and b will 60
A parameterized constructor with default

Copy Constructor:- This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. Copy constructors define the actions performed by the compiler when copying class objects. A copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). This constructor takes one argument or more arguments, also called one argument constructor It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor. If you do not declare a copy constructor for a class A, the compiler will implicitly declare one for you, which will be an inline public member. A copy constructor looks just like a normal constructor that takes a parameter of the class type. The parameter MUST be passed by reference and not by value because A copy constructor is called when a parameter is passed by value. If we pass our cSource parameter by value, it would need to call the copy constructor to do so. But calling the copy constructor again would mean the parameter is passed by value again, requiring another call to the copy constructor. This would result in an

infinite recursion (well, until the stack memory ran out and the program crashed). Fortunately, modern C++ compilers will produce an error class X { private: int a; public: X(int i=0) { a=i; } X(X &XY) { a=XY.a; }

Default Constructor

Copy Constructor

Default constructor will be invoked


};

Copy constructor will be invoked


X X1; X X2=X1; X X3(X2); The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization Constructor overloading:- Two or more constructor in a class with different signatures is called constructor overloading. The constructors can be default, parameterized and copy constructors.

class Point { public: Point(); // parameterless default constructor Point(int new_x, int new_y); // constructor with parameters int getX(); int getY(); private: int x; int y; };

Destructor
Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name. General Syntax of Destructors ~ classname(); The above is the general syntax of a destructor. In the above, the symbol tilde ~ represents a destructor which precedes the name of the class. Some important points about destructors: Destructors take the same name as the class name. Like the constructor, the destructor must also be defined in the public. The destructor must be a public member. The Destructor does not take any argument which means that destructors cannot be overloaded. No return type is specified for destructors. A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. 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. class ABC { private: ... public: Destructor will be invoked when object goes out of ABC() scope. {} ~ ABC() {} }

You might also like