Constructors and destructors are special member functions in C++. Constructors are used to initialize objects and are invoked automatically when an object is created. There are default, parameterized, copy, and dynamic constructors. Destructors are used to destroy objects and free allocated memory, and are called when an object goes out of scope. Destructors name is prefixed with a tilde and never have arguments or return values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
67 views16 pages
Constructors and Destructors Slides
Constructors and destructors are special member functions in C++. Constructors are used to initialize objects and are invoked automatically when an object is created. There are default, parameterized, copy, and dynamic constructors. Destructors are used to destroy objects and free allocated memory, and are called when an object goes out of scope. Destructors name is prefixed with a tilde and never have arguments or return values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
•Constructors and Destructors
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer Constructors A special member function used to initialize the objects of its class. Characteristics of constructors- Its name is the same as the class name. Should be declared in the public section. Invoked automatically when the objects are created. Don't have return types, not even void.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer Types of constructors Default constructors- take zero arguments. Parameterized constructors- take one or more arguments. Copy constructors- declare and initialize an object from another object. Dynamic constructors- allocate sufficient amount of memory while creating objects.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer Default constructors For example class point { int x,y; public: point() // Default constructor defined { x=0; y=0; } --------- }; point p; // Default constructor automatically called and initialize x and y to zero for the object p. If no such constructor is defined in a class, then compiler provides a default constructor. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Parameterized constructors Example- class point { int x, y; public: point() // if we define other constructors then default constructor must be define. { } point(int a, int b); // parameterized constructor { x=a; y=b; } }; point p1(10,20); // parameterized constructor called int num1, num2; cout<<“Enter the values of num1 and num2”<<“\n”; cin>>num1>>num2; point p2(num1, num2); //dynamic initialization of object. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Copy constructors A copy constructor takes a reference to an object of the same class as an argument. Example- class product { int code; public: product(){ } // default constructor product(int x) //parameterized constructor { code=x; } product(product &y) //copy constructor { code=y.code; } //copy the value void display(void) { cout<<code<<“\n”; } }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Copy constructors int main() { product p1(10); // parameterized constructor called product p2(p1); //copy constructor called product p3=p1; //again copy constructor called p1.display(); p2.display(); p3.display(); return 0; } output: 10 10 10
When no copy constructor is defined in the program, the
compiler supplies its own copy constructor. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Dynamic constructors Example- class string { char *name; int length; public: string ( ) //default constructor { length=0; name= new char[length+1]; // one extra for ‘\0’ } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Dynamic constructors string( char *s) //dynamic constructor { length=strlen(s); name=new char[length+1]; strcpy(name,s); } void display(void) { cout<<name<<“\n”; } }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Dynamic constructors int main( ) { string city1(“Ajmer”); //allocate six bytes of memory string city2(“Shri Ganganagar”); //allocate 16 bytes memory city1.display(); city2.display(); return 0; } Output: Ajmer Shri Ganganagar Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Destructors A special member function of the class that is used to destroy the objects that have been created by constructor. Characteristics of destructors: Its name is same as class name but preceded by tilde(~). It never takes any argument and does not return any value. It is invoked implicitly by the compiler upon exit from the program or block or function. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Destructors Use of destructors are to free the allocated memory to objects at run time. So, that the freed memory can be reuse for another program or objects. Memory allocated to an object by new operator in constructor function. Memory de-allocated by delete operator in destructor function.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer Destructors Example- class sample { char *t; public: sample(int length) { t=new char[length]; cout<<“Character array of length” <<length <<“created”; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Destructors ~sample() { delete t; cout<<“\n Memory de-allocated for the character array”; } }; int main() { sample s(10); return 0; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer Destructors Output: Character array of length 10 created Memory de-allocated for the character array