The default constructor is the constructor without parameters
class Complex
{
private:
int real;
int image;
public:
void display();
Complex(){ //This is the default constructor with no parameters and is used to initialize C1
real=20;
image=40;
};
Complex(int x,int y):a(x),b(y){ //This is the constructor passed in parameters - an overload of the constructor is used to initialize C2
}
Complex(Complex &c) {//Another copy/copy constructor initialization C3
Real=c.Real;
Image=c.Image;
}
~Complex(){};
}
void display()
{
cout
}
void main(){
Complex c1();
Complex c2(0.0);
Complex c3(c1);
c1.display();
c2.display();
c3.display();
}
class CustomerData : public PersonData
{
private:
int customerNumber;
bool mailingList;
};
This class inherits PersonData, but the base class is not the default constructor, so you must explicitly declare the constructor in the inherited class and construct the base class object
class CustomerData : public PersonData
{
public:
CustomerData(string s,string f):PersonData(s,f){}
private:
int customerNumber;
bool mailingList;
};
The above is the detailed content of When encountering C problems, the default constructor initialization method is not familiar.. For more information, please follow other related articles on the PHP Chinese website!