#include<iostream>
using namespace std;
class Complex
{
int real;
int img;
public:
Complex(int r, int i) // parameterized Constructor
{
real=r;
img=i;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};
int main()
{
Complex c1; // Error as no matching function call,
//Reason: Compiler could not find appropriate constructor to call i.e. a constructor with no parameter
//Rules: If no constructor is defined within class defination than compiler can only call default constructor automatically //otherwise not
// Solution: Create Define Default constructor within class defination i.e a constructor with no parameters.
// note: Default constructor is also called parameter less constructor
Complex c2(10,200); //Correct
c1.putdata();
c2.putdata();
}
using namespace std;
class Complex
{
int real;
int img;
public:
Complex(int r, int i) // parameterized Constructor
{
real=r;
img=i;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};
int main()
{
Complex c1; // Error as no matching function call,
//Reason: Compiler could not find appropriate constructor to call i.e. a constructor with no parameter
//Rules: If no constructor is defined within class defination than compiler can only call default constructor automatically //otherwise not
// Solution: Create Define Default constructor within class defination i.e a constructor with no parameters.
// note: Default constructor is also called parameter less constructor
Complex c2(10,200); //Correct
c1.putdata();
c2.putdata();
}
No comments:
Post a Comment