Constructor Overloading – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

Constructor OverloadingConstructor Overloading – Reference Note
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Constructor Overloading
When more than one constructor function is defined in a class, then it is called constructor overloading or use of multiple constructor in a class. It is used to increase the flexibility of a class by having more number of constructors for a single class. Overloading constructors in C++ programming gives us more than one way to initialize objects in a class.

To illustrate the constructor overloading, let us make the following examples:
#include<iostream>
#include<conio.h>
Using namespace std;
class Student
{
int roll;
char name[30];
public:
Student(int x,char y[]) //parametrized constructor
{
roll = x;
strepy(name,y);
}
Student()
{
roll=100;
strepy(name,”y”);
}
void input_data()
{
cout<<”\n Roll no:”<<roll;
cout<<”\n Name :”<<name;
}
};
int main()
{
Student s(10,”z”);
s.show_data();
getch();
return 0;
}

The above C++ program is used to create a constructor student inside the class student. Here in this program, the student constructor has been defined in many ways and the output of the program is student name and his roll no.

(Visited 704 times, 10 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary