/*
time:2020年4月27日21:16:25
where:gfdx
function:构造函数的使用1-与类名相同*/
#include<iostream>
#include<cmath>
using namespace std;
class complex
{
private:
double real;
double imag;
public:
complex(double r, double i)//成员函数:构造函数
{
real = r;
imag = i;
}
double abscomplex()
{
double t;
t = real * real + imag * imag;
return sqrt(t);
}
};
int main()
{
complex A(1.1, 2.2);//定义类complex的对象A时调用构造函数complex
cout << "abs of complex A=" << A.abscomplex() << endl;
return 0;
}
1.构造函数的函数名与类名相同
一般格式为:类名 对象名(实参表)
















