Constructor Overloading in C++

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 on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment