Created
July 13, 2014 20:36
-
-
Save lnrsoft/070e3c56c99c48ac92c4 to your computer and use it in GitHub Desktop.
This simple example consists of an overview of the syntax of Polymorphism and practical use of Virtual Destructor in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This source code written by Roland Ihasz | |
#include <iostream> | |
using namespace std; | |
// base class | |
class GPSCoordinates { | |
protected: | |
double latitude, longitude; | |
string name; | |
public: | |
GPSCoordinates(double la, double lo, string n) { | |
latitude = la; | |
longitude = lo; | |
name = n; | |
} | |
virtual ~GPSCoordinates(){ // Virtual Destructor // Tidying Up... | |
cout<<">> GPSCoordinates-Base Destructor called" << endl; | |
} | |
void display() { | |
cout << name << " GPS Coordinates in Decimal Degrees (WGS84)" | |
<< endl << "Latitude: " << latitude | |
<< ", Longitude: " << longitude << endl; | |
} | |
}; | |
// derive class | |
class Hawaii : public GPSCoordinates { // constructor | |
public: | |
Hawaii (double la, double lo, string n) : | |
GPSCoordinates(la,lo,n) {} | |
virtual ~Hawaii(){ // Virtual Destructor // Tidying Up... | |
cout<<">> Hawaii-Derived destructor called" << endl; | |
} | |
}; | |
class Dublin : public GPSCoordinates { | |
public: | |
Dublin(double latitude, double longitude, string name) : | |
GPSCoordinates(latitude, longitude, name) {} | |
virtual ~Dublin(){ // Virtual Destructor // Tidying Up... | |
cout<<">> Dublin-Derived destructor called" << endl; | |
} | |
}; | |
int main() | |
{ | |
Hawaii loc1(21.3069400, -157.8583300, "Hawaii"); | |
Dublin loc2(53.3439900, -6.2671900, "Dublin"); | |
loc1.display(); | |
loc2.display(); | |
cout << endl << "Tidying Up..." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment