Thursday 16 March 2017

// Use of defualt parameters in constructor 
#include<iostream>
using namespace std;

class Complex
{
int real;
int img;
public:
Complex(int r=0, int i=0) // parameterized Constructor with default parameters
{
real=r;
img=i;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};
int main()
{
Complex c1,c2(10),c3(10,20); //Correct
// Case c1: Compiler will call Parameterized Constructor with default value of r and i as 0 and 0 respectively
// Case c2: Compiler will call the same Constructor with value of r as 10 and i as 0(i is default in this case)
// Case c3: Compiler will call the same Constructor with value of r as 10 and i as 20
c1.putdata();
c2.putdata();
c3.putdata();
}

No comments:

Post a Comment