Polymorphism
Static Binding

#include <iostream>
#include "Vehicle.h"
#include "Car.h"
#include "Bicycle.h"

void display(Vehicle &v)
{
	std::cout<<v.getProducer()<<std::endl;
	std::cout<<v.toString()<<std::endl;//toString in Vehicle class is called
}

int main(int argc, char *argv[])
{
	Vehicle v("Buick");
	Car c("Honda", "Accord");
	Bicycle b("Yongjiu", 26);

	std::cout<<"-----------------------"<<std::endl;
	display(v);
	display(c);
	display(b);
	std::cout<<"-----------------------"<<std::endl;

	return 0;
}
			
Dynamic Binding

//Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <string>

class Vehicle
{
	private:
		std::string producer;
	public:
		//constructor
		Vehicle(std::string s);

		std::string getProducer() const {return producer;}
		void setProducer(std::string producer);

		virtual std::string toString();

		~Vehicle();
};
#endif
			
//Vehicle.cpp
#include <iostream>
#include <string>
#include "Vehicle.h"

//constructor
Vehicle::Vehicle(std::string s):producer(s)
{
	std::cout<<"Vehicle Constructor ..."<<std::endl;
}

void Vehicle::setProducer(std::string producer)
{
	this->producer = producer;
}

std::string Vehicle::toString()
{
	std::string temp;
	temp = "Vehicle Producer: "+producer;
	return temp;
}

Vehicle::~Vehicle()
{
	std::cout<<"Vehicle Destructor ..."<<std::endl;
}
			
//Car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string>
#include "Vehicle.h"

class Car : public Vehicle
{
	private:
		std::string model;
	public:
		//constructor
		Car(std::string p, std::string m);

		std::string getModel() const {return model;}
		void setModel(std::string model);

		std::string toString();

		~Car();
};
#endif
			
//Car.cpp
#include <iostream>
#include <string>
#include "Vehicle.h"
#include "Car.h"

//constructor
Car::Car(std::string p, std::string m):Vehicle(p), model(m)
{
	std::cout<<"Car Constructor ..."<<std::endl;
}

void Car::setModel(std::string model)
{
	this->model = model;
}

std::string Car::toString()
{
	std::string temp;
	temp = "Car Producer: "+getProducer()+" Model: "+model;
	return temp;
}

Car::~Car()
{
	std::cout<<"Car Destructor ..."<<std::endl;
}
			
//Bicycle.h
#ifndef BICYCLE_H
#define BICYCLE_H
#include <iostream>
#include <string>
#include "Vehicle.h"

class Bicycle : public Vehicle
{
	private:
		int size;
	public:
		//constructor
		Bicycle(std::string p, int s);

		int getSize() const {return size;}
		void setSize(int s);

		std::string toString();

		~Bicycle();
};
#endif
			
//Bicycle.cpp
#include <iostream>
#include <string>
#include "Vehicle.h"
#include "Bicycle.h"

//constructor
Bicycle::Bicycle(std::string p, int s) : Vehicle(p), size(s)
{
	std::cout<<"Bicycle Constructor ..."<<std::endl;
}

void Bicycle::setSize(int s)
{
	size = s;
}

std::string Bicycle::toString()
{
	std::string temp;
	temp = "Bicycle Producer: "+getProducer()+" Size: "+std::to_string(size);
	return temp;
}

Bicycle::~Bicycle()
{
	std::cout<<"Bicycle Destructor ..."<<std::endl;
}
			
//main.cpp
#include <iostream>
#include "Vehicle.h"
#include "Car.h"
#include "Bicycle.h"

void display(Vehicle &v)
{
	std::cout<<v.getProducer()<<std::endl;
	std::cout<<v.toString()<<std::endl;
}

int main(int argc, char *argv[])
{
	Vehicle v("Buick");
	Car c("Honda", "Accord");
	Bicycle b("Yongjiu", 26);

	std::cout<<"-----------------------"<<std::endl;
	display(v);
	display(c);
	display(b);
	std::cout<<"-----------------------"<<std::endl;

	return 0;
}
			
Virtual Destructor
//Use the code of static binding
#include <iostream>
#include "Vehicle.h"
#include "Car.h"
#include "Bicycle.h"

void display(Vehicle &v)
{
	std::cout<<v.toString()<<std::endl;
}

int main(int argc, char *argv[])
{
	Vehicle *c = new Car("Honda", "Accord");
	display(*c);

	delete c;

	return 0;
}
			
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <string>

class Vehicle
{
	private:
		std::string producer;
	public:
		//constructor
		Vehicle(std::string s);

		std::string getProducer() const {return producer;}
		void setProducer(std::string producer);

		std::string toString();

		virtual ~Vehicle();
};
#endif
		
Pointer
//Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include 
#include 

class Vehicle
{
	private:
		std::string producer;
	public:
		//constructor
		Vehicle(std::string s);

		std::string getProducer() const {return producer;}
		void setProducer(std::string producer);

		virtual std::string toString();

		virtual ~Vehicle();
};
#endif
			
//main.cpp
#include <iostream>
#include "Vehicle.h"
#include "Car.h"
#include "Bicycle.h"

void display(Vehicle *v)
{
	std::cout<<v->toString()<<std::endl;
}

int main(int argc, char *argv[])
{
	Vehicle *v[3] = {new Vehicle("Buick"), new Car("Honda", "Accord"), new Bicycle("Yongjiu", 26)};

	std::cout<<"-----------------------"<<std::endl;
	for(int i = 0; i < 3; i++)
		display(v[i]);
	std::cout<<"-----------------------"<<std::endl;

	for(int i = 0; i < 3; i++)
		delete v[i];

	return 0;
}
			
Abstract Class

#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <string>

class Vehicle
{
	private:
		std::string producer;
	public:
		//constructor
		Vehicle(std::string s);

		std::string getProducer() const {return producer;}
		void setProducer(std::string producer);

		virtual std::string toString() = 0;

		virtual ~Vehicle();
};
#endif