Constructor Overloading

Constructor Overloading 

Proclaiming numerous constructors with changing number of contentions or kinds of contentions is known as constructor over-burdening. All the over-burden constructors will have a similar name as that of the class. Following project exhibits constructor over-burdening: 
Constructor Overloading in C++
#include <iostream> utilizing namespace sexually transmitted disease; class Student { private: string name; string regdno; int age; string branch; public: Understudy() { cout<<"Default understudy constructor is invoked"<<endl; } Student(int age) { cout<<"Student constructor with one boundary is invoked"<<endl; } Student(string n, string reg, int a, string br) { cout<<"Student constructor with four boundaries is invoked"<<endl; } }; int primary() { Understudy s1; Understudy s2(20); Understudy s3("Mahesh", "501", 20, "CSE"); bring 0 back; } Yield for the above program is as per the following: Default understudy constructor is conjured Understudy constructor with one boundary is conjured Understudy constructor with four boundaries is conjured Destructor A destructor is a unique part work that is summoned naturally when the article leaves scope and is utilized to perform tidy up activities. Following are the qualities of a destructor: Name of the destructor is same as the class name. Be that as it may, destructor name is gone before by the tilde (~) image. A destructor is considered when an item leaves scope. A destructor is likewise considered when the developer calls erase administrator on an article. Like a constructor, destructor is likewise announced in the public segment of a class. The request for summoning a destructor is the opposite of conjuring a constructor. Destructors don't take any contentions and thus can't be over-burden. A destructor doesn't restore a worth. The location of a destructor can't be gotten to in a program. An article with a constructor or a destructor can't be utilized as individuals from an association. Destructor can't be acquired. In contrast to constructors, destructors can be virtual. Linguistic structure for making a destructor is as demonstrated as follows: ~ClassName( ) { /Body of destructor } Following project exhibits a destructor: #include <iostream> utilizing namespace sexually transmitted disease; class Student { private: string name; string regdno; int age; string branch; public: Student(string n) { regdno = n; cout<<"Memory dispensed for understudy "<<regdno<<endl; } ~Student() { cout<<"Memory deallocated for understudy "<<regdno<<endl; } }; int fundamental() { Understudy s1("501"); Understudy s2("502"); Understudy s3("503"); bring 0 back; } Yield for the above program is as per the following: Memory dispensed for understudy 501 Memory dispensed for understudy 502 Memory dispensed for understudy 503 Memory deallocated for understudy 503 Memory deallocated for understudy 502 Memory deallocated for understudy 501 Note: If a destructor isn't characterized by the developer, C++ compiler will make an inline public destructor consequently. Constructors and Destructors Express Invocation Constructors and destructors can be conjured (called) expressly from the primary() work. A constructor can be conjured as demonstrated as follows: ClassName(params-list ); For instance, constructor of Student class can be called as demonstrated as follows: Understudy( ); If there should arise an occurrence of a destructor in c++, we have to compose the class name alongside the destructor name as follows: object_name.ClassName::~ClassName( ); or on the other hand object_name.~ClassName( ); For instance, destructor of Student class can be called as follows: s1.~Student( ); A destructor can be called from a constructor as follows: class Student { ... Understudy( ) { this->Student::~Student( ); /Call to destructor ... } ... }; Likewise, a constructor can be called from a destructor as follows: class Student { ... ~Student( ) { Understudy( ); /Call to constructor ... } ... }; Private Constructor and Destructor  

In some cases, developers might need to forestall direct admittance to a constructor or destructor. In such cases, they can be made private. Admittance to a private constructor or destructor can be conceded through open part works or through a companion work. 

Also visit: Data abstraction in c ++

Comments