/* 今天学习 构造函数这一个知识点 */
#include<iostream>
#include<cmath>
using namespace std;
class Constructor
{
private:
float xCenter;
float yCenter;
float Radius;
public:
Constructor(); //构造函数 必须和类名相同,构造函数名字前面不加任何函数类型 void也不行,这就保证了构造函数只能用来初始化对象,而没有任何返回值。
void setXcenter(float x);
void setYcenter(float y);
void setRadius(float r);
void setConstructor(float x, float y, float r);
float distance(Constructor c1);
void printMessage(float d);
};
Constructor::Constructor()
{
xCenter = 0;
yCenter = 0;
Radius = 1;
cout << "构造函数" << endl;
}
void Constructor::setXcenter(float x)
{
xCenter = x;
}
void Constructor::setYcenter(float y)
{
yCenter = y;
}
void Constructor::setRadius(float r)
{
Radius = r;
}
void Constructor::setConstructor(float x, float y, float r)
{
xCenter = x;
yCenter = y;
Radius = r;
}
float Constructor::distance(Constructor c1)
{
float x = c1.xCenter - xCenter;
float y = c1.xCenter - xCenter;
return sqrt(x*x + y*y);
}
void Constructor::printMessage(float d)
{
cout << xCenter << yCenter << Radius << endl;
cout << "两圆之间的距离" << d << endl;
}
int main()
{
float d =0;
Constructor c1; // 定义之后会自动调用构造函数 这就说明的构造函数的作用 用来初始化类的对象
Constructor c2;
c2.setConstructor(1, 1, 0.5);
d = c1.distance(c2);
c1.printMessage(d); //老是忘记打对象名 这怎么能行呢?
return 0;
}
/*C++ 盗墓笔记
构造函数是一种比较特殊的成员函数,用于创建并初始化对象。声明对象时会被编译器自动调用。
1:构造函数的访问权限必须为公有
2:构造函数名 和类名相同
3:构造函数没有返回值
4:构造函数可以带参数,用于初始化成员变量
构造函数的分类 :带参数 和不带参数
1:类名() 这就是默认构造参数
2:类名(flaot a=2, float b28) 这也是 默认构造函数 在使用的可以不用传递参数 使用默认值进行初始化
3:类名(float a,float b)普通构造函数 必须传递实参 否则报错
*/