#include #include "Property.h" #include "House.h" #include "Land.h" using namespace std; using namespace ESTATE; void cast_operators() { double b = 13.4; int a = static_cast(b); cout << "Casting double to integer: " << b << " -> " << a << endl; int land_price = 300000; int land_size = 10000; int house_price = 600000; int house_size = 2000; Land *l = new Land(land_price,land_size); House *h = new House(house_price,house_size,land_size); Land *l2; House *h2; cout << "Correct values are \n"; cout << l->toString() << endl; cout << h->toString() << endl; cout << "-----------------\n"; l2 = static_cast(h); if ( l2 ) { cout << "Experiment 1: WORKS " << l2->toString() << endl; } else { cout << "Experiment 1: ERROR " << endl; } cout << "-----------------\n"; h2 = static_cast(l); if ( h2 ) { cout << "Experiment 2: WORKS but shouldn't! " << h2->toString() << endl; } else { cout << "Experiment 2: ERROR " << endl; } cout << "-----------------\n"; l2 = dynamic_cast(h); if ( l2 ) { cout << "Experiment 3: WORKS " << l2->toString() << endl; } else { cout << "Experiment 3: ERROR " << endl; } cout << "-----------------\n"; h2 = dynamic_cast(l); if ( h2 ) { cout << "Experiment 4: WORKS but shouldn't! " << h2->toString() << endl; } else { cout << "Experiment 4: ERROR" << endl; } cout << "-----------------\n"; h2 = dynamic_cast(l2); if ( h2 ) { cout << "Experiment 5: WORKS " << h2->toString() << endl; } else { cout << "Experiment 5: ERROR" << endl; } delete l; delete h; } void overriding_catch() { int land_size = 10000; int house_price = 600000; int house_size = 2000; House *h = new House(house_price,house_size,land_size); cout << "-----------------\n"; cout << "House, print yourself: " << h->toString() << endl; Land *l = h; // Implicit cast // Which toString() function do we want to use? // The one in class Land or the one in class House? cout << "Land, print yourself: " << l->toString() << endl; cout << "Land, print yourself again: " << l->toString2() << endl; delete l; } int main() { // Part 1: See last lecture // Part 2 (estate.cast-examples.output.txt) cast_operators(); // Part 3 (estate.virtual-function-examples.output.txt) overriding_catch(); // Part 4 (estate.abstract-classes-examples.output.txt) int land_size = 10000; int house_price = 600000; int house_size = 2000; House *h = new House(house_price,house_size,land_size); cout << "Value is " << h->getValue() << endl; House *h2 = new House(2*house_price,house_size,land_size); cout << "Comparison yields: " << h->compare(*h2) << endl; delete h; delete h2; // The line below no longer compile once we add the pure // virtual function getValue to Property //Property *p = new Property(); return 0; }