class Circle{
public:
double radius;
Circle() { //Constructors:
radius = 1; //Initialize objects(构造函数:初始化对象)
} //Has the same name as the defining class(与类同名)
//No return value(无返回值)
Circle(double newRadius) { //constructors can be overloaded(可重载)
radius = newRadius; //may be no arguments(可不带参数)
}
double getArea() {
return radius * radius *3.14159;
}
};
A class may be declared without constructors(类里面也可以不声明构造函数)
A no-arg constructor with an empty body is implicitly declared in the class.
(编译器会提供一个带有空函数体的无参构造函数)This constructor,called a default constructor is provided automatically **only if no
constructors are explicitly declared in the class**.
(只有当未明确声明构造函数时,编译器不会提供这个构造函数,并称之为“默认构造函数”)
【Constructing Objects(创建对象)】
Without Arguments;(无参数)
ClassName objectName;
For example;
Circle circle1;// the no-arg constructor
// is invoked
With Arguments;
ClassName objectName(arguments);
For example:
Circle circle2(5.5);