c++ - Constructor definition with a member initializer list -
i have code given me finish few things unfamiliar me. first of all, what's point of fellow member initializer in first function?
class circle { private: double r; public: circle(double radius) : r(radius) {} double get_perimeter() { homecoming 2*pi*r; } double get_radius() { homecoming get_perimeter()/2*pi; } };
and in main()
function:
int main() { double radius; cin >> radius; circle c(radius); cout << c.get_radius << endl; homecoming 0; }
the circle c(radius);
line makes no sense me. can narrate me few lines addressed?
circle(double radius) : r(radius) {}
the point of fellow member initializer, r(radius)
, initialize fellow member r
, of course! line defines constructor of circle
. constructor takes argument of type double
, initialises fellow member r
value.
circle c(radius);
this declares variable c
type circle
, passes radius
constructor. compare std::string str("foo");
or int x(5);
- these of same form.
c++
No comments:
Post a Comment