conversions/between-user-defined-types/prog1.cc - Illustration of conversions. | Lecture 4 - slide 24 : 40 Program 7 |
#include <iostream> #include <string> #include "point1.h" using namespace std; int main(){ Point p1, p2; // Both (0, 7) Tripple t1; // (0, 0, 0) cout << "t1: " << t1 << endl; // [0, 0, 0] t1 = p2; // Tripple constructor on Point is used. // p2 = (0,7) is copied into the constructor, using Point copy constructor: (1,9) // This point is passed into the Tripple(Point) constructor. t1 becomes (1,9,0). p1 = t1; // Tripple t1 = (1,9,0) converted to Point using conversion operator Point in // class Tripple: (1, 9). // This point is - surprise - converted to double via the double conversion operator // in class Point: 10 // This double is converted to a Point using Point(double) constructor // (10, 20). // In summary: [1,9,0] -> (1,9) -> 10 -> (10,20). cout << "t1: " << t1 << endl; // [1, 9, 0] cout << "p1: " << p1 << endl; // (10, 20) }