constructors/reordered/prog3.cc - Use of constructors - with local variables of class type. | Lecture 4 - slide 8 : 40 Program 3 |
//Point(): (0,7) Point(double x): (x, 20) Point(double x, double y): (x, y) Point(Point& p) (p.x+1, p.y+2) //Reveal... #include <iostream> #include "point.h" using namespace std; int f(){ Point p; cout << "Point p: " << p << endl; // (0,7) Point r(11.0, 12.0); cout << "Point r: " << r << endl; // (11,12) Point q; q = r; // default assignment - bit-wise copying, no use of copy constructor cout << "Point q: " << q << endl; // (11,12) Point s(p); // The copy constructor used. p is (0,7) at this location. cout << "Point s: " << s << endl; // (1,9) Point t(3.0); // Point(double) used cout << "Point t: " << t << endl; // (3,20) Point u = t; // Another way to use the copy constructor, via an initializer. t is (3,20). cout << "Point u: " << u << endl; // (4,22) Point v{t}; // Same. t is (3,20). cout << "Point v: " << v << endl; // (4,22) Point ap[10]; // The default constructor used 10 times for(int i = 0; i < 10; i++) cout << "ap[" << i << // (0,7) ten times "]: " << ap[i] << endl; } int main(){ f(); }