Saturday 31 January 2015

constructor overloading in c++

Structure of the Problem Requirements

Constructor is used to initialize the data elements of the class. In this problem a constructor is overloaded three times and save the CMS numbers of the different student.

Source Code

#include<iostream>
#include<conio.h>
using namespace std;
class CMS
{
 private:
int cmsNum,regNum;
 public:
CMS() //default constructor
{
cmsNum=regNum=0; //set the value of variables in default constructor
}
CMS(int x) //one arguments constructor
{
cmsNum=regNum=x;
}
CMS(int x,int y)//Two arguments constructor
{
cmsNum = x;
regNum = y;
}
void Display() //Values display function
{
cout<<"Cms Number : "<<cmsNum<<endl<<endl;
cout<<"Reg Number : "<<regNum<<endl<<endl;
}
};
int main()
{
cout<<"\n\n\t ********* Constructor Overloading ************* \n\n";
CMS c(8651);
CMS c1(8661);
CMS c2(8939,8649);
c.Display();
c1.Display();
c2.Display();
return 0;
}

Output of the Program

constructor overloading in c++

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment