Upload
hitesh-wagle
View
233
Download
4
Embed Size (px)
Citation preview
Prepared By : Rakesh Kumar
Constructor & Destructor
Constructor : it is a special member function of class with the following unique features1. It has the same name as the name of the class they belongs to2. it has no return type3. it is defined in public visibility mode4. it is automatically called at the time of object creation/declaration5. Constructor function can be overloaded
Example of constructor function#include<iostream>#include<conio.h>using namespace std;class student{ public: int roll=10; char name[30]="ramji";
student() { roll =10; strcpy(name,"ramji"); cout<<"\n I am constructor"; } void input_data() { cout<<"\n Enter roll no :"; cin>>roll; cout<<"\n Enter name :"; cin>>name; } void show_data() { cout<<"\n Roll no :"<<roll; cout<<"\n Name :"<<name; }}; int main() { student s; getch(); return 0; }
Example of parameterized constructor#include<iostream>#include<conio.h>using namespace std;class student{ int roll; char name[30]; public: student(int x, char y[]) { roll =x; strcpy(name,y); } void input_data()
D.A.V. Centenary Public School Chander Nagar
Prepared By : Rakesh Kumar { cout<<"\n Enter roll no :"; cin>>roll; cout<<"\n Enter name :"; cin>>name; } void show_data() { cout<<"\n Roll no :"<<roll; cout<<"\n Name :"<<name; }}; int main() { //student s(10,"ramji"); //implicit call //s.show_data(); student s1=student(10,"ramji"); s1.show_data(); getch(); return 0; }
Example of parameterized constructor function with default values/* parametrized constructor */#include<iostream>#include<conio.h>using namespace std;class student{ int roll; char name[30]; public: student(int x=10, char y[]="rakesh") // default parameter { roll =x; strcpy(name,y); } void input_data() { cout<<"\n Enter roll no :"; cin>>roll; cout<<"\n Enter name :"; cin>>name; } void show_data() { cout<<"\n Roll no :"<<roll; cout<<"\n Name :"<<name; }}; int main() { student s; s.show_data(); getch(); return 0; }
Example of constructor overloading#include<iostream>#include<conio.h>using namespace std;class student{
D.A.V. Centenary Public School Chander Nagar
Prepared By : Rakesh Kumar int roll; char name[30]; public: student(int x, char y[]) // parameterized constructor { roll =x; strcpy(name,y); } student() // normal constructor { roll =100; strcpy(name,"Rakesh Kumar"); } void input_data() { cout<<"\n Enter roll no :"; cin>>roll; cout<<"\n Enter name :"; cin>>name; } void show_data() { cout<<"\n Roll no :"<<roll; cout<<"\n Name :"<<name; }}; int main() { student s(10,"sandep"); s.show_data(); getch(); return 0; }
Copy Constructor: is an overloaded constructor function in which (an) object(s) of the same class is /are passed as a reference parameter(s). it is used when an object ‘s data value is related to is initialized using using another object’s data values of the same class. In the example below the values of data member of object Q are dependent on the value of the data member of the object P and the data members.Example of copy constructor/* parametrized constructor */#include<iostream>#include<conio.h>using namespace std;class student{ int roll; char name[30]; public: student() // default constrcutor { roll =10; strcpy(name,"rakesh"); } student( student &O) // copy constrcutor { roll =O.roll; strcpy(name,O.name); } void input_data() { cout<<"\n Enter roll no :"; cin>>roll;
D.A.V. Centenary Public School Chander Nagar
Prepared By : Rakesh Kumar cout<<"\n Enter name :"; cin>>name; } void show_data() { cout<<"\n Roll no :"<<roll; cout<<"\n Name :"<<name; }}; int main() { student s; // default constr s.show_data(); student A(s); // copy constructor A.show_data(); getch(); return 0; }
Destructor : it is also a special member function of a class with the following unique features1. it has the same name as the name of the class it belongs to. the only difference with
constructor is it has a prefix (~) tilde.2. It is automatically invoked when the scope of the object is over.3. it is defined inside the public visibility mode.4. it has no return type value
Example of Destructor #include<iostream>#include<conio.h>using namespace std;class xyz { public: xyz() { cout<<"\n Constructor "; } ~xyz() // desctrot -- tilde { cout<<"\n Destructor "; } };int main() { { xyz B; } getch(); return 0; }
D.A.V. Centenary Public School Chander Nagar