| conversions/prog1.cc - Use implicit of the conversions. | Lecture 4 - slide 24 : 40 Program 3 |
// Illustration of implicit conversions between Point and double.
// Point(): (0,7) Point(x): (x, 20) Point(x,y): (x,y) Point(p) = (p.x+1,p.y+2) double(p) = p.x+p.y
#include <iostream>
#include <string>
#include "point1.h"
using namespace std;
int main(){
double d1, d2;
Point p1, p2; // Both (0, 7), not important for
// this example.
p1 = 5.0; // (5, 20) - Using Point(double)
p2 = Point(p1); // (6, 22) - Using copy constructor. Default assignment is used.
cout << "p1: " << p1 << endl; // (5, 20)
cout << "p2: " << p2 << endl; // (6, 22)
d1 = p1; // 25 - Using conversion operator double() in Point.
d2 = p1 - p2; // Implicitly converts both p1 and p2 to doubles: 25 and 28.
// Using conversion operator twice.
// 25 - 28 == -3,
cout << "d1: " << d1 << endl; // 25
cout << "d2: " << d2 << endl; // -3
}