constructors/point-constructors-problems.cc - Illustration of legal and illegal member initializers - constructor chaining is not supported. | Lecture 4 - slide 6 : 41 Program 6 |
#include <cmath> #include <iostream> #include "point.h" Point::Point(double x_coord, double y_coord): x(x_coord), y(y_coord){ } Point::Point(): x(0.0), y(0.0){ } Point::Point(double x_coord): Point(x_coord, 0.0) { // Not allowed in older C++, but legal i C++11. // error: type Point is not a direct base of 'Point' } Point::Point(Point& p): this(p.x + 1.0, p.y + 2.0){ // this not allowed here in C++ (not even i C++ 11). } double Point::getx () const{ return x; } double Point::gety () const{ return y; } std::ostream& operator<<(std::ostream& s, const Point& p){ return s << "(" << p.getx() << "," << p.gety() << ")" ; }