Defining more than one constructor within class by changing -->Number of parameters -->Types of parameters -->Order of parameters is called Constructor Overloading in C++ Constructor is overloaded in order to extend functionality of existing constructor
# include<iostream.h> class stock { int*a; int size; int top; public: stack(); stack(int); void push(int); void pop(); } stack :: stack() { top=-1; size=5; q=new int[size]; } stack :: stack(int s) { size=s; top=-1; a=new int[size]; void stack :: push (int e) { if(top)>size-1; cout<<”\n stack is full”; else a[++top]=e; } void stack :: pop() { if(top<0) cout<<”\n stack is empty”; else cout<<”\n element is”<<a[top---]; } void main() { stack stack1; stack stack2(3); stack1.push(0); stack1.push(20); stack1.pop(); stack2.push(60); stack2.push(70); stack2.push(); } o/p : void push(int e) { if(top>>size-1) else a[++top]=e; } # include<iostream.h> #inlude<string.h> class string { char *s; int size; public : string(); string(int); void read_string(); void print_string(); }; string:: string() { size=10; s=new char[size]; } string:: string (int s1) { size=s1; s=new char[size]; } void string :: read-string() { cout<<”\n input any string”; cin>>s; } void string:: print_string() { cout<<”\n string is”<<s; } void main() { string string1; string string2[20]; string1.read_string(); string2.print_string(); string2.print_string(); }
Copy Constructor Syntax constructor-name(class-name & red-name) { staments; { }
Copy Constructor Example # include<iostream.h> class date { int dd, MM,YY; public: data(int,int,int); date(date&) //(copy constructor) void point_date(); }; date:: date(int d, int M, int Y) { dd=d; MM=M; YY=Y; } date:: date(date &d) { dd=d.dd; mm=d.MM; YY=d.YY; } void date:: print_date() { cout<<”]n” <<d<<”\t”<<mm<<”\t”<<yy; } void main() { date dob(10,10,2014); date cdob(dob); dob.print_date(); colob.print_date(); }
# include<iostream.h> # include<string.h> class strudent { int rno; char name[10]; public : student(int,char*); student(student &); void print_student(); }; student::student(int r,char *n) { rno=r; strcopy(name,s.name); } void student::print_student() { count<<”\n rollno”<<rno; cout<<”\n name”<<name; } void main() { student1 stud1(101,”RONO”); student1 stud2(stud1); stud1.print_student(); stud2.print_student();
Destructor is a special member function This member function get executed before destroying object.
Destructor name is same as class name and it is prefix with ~ operator It is without any return type not even void It is not having any parameters/ arguments A class contain only one destructor.
~destructor-name() { statements; }
# include <iostream.h> class alpha { int x; public : alpha() { cout<<”\n inside construtor”; } ~alpha() { cout<<”\n inside destructor”; } }; void main() { alpha alpha1; } o/p : inside constructor(creating object) inside Destructor(Deleting object) Eg: #include <iostream.h> class array { int *a; int size; public : array(int s) { size=5; a=new int[size]; } void read_elements() { cout<<”\n input elements”; for(int i=0;i<size;i++) cin>>a[i]; } void print_elements() { cout<<”\n elements are \n”; for(int i=0;i<size;i++) cout<<endl<<a[i]; } ~array() { cout<<”\n inside destructor”; delete a; void main() { array array1(5); array1.read_elements(); array1.print_elements(); }
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.