# CPP/Classes/PureVirtualFunction

< CPP

## PureVirtualFunction.cpp

```#include <iostream>
#include <limits>

class Point3{

public:
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
Point() x(),y(),z(){};
private:
double x,y,z;
virtual void print(){ cout << "(" << x << "," << y << "," << z << ")" << endl; }
};

class Coordinate: Point3
{
void point(){
cout << "Coordinate (" << x << "," << y << "," << z << ")" << endl;
}
};

int main(){
Point3 p1 = new Point3(1,2,3);
Coordinate c1 = new Coordinate();

//polymorphism
Point3 *p = &c1;
p.print(); //output "Coordinate (0,0,0)"

p = &p1;
p.print(); //output "(1,2,3)"

return 0;
}```

## Virtual Functions & Abstract Classes

```#include <iostream>

class Vehicle
{
public:
/*
* Here we declare a Pure Virtual Function
* This class has now become an abstract class &
* it's instances cannot be created. Only pointers.
*
*/
virtual void SeeMeGo()=0;
};

/* Some Derived Classes */
class Car : public Vehicle
{
public:
void SeeMeGo()
{
std:cout<<"I'm running at 50mph.\n";
}
};

class Bike : public Vehicle
{
public:
void SeeMeGo()
{
std:cout<<"I'm whooshing at 80mph.\n";
}
};

class Truck : public Vehicle
{
public:
void SeeMeGo()
{
std:cout<<"I'm crawling at 20mph.\n";
}
};

int main(int argc, char *argv[])
{
//Create a Pointer to Abstract Class Vehicle
Vehicle *myVehicle;

//Create Some Objects
Car   myCar;
Bike  myBike;
Truck myTruck;

//Call Functions
myVehicle = &myCar;
myVehicle->SeeMeGo();

myVehicle = &myBike;
myVehicle->SeeMeGo();

myVehicle = &myTruck;
myVehicle->SeeMeGo();

return 1;
}```