Difference between revisions of "CPP/Classes/PureVirtualFunction"
From ProgrammingExamples
< CPP
(→PureVirtualFunction.cpp) |
FirstPerson (Talk | contribs) (→PureVirtualFunction.cpp) |
||
(6 intermediate revisions by one other user not shown) | |||
Line 4: | Line 4: | ||
#include <limits> | #include <limits> | ||
− | class | + | class Point3{ |
− | { | + | |
public: | public: | ||
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {} | 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; | double x,y,z; | ||
− | + | virtual void print(){ cout << "(" << x << "," << y << "," << z << ")" << endl; } | |
− | virtual void | + | |
− | + | ||
}; | }; | ||
− | class | + | class Coordinate: Point3 |
{ | { | ||
− | void | + | void point(){ |
− | + | cout << "Coordinate (" << x << "," << y << "," << z << ")" << endl; | |
− | + | ||
} | } | ||
}; | }; | ||
− | int main( | + | 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; | return 0; | ||
} | } | ||
</source> | </source> | ||
+ | |||
==Virtual Functions & Abstract Classes== | ==Virtual Functions & Abstract Classes== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | + | #include <iostream> | |
+ | |||
+ | class Vehicle | ||
{ | { | ||
− | return | + | 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; | ||
} | } | ||
</source> | </source> |
Latest revision as of 00:38, 29 June 2010
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; }